More line edititng

master
Michael Dvorkin 12 years ago
parent 4de33c3807
commit 26f1b60fb3
  1. 42
      lib/line_editor.go

@ -3,6 +3,7 @@
package mop
import (
"fmt"
"github.com/nsf/termbox-go"
)
@ -42,17 +43,48 @@ func (self *LineEditor) Handle(ev termbox.Event) bool {
case termbox.KeyBackspace, termbox.KeyBackspace2:
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)+len(self.input), 3)
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()
}

Loading…
Cancel
Save