Finished basic features of the line editor

master
Michael Dvorkin 12 years ago
parent 26f1b60fb3
commit a26b37bb46
  1. 100
      lib/line_editor.go

@ -29,50 +29,40 @@ func (self *LineEditor) Prompt(command rune) {
//-----------------------------------------------------------------------------
func (self *LineEditor) Handle(ev termbox.Event) bool {
defer termbox.Flush()
switch ev.Key {
case termbox.KeyEsc:
ClearLine(0, 3)
termbox.HideCursor()
termbox.Flush()
return true
case termbox.KeyEnter:
ClearLine(0, 3)
termbox.HideCursor()
termbox.Flush()
return true
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) + self.cursor, 3)
termbox.Flush()
}
self.delete_previous_character()
case termbox.KeyCtrlB, termbox.KeyArrowLeft:
if self.cursor > 0 {
self.cursor -= 1
termbox.SetCursor(len(self.prompt) + self.cursor, 3)
termbox.Flush()
}
self.move_left()
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()
self.move_right()
case termbox.KeyCtrlA:
self.jump_to_beginning()
case termbox.KeyCtrlE:
self.jump_to_end()
case termbox.KeySpace:
self.append_character(' ')
self.insert_character(' ')
default:
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))
@ -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.cursor += 1
}
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.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)
}

Loading…
Cancel
Save