-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.go
198 lines (170 loc) · 4.55 KB
/
commandline.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"unicode/utf8"
gsq "github.com/kballard/go-shellquote"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
var arrowLeft = '←'
var arrowRight = '→'
const preferred_horizontal_threshold = 5
func fill(x, y, w, h int, cell termbox.Cell) {
for ly := 0; ly < h; ly++ {
for lx := 0; lx < w; lx++ {
termbox.SetCell(x+lx, y+ly, cell.Ch, cell.Fg, cell.Bg)
}
}
}
type CommandLine struct {
text []byte
lineCellOffset int
cursorByteOffset int
cursorCellOffset int
}
// Draws the CommandLine in the given location, 'h' is not used at the moment
func (eb *CommandLine) Draw(x, y, w, h int) {
eb.AdjustLineCellOffset(w)
const coldef = termbox.ColorDefault
const colred = termbox.ColorRed
fill(x, y, w, h, termbox.Cell{Ch: ' '})
t := eb.text
lx := 0
for {
rx := lx - eb.lineCellOffset
if len(t) == 0 {
break
}
if rx >= w {
termbox.SetCell(x+w-1, y, arrowRight,
colred, coldef)
break
}
r, size := utf8.DecodeRune(t)
if rx >= 0 {
termbox.SetCell(x+rx, y, r, coldef, coldef)
}
lx += runewidth.RuneWidth(r)
t = t[size:]
}
if eb.lineCellOffset != 0 {
termbox.SetCell(x, y, arrowLeft, colred, coldef)
}
}
// AdjustLineCellOffset sets line visual offset based on desired line width.
func (eb *CommandLine) AdjustLineCellOffset(width int) {
ht := preferred_horizontal_threshold
max_h_threshold := (width - 1) / 2
if ht > max_h_threshold {
ht = max_h_threshold
}
threshold := width - 1
if eb.lineCellOffset != 0 {
threshold = width - ht
}
if eb.cursorCellOffset-eb.lineCellOffset >= threshold {
eb.lineCellOffset = eb.cursorCellOffset + (ht - width + 1)
}
if eb.lineCellOffset != 0 && eb.cursorCellOffset-eb.lineCellOffset < ht {
eb.lineCellOffset = eb.cursorCellOffset - ht
if eb.lineCellOffset < 0 {
eb.lineCellOffset = 0
}
}
}
func (eb *CommandLine) MoveCursorTo(boffset int) {
eb.cursorByteOffset = boffset
eb.cursorCellOffset = wcwidth(eb.text[:boffset])
}
func (eb *CommandLine) RuneUnderCursor() (rune, int) {
return utf8.DecodeRune(eb.text[eb.cursorByteOffset:])
}
func (eb *CommandLine) RuneBeforeCursor() (rune, int) {
return utf8.DecodeLastRune(eb.text[:eb.cursorByteOffset])
}
func (eb *CommandLine) MoveCursorOneRuneBackward() {
if eb.cursorByteOffset == 0 {
return
}
_, size := eb.RuneBeforeCursor()
eb.MoveCursorTo(eb.cursorByteOffset - size)
}
func (eb *CommandLine) MoveCursorOneRuneForward() {
if eb.cursorByteOffset == len(eb.text) {
return
}
_, size := eb.RuneUnderCursor()
eb.MoveCursorTo(eb.cursorByteOffset + size)
}
func (eb *CommandLine) DeleteRuneBackward() {
if eb.cursorByteOffset == 0 {
return
}
eb.MoveCursorOneRuneBackward()
_, size := eb.RuneUnderCursor()
eb.text = byteSliceRemove(eb.text, eb.cursorByteOffset, eb.cursorByteOffset+size)
}
func (eb *CommandLine) DeleteAll() {
eb.MoveCursorTo(0)
eb.text = byteSliceRemove(eb.text, 0, len(eb.text))
}
func (eb *CommandLine) InsertRune(r rune) {
var buf [utf8.UTFMax]byte
n := utf8.EncodeRune(buf[:], r)
eb.text = byteSliceInsert(eb.text, eb.cursorByteOffset, buf[:n])
eb.MoveCursorOneRuneForward()
}
func (eb *CommandLine) Redraw() {
w, h := termbox.Size()
termbox.SetCell(0, h-1, ':', termbox.ColorDefault, termbox.ColorDefault)
eb.Draw(1, h-1, w-1, 1)
termbox.SetCursor(1+eb.CursorX(), h-1)
}
func (cl *CommandLine) Run(events <-chan termbox.Event) <-chan []string {
ch := make(chan []string)
go func() {
for {
switch ev := <-events; ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
cl.MoveCursorOneRuneBackward()
case termbox.KeyArrowRight, termbox.KeyCtrlF:
cl.MoveCursorOneRuneForward()
case termbox.KeyBackspace, termbox.KeyBackspace2:
cl.DeleteRuneBackward()
case termbox.KeyTab:
case termbox.KeySpace:
cl.InsertRune(' ')
case termbox.KeyHome, termbox.KeyCtrlA:
cl.MoveCursorTo(0)
case termbox.KeyEnd, termbox.KeyCtrlE:
cl.MoveCursorTo(len(cl.text))
case termbox.KeyEnter:
tokens, err := gsq.Split(string(cl.text))
if err != nil {
panic(err)
}
if len(tokens) == 0 {
break
}
ch <- tokens
cl.DeleteAll()
default:
if ev.Ch != 0 {
cl.InsertRune(ev.Ch)
}
}
case termbox.EventError:
panic(ev.Err)
}
cl.Redraw()
termbox.Flush()
}
}()
return ch
}
// Please, keep in mind that cursor depends on the value of lineCellOffset, which
// is being set on Draw() call, so.. call this method after Draw() one.
func (eb *CommandLine) CursorX() int {
return eb.cursorCellOffset - eb.lineCellOffset
}