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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
import std/strutils
import std/unicode
import bindings/quickjs
import display/term
import js/javascript
import types/cell
import types/opt
import utils/strwidth
import utils/twtstr
import chagashi/charset
import chagashi/validator
import chagashi/decoder
type
LineEditState* = enum
EDIT, FINISH, CANCEL
LineHistory* = ref object
lines: seq[string]
LineEdit* = ref object
news*: string
prompt*: string
promptw: int
state*: LineEditState
escNext*: bool
cursorx: int # 0 ..< news.notwidth
cursori: int # 0 ..< news.len
shiftx: int # 0 ..< news.notwidth
shifti: int # 0 ..< news.len
padding: int # 0 or 1
maxwidth: int
disallowed: set[char]
hide: bool
hist: LineHistory
histindex: int
histtmp: string
invalid*: bool
jsDestructor(LineEdit)
func newLineHistory*(): LineHistory =
return LineHistory()
# Note: capped at edit.maxwidth.
func getDisplayWidth(edit: LineEdit): int =
var dispw = 0
var i = edit.shifti
var r: Rune
while i < edit.news.len and dispw < edit.maxwidth:
fastRuneAt(edit.news, i, r)
dispw += r.width()
return dispw
proc shiftView(edit: LineEdit) =
# Shift view so it contains the cursor.
if edit.cursorx < edit.shiftx:
edit.shiftx = edit.cursorx
edit.shifti = edit.cursori
# Shift view so it is completely filled.
if edit.shiftx > 0:
let dispw = edit.getDisplayWidth()
if dispw < edit.maxwidth:
let targetx = edit.shiftx - edit.maxwidth + dispw
if targetx <= 0:
edit.shiftx = 0
edit.shifti = 0
else:
while edit.shiftx > targetx:
let (r, len) = edit.news.lastRune(edit.shifti - 1)
edit.shiftx -= r.width()
edit.shifti -= len
edit.padding = 0
# Shift view so it contains the cursor. (act 2)
if edit.shiftx < edit.cursorx - edit.maxwidth:
while edit.shiftx < edit.cursorx - edit.maxwidth and
edit.shifti < edit.news.len:
var r: Rune
fastRuneAt(edit.news, edit.shifti, r)
edit.shiftx += r.width()
if edit.shiftx > edit.cursorx - edit.maxwidth:
# skipped over a cell because of a double-width char
edit.padding = 1
proc generateOutput*(edit: LineEdit): FixedGrid =
edit.shiftView()
# Make the output grid +1 cell wide, so it covers the whole input area.
result = newFixedGrid(edit.promptw + edit.maxwidth + 1)
var x = 0
for r in edit.prompt.runes:
result[x].str &= $r
x += r.width()
if x >= result.width: break
for i in 0 ..< edit.padding:
if x < result.width:
result[x].str = " "
inc x
var i = edit.shifti
while i < edit.news.len:
var r: Rune
fastRuneAt(edit.news, i, r)
if not edit.hide:
let w = r.width()
if x + w > result.width: break
if r.isControlChar():
result[x].str &= '^'
inc x
result[x].str &= char(r).getControlLetter()
inc x
else:
result[x].str &= $r
x += w
else:
if x + 1 > result.width: break
result[x].str &= '*'
inc x
proc getCursorX*(edit: LineEdit): int =
return edit.promptw + edit.cursorx + edit.padding - edit.shiftx
proc insertCharseq(edit: LineEdit, s: string) =
let s = if edit.escNext:
s
else:
deleteChars(s, edit.disallowed)
edit.escNext = false
if s.len == 0:
return
let rem = edit.news.substr(edit.cursori)
edit.news.setLen(edit.cursori)
edit.news &= s
edit.news &= rem
edit.cursori += s.len
edit.cursorx += s.notwidth()
edit.invalid = true
proc cancel(edit: LineEdit) {.jsfunc.} =
edit.state = CANCEL
proc submit(edit: LineEdit) {.jsfunc.} =
if edit.hist.lines.len == 0 or edit.news != edit.hist.lines[^1]:
edit.hist.lines.add(edit.news)
edit.state = FINISH
proc backspace(edit: LineEdit) {.jsfunc.} =
if edit.cursori > 0:
let (r, len) = edit.news.lastRune(edit.cursori - 1)
edit.news.delete(edit.cursori - len .. edit.cursori - 1)
edit.cursori -= len
edit.cursorx -= r.width()
edit.invalid = true
proc write*(edit: LineEdit, s: string, cs: Charset): bool =
if cs == CHARSET_UTF_8:
if s.validateUTF8Surr() != -1:
return false
edit.insertCharseq(s)
else:
let td = newTextDecoder(cs)
var success = false
let s = td.decodeAll(s, success)
if not success:
return false
edit.insertCharseq(s)
return true
proc write(edit: LineEdit, s: string): bool {.jsfunc.} =
edit.write(s, CHARSET_UTF_8)
proc delete(edit: LineEdit) {.jsfunc.} =
if edit.cursori < edit.news.len:
let len = edit.news.runeLenAt(edit.cursori)
edit.news.delete(edit.cursori ..< edit.cursori + len)
edit.invalid = true
proc escape(edit: LineEdit) {.jsfunc.} =
edit.escNext = true
proc clear(edit: LineEdit) {.jsfunc.} =
if edit.cursori > 0:
edit.news.delete(0..edit.cursori - 1)
edit.cursori = 0
edit.cursorx = 0
edit.invalid = true
proc kill(edit: LineEdit) {.jsfunc.} =
if edit.cursori < edit.news.len:
edit.news.setLen(edit.cursori)
edit.invalid = true
proc backward(edit: LineEdit) {.jsfunc.} =
if edit.cursori > 0:
let (r, len) = edit.news.lastRune(edit.cursori - 1)
edit.cursori -= len
edit.cursorx -= r.width()
if edit.cursorx < edit.shiftx:
edit.invalid = true
proc forward(edit: LineEdit) {.jsfunc.} =
if edit.cursori < edit.news.len:
var r: Rune
fastRuneAt(edit.news, edit.cursori, r)
edit.cursorx += r.width()
if edit.cursorx >= edit.shiftx + edit.maxwidth:
edit.invalid = true
proc prevWord(edit: LineEdit) {.jsfunc.} =
if edit.cursori == 0:
return
let (r, len) = edit.news.lastRune(edit.cursori - 1)
if r.breaksWord():
edit.cursori -= len
edit.cursorx -= r.width()
while edit.cursori > 0:
let (r, len) = edit.news.lastRune(edit.cursori - 1)
if r.breaksWord():
break
edit.cursori -= len
edit.cursorx -= r.width()
if edit.cursorx < edit.shiftx:
edit.invalid = true
proc nextWord(edit: LineEdit) {.jsfunc.} =
if edit.cursori >= edit.news.len:
return
let oc = edit.cursori
var r: Rune
fastRuneAt(edit.news, edit.cursori, r)
if r.breaksWord():
edit.cursorx += r.width()
else:
edit.cursori = oc
while edit.cursori < edit.news.len:
let pc = edit.cursori
fastRuneAt(edit.news, edit.cursori, r)
if r.breaksWord():
edit.cursori = pc
break
edit.cursorx += r.width()
if edit.cursorx >= edit.shiftx + edit.maxwidth:
edit.invalid = true
proc clearWord(edit: LineEdit) {.jsfunc.} =
let oc = edit.cursori
edit.prevWord()
if oc != edit.cursori:
edit.news.delete(edit.cursori .. oc - 1)
edit.invalid = true
proc killWord(edit: LineEdit) {.jsfunc.} =
if edit.cursori >= edit.news.len:
return
let oc = edit.cursori
let ox = edit.cursorx
edit.nextWord()
if edit.cursori != oc:
if edit.cursori < edit.news.len:
let len = edit.news.runeLenAt(edit.cursori)
edit.news.delete(oc ..< edit.cursori + len)
else:
edit.news.delete(oc ..< edit.cursori)
edit.cursori = oc
edit.cursorx = ox
edit.invalid = true
proc begin(edit: LineEdit) {.jsfunc.} =
edit.cursori = 0
edit.cursorx = 0
if edit.shiftx > 0:
edit.invalid = true
proc `end`(edit: LineEdit) {.jsfunc.} =
if edit.cursori < edit.news.len:
edit.cursori = edit.news.len
edit.cursorx = edit.news.notwidth()
if edit.cursorx >= edit.shiftx + edit.maxwidth:
edit.invalid = true
proc prevHist(edit: LineEdit) {.jsfunc.} =
if edit.histindex > 0:
if edit.news.len > 0:
edit.histtmp = $edit.news
dec edit.histindex
edit.news = edit.hist.lines[edit.histindex]
# The begin call is needed so the cursor doesn't get lost outside
# the string.
edit.begin()
edit.end()
edit.invalid = true
proc nextHist(edit: LineEdit) {.jsfunc.} =
if edit.histindex + 1 < edit.hist.lines.len:
inc edit.histindex
edit.news = edit.hist.lines[edit.histindex]
edit.begin()
edit.end()
edit.invalid = true
elif edit.histindex < edit.hist.lines.len:
inc edit.histindex
edit.news = edit.histtmp
edit.begin()
edit.end()
edit.histtmp = ""
proc windowChange*(edit: LineEdit, attrs: WindowAttributes) =
edit.maxwidth = attrs.width - edit.promptw - 1
proc readLine*(prompt: string, termwidth: int, current = "",
disallowed: set[char] = {}, hide = false, hist: LineHistory): LineEdit =
result = LineEdit(
prompt: prompt,
promptw: prompt.width(),
news: current,
disallowed: disallowed,
hide: hide,
invalid: true
)
result.cursori = result.news.len
result.cursorx = result.news.notwidth()
# - 1, so that the cursor always has place
result.maxwidth = termwidth - result.promptw - 1
result.hist = hist
result.histindex = result.hist.lines.len
proc addLineEditModule*(ctx: JSContext) =
ctx.registerType(LineEdit)
|