diff --git a/lib/line_editor.go b/lib/line_editor.go index 225959f..febce62 100644 --- a/lib/line_editor.go +++ b/lib/line_editor.go @@ -3,6 +3,7 @@ package mop import ( +"fmt" "github.com/nsf/termbox-go" ) @@ -40,19 +41,50 @@ func (self *LineEditor) Handle(ev termbox.Event) bool { termbox.Flush() return true case termbox.KeyBackspace, termbox.KeyBackspace2: - if len(self.input) > 0 { - self.input = self.input[:len(self.input)-1] - DrawLine(len(self.prompt), 3, self.input + " ") - termbox.SetCursor(len(self.prompt)+len(self.input), 3) - termbox.Flush() - } + if len(self.input) > 0 { + 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: + if self.cursor > 0 { + self.cursor -= 1 + termbox.SetCursor(len(self.prompt) + self.cursor, 3) + termbox.Flush() + } + case termbox.KeyCtrlF, termbox.KeyArrowRight: + if self.cursor < len(self.input) { + self.cursor += 1 + termbox.SetCursor(len(self.prompt) + self.cursor, 3) + termbox.Flush() + } + case termbox.KeyCtrlA: // Jump to the beginning of line. + self.cursor = 0 + 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: + self.append_character(' ') default: if ev.Ch != 0 { - self.input += string(ev.Ch) - DrawLine(len(self.prompt), 3, self.input) - termbox.SetCursor(len(self.prompt)+len(self.input), 3) - termbox.Flush() + self.append_character(ev.Ch) } } + DrawLine(20,20, fmt.Sprintf("cursor: %02d [%s] %08d", self.cursor, self.input, ev.Ch)) return false } + +//----------------------------------------------------------------------------- +func (self *LineEditor) append_character(ch rune) { + self.input += string(ch) + self.cursor += 1 + DrawLine(len(self.prompt), 3, self.input) + termbox.SetCursor(len(self.prompt) + self.cursor, 3) + termbox.Flush() +} +