forked from evanmiller/hecate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_editor.go
47 lines (43 loc) · 1.48 KB
/
field_editor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"unicode"
"unicode/utf8"
"github.com/nsf/termbox-go"
)
type FieldEditor struct {
value []byte
cursor_pos int
}
func (field_editor *FieldEditor) handleKeyEvent(event termbox.Event) (string, bool) {
is_done := false
if event.Key == termbox.KeyEnter {
is_done = true
} else if event.Key == termbox.KeyEsc {
is_done = true
field_editor.value = nil
} else if event.Key == termbox.KeyArrowLeft {
if field_editor.cursor_pos > 0 {
field_editor.cursor_pos--
}
} else if event.Key == termbox.KeyArrowUp || event.Key == termbox.KeyCtrlA {
field_editor.cursor_pos = 0
} else if event.Key == termbox.KeyArrowRight {
if field_editor.cursor_pos < utf8.RuneCount(field_editor.value) {
field_editor.cursor_pos++
}
} else if event.Key == termbox.KeyArrowDown || event.Key == termbox.KeyCtrlE {
field_editor.cursor_pos = utf8.RuneCount(field_editor.value)
} else if event.Key == termbox.KeyCtrlH || event.Key == termbox.KeyBackspace {
if field_editor.cursor_pos > 0 {
field_editor.value = removeRuneAtIndex(field_editor.value, field_editor.cursor_pos-1)
field_editor.cursor_pos--
}
} else if unicode.IsPrint(event.Ch) {
field_editor.value = insertRuneAtIndex(field_editor.value, field_editor.cursor_pos, event.Ch)
field_editor.cursor_pos++
} else if event.Key == termbox.KeySpace {
field_editor.value = insertRuneAtIndex(field_editor.value, field_editor.cursor_pos, ' ')
field_editor.cursor_pos++
}
return string(field_editor.value), is_done
}