/
editline.go
217 lines (190 loc) · 4.53 KB
/
editline.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package platform
import (
"unicode"
"unicode/utf8"
"github.com/jcorbin/anansi/ansi"
)
// EditLineState code.
type EditLineState uint8
// EditLineState constants.
const (
EditLineReady EditLineState = iota
EditLineDone
EditLineCanceled
)
// EditLine provides basic line-editing .
type EditLine struct {
State EditLineState
Box ansi.Rectangle
Buf []byte
Cur int
View int
handleEvent editLineHandler
}
// Reset state.
func (edl *EditLine) Reset() {
edl.State = EditLineReady
edl.Box.Min = ansi.Pt(1, 1)
edl.Box.Max = edl.Box.Min
edl.Buf = edl.Buf[:0]
edl.View = 0
edl.Cur = 0
}
// Active returns true if the edit line is accepting user input.
func (edl *EditLine) Active() bool { return edl.State == EditLineReady }
// Done returns true if the user submitted input (e.g. hit <Enter>).
func (edl *EditLine) Done() bool { return edl.State == EditLineDone }
// Canceled returns true if the user canceled editing (e.g. hit <Esc>).
func (edl *EditLine) Canceled() bool { return edl.State == EditLineCanceled }
// Update processes user input and draws the edit line.
func (edl *EditLine) Update(ctx *Context) {
if edl.State != EditLineReady {
return
}
if edl.handleEvent == nil {
edl.handleEvent = defaultBehavior
}
for eid := range ctx.Input.Type {
edl.handleEvent(edl, ctx, eid)
}
if edl.State != EditLineReady {
return
}
// TODO clamp to box, showing cursor
n := utf8.RuneCount(edl.Buf)
// ensure cursor past left rune offset
if edl.Cur < edl.View {
if edl.Cur > 0 {
edl.View = edl.Cur - 1
} else {
edl.View = 0
}
}
// ensure cursor within right rune offset
hi := edl.View + n
if w := edl.Box.Dx(); n > w {
hi = edl.View + w
}
if edl.Cur >= hi {
d := edl.Cur - hi
if hi < n {
d++
}
hi += d
edl.View += d
} else if reclaim := hi - n; reclaim > 0 {
edl.View -= reclaim
hi -= reclaim
}
lo := edl.View
// TODO ellipses
b := edl.Buf
for i := 0; i < lo; i++ {
_, n := utf8.DecodeRune(b)
b = b[n:]
}
for i := hi; i < n; i++ {
_, n := utf8.DecodeLastRune(b)
b = b[:len(b)-n]
}
if len(b) > 0 {
ctx.Output.To(edl.Box.Min)
ctx.Output.Write(b)
}
ctx.Output.UserCursor.Visible = true
ctx.Output.UserCursor.Point = ansi.Pt(edl.Box.Min.X+edl.Cur-edl.View, edl.Box.Min.Y)
if ctx.Output.Cursor.X == ctx.Output.UserCursor.X {
ctx.Output.WriteRune(' ')
}
return
}
// TODO elaborate the monadic structure around this
type editLineHandler func(edl *EditLine, ctx *Context, eid int)
// TODO other behaviors to support advanced editing ala Emacs/Vi-syle
var defaultBehavior = editLineHandlers(
// TODO accept bracketed paste
(*EditLine).handleGraphicRune,
(*EditLine).handleControlRune,
(*EditLine).handleArrowKeys,
)
func editLineHandlers(hs ...editLineHandler) editLineHandler {
return func(edl *EditLine, ctx *Context, eid int) {
for i := 0; i < len(hs) && ctx.Input.Type[eid] != EventNone; i++ {
hs[i](edl, ctx, eid)
}
return
}
}
func (edl *EditLine) handleGraphicRune(ctx *Context, eid int) {
if ctx.Input.Type[eid] != EventRune || !unicode.IsGraphic(ctx.Input.Rune(eid)) {
return
}
var tmp [4]byte
b := tmp[:utf8.EncodeRune(tmp[:], ctx.Input.Rune(eid))]
n := utf8.RuneCount(edl.Buf)
edl.Buf = append(edl.Buf, b...)
if edl.Cur < n {
off := 0
in := edl.Buf
in0 := in
for i := 0; i < edl.Cur; i++ {
off += n
_, n := utf8.DecodeRune(in)
in = in[n:]
}
copy(in[len(b):], in)
copy(in, b)
edl.Buf = in0
}
edl.Cur++
ctx.Input.Type[eid] = EventNone
return
}
func (edl *EditLine) handleControlRune(ctx *Context, eid int) {
if ctx.Input.Type[eid] != EventRune {
return
}
switch ctx.Input.Rune(eid) {
case '\x0D': // <Enter> submits
edl.State = EditLineDone
case '\x1B': // ESC cancels
edl.State = EditLineCanceled
case '\x7F': // <Delete> backwards
if edl.Cur > 0 {
if edl.Cur < utf8.RuneCount(edl.Buf) {
in := edl.Buf
for j := 0; j < edl.Cur; j++ {
_, n := utf8.DecodeRune(in)
in = in[n:]
}
_, n := utf8.DecodeRune(in)
copy(in, in[n:])
edl.Buf = edl.Buf[:len(edl.Buf)-n]
} else {
_, n := utf8.DecodeLastRune(edl.Buf)
edl.Buf = edl.Buf[:len(edl.Buf)-n]
}
edl.Cur--
}
default:
return
}
ctx.Input.Type[eid] = EventNone
return
}
func (edl *EditLine) handleArrowKeys(ctx *Context, eid int) {
if ctx.Input.Type[eid] != EventEscape {
return
}
e := ctx.Input.Escape(eid)
if move, is := ansi.DecodeCursorCardinal(e.ID, e.Arg); is {
edl.Cur += move.X
if n := utf8.RuneCount(edl.Buf); edl.Cur > n {
edl.Cur = n
} else if edl.Cur < 0 {
edl.Cur = 0
}
ctx.Input.Type[eid] = EventNone
}
return
}