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 package mop
import ( import (
"fmt"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
@ -42,17 +43,48 @@ func (self *LineEditor) Handle(ev termbox.Event) bool {
case termbox.KeyBackspace, termbox.KeyBackspace2: case termbox.KeyBackspace, termbox.KeyBackspace2:
if len(self.input) > 0 { if len(self.input) > 0 {
self.input = self.input[:len(self.input)-1] self.input = self.input[:len(self.input)-1]
self.cursor -= 1
DrawLine(len(self.prompt), 3, self.input + " ") 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() 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: default:
if ev.Ch != 0 { if ev.Ch != 0 {
self.input += string(ev.Ch) self.append_character(ev.Ch)
DrawLine(len(self.prompt), 3, self.input)
termbox.SetCursor(len(self.prompt)+len(self.input), 3)
termbox.Flush()
} }
} }
DrawLine(20,20, fmt.Sprintf("cursor: %02d [%s] %08d", self.cursor, self.input, ev.Ch))
return false 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