|
|
@ -3,6 +3,7 @@ |
|
|
|
package mop |
|
|
|
package mop |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"github.com/nsf/termbox-go" |
|
|
|
"github.com/nsf/termbox-go" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -40,19 +41,50 @@ func (self *LineEditor) Handle(ev termbox.Event) bool { |
|
|
|
termbox.Flush() |
|
|
|
termbox.Flush() |
|
|
|
return true |
|
|
|
return true |
|
|
|
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] |
|
|
|
DrawLine(len(self.prompt), 3, self.input + " ") |
|
|
|
self.cursor -= 1 |
|
|
|
termbox.SetCursor(len(self.prompt)+len(self.input), 3) |
|
|
|
DrawLine(len(self.prompt), 3, self.input + " ") |
|
|
|
termbox.Flush() |
|
|
|
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: |
|
|
|
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() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|