|
|
@ -29,50 +29,40 @@ func (self *LineEditor) Prompt(command rune) { |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func (self *LineEditor) Handle(ev termbox.Event) bool { |
|
|
|
func (self *LineEditor) Handle(ev termbox.Event) bool { |
|
|
|
|
|
|
|
defer termbox.Flush() |
|
|
|
|
|
|
|
|
|
|
|
switch ev.Key { |
|
|
|
switch ev.Key { |
|
|
|
case termbox.KeyEsc: |
|
|
|
case termbox.KeyEsc: |
|
|
|
ClearLine(0, 3) |
|
|
|
ClearLine(0, 3) |
|
|
|
termbox.HideCursor() |
|
|
|
termbox.HideCursor() |
|
|
|
termbox.Flush() |
|
|
|
|
|
|
|
return true |
|
|
|
return true |
|
|
|
|
|
|
|
|
|
|
|
case termbox.KeyEnter: |
|
|
|
case termbox.KeyEnter: |
|
|
|
ClearLine(0, 3) |
|
|
|
ClearLine(0, 3) |
|
|
|
termbox.HideCursor() |
|
|
|
termbox.HideCursor() |
|
|
|
termbox.Flush() |
|
|
|
|
|
|
|
return true |
|
|
|
return true |
|
|
|
|
|
|
|
|
|
|
|
case termbox.KeyBackspace, termbox.KeyBackspace2: |
|
|
|
case termbox.KeyBackspace, termbox.KeyBackspace2: |
|
|
|
if len(self.input) > 0 { |
|
|
|
self.delete_previous_character() |
|
|
|
self.input = self.input[:len(self.input)-1] |
|
|
|
|
|
|
|
self.cursor -= 1 |
|
|
|
|
|
|
|
DrawLine(len(self.prompt), 3, self.input + " ") |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
termbox.Flush() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case termbox.KeyCtrlB, termbox.KeyArrowLeft: |
|
|
|
case termbox.KeyCtrlB, termbox.KeyArrowLeft: |
|
|
|
if self.cursor > 0 { |
|
|
|
self.move_left() |
|
|
|
self.cursor -= 1 |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
termbox.Flush() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case termbox.KeyCtrlF, termbox.KeyArrowRight: |
|
|
|
case termbox.KeyCtrlF, termbox.KeyArrowRight: |
|
|
|
if self.cursor < len(self.input) { |
|
|
|
self.move_right() |
|
|
|
self.cursor += 1 |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
case termbox.KeyCtrlA: |
|
|
|
termbox.Flush() |
|
|
|
self.jump_to_beginning() |
|
|
|
} |
|
|
|
|
|
|
|
case termbox.KeyCtrlA: // Jump to the beginning of line.
|
|
|
|
case termbox.KeyCtrlE: |
|
|
|
self.cursor = 0 |
|
|
|
self.jump_to_end() |
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
termbox.Flush() |
|
|
|
|
|
|
|
case termbox.KeyCtrlE: // Jump to the end of line.
|
|
|
|
|
|
|
|
self.cursor = len(self.input) |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
termbox.Flush() |
|
|
|
|
|
|
|
case termbox.KeySpace: |
|
|
|
case termbox.KeySpace: |
|
|
|
self.append_character(' ') |
|
|
|
self.insert_character(' ') |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
if ev.Ch != 0 { |
|
|
|
if ev.Ch != 0 { |
|
|
|
self.append_character(ev.Ch) |
|
|
|
self.insert_character(ev.Ch) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
DrawLine(20,20, fmt.Sprintf("cursor: %02d [%s] %08d", self.cursor, self.input, ev.Ch)) |
|
|
|
DrawLine(20,20, fmt.Sprintf("cursor: %02d [%s] %08d", self.cursor, self.input, ev.Ch)) |
|
|
@ -80,11 +70,57 @@ func (self *LineEditor) Handle(ev termbox.Event) bool { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func (self *LineEditor) append_character(ch rune) { |
|
|
|
func (self *LineEditor) delete_previous_character() { |
|
|
|
|
|
|
|
if self.cursor > 0 { |
|
|
|
|
|
|
|
if self.cursor < len(self.input) { |
|
|
|
|
|
|
|
// Remove character in the middle of the input string.
|
|
|
|
|
|
|
|
self.input = self.input[0 : self.cursor-1] + self.input[self.cursor : len(self.input)] |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Remove last input character.
|
|
|
|
|
|
|
|
self.input = self.input[ : len(self.input)-1] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
DrawLine(len(self.prompt), 3, self.input + " ") // Erase last character.
|
|
|
|
|
|
|
|
self.move_left() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *LineEditor) insert_character(ch rune) { |
|
|
|
|
|
|
|
if self.cursor < len(self.input) { |
|
|
|
|
|
|
|
// Insert the character in the middle of the input string.
|
|
|
|
|
|
|
|
self.input = self.input[0 : self.cursor] + string(ch) + self.input[self.cursor : len(self.input)] |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Append the character to the end of the input string.
|
|
|
|
self.input += string(ch) |
|
|
|
self.input += string(ch) |
|
|
|
self.cursor += 1 |
|
|
|
} |
|
|
|
DrawLine(len(self.prompt), 3, self.input) |
|
|
|
DrawLine(len(self.prompt), 3, self.input) |
|
|
|
|
|
|
|
self.move_right() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *LineEditor) move_left() { |
|
|
|
|
|
|
|
if self.cursor > 0 { |
|
|
|
|
|
|
|
self.cursor -= 1 |
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
termbox.Flush() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *LineEditor) move_right() { |
|
|
|
|
|
|
|
if self.cursor < len(self.input) { |
|
|
|
|
|
|
|
self.cursor += 1 |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *LineEditor) jump_to_beginning() { |
|
|
|
|
|
|
|
self.cursor = 0 |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *LineEditor) jump_to_end() { |
|
|
|
|
|
|
|
self.cursor = len(self.input) |
|
|
|
|
|
|
|
termbox.SetCursor(len(self.prompt) + self.cursor, 3) |
|
|
|
|
|
|
|
} |
|
|
|