about summary refs log tree commit diff stats
path: root/src/io/lineedit.nim
blob: 3336d7ac38ef57bc75417c0401638ba8481d28ac (plain) (blame)
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
import terminal
import unicode
import strutils
import sequtils
import sugar

import config/config
import io/term
import utils/twtstr

type LineState* = object
  news*: seq[Rune]
  prompt*: string
  current: string
  s: string
  feedNext: bool
  escNext: bool
  cursor: int
  shift: int
  minlen: int
  maxlen: int
  displen: int
  disallowed: set[char]
  hide: bool
  config: Config #TODO get rid of this
  tty: File
  callback: proc(state: var LineState): bool

func lwidth(r: Rune): int =
  if r.isControlChar():
    return 2
  return r.width()

func lwidth(s: string): int =
  for r in s.runes():
    result += lwidth(r)

func lwidth(s: seq[Rune]): int =
  for r in s:
    result += lwidth(r)

func lwidth(s: seq[Rune], min, max: int): int =
  var i = min
  var mi = min(max, s.len)
  while i < mi:
    result += lwidth(s[i])
    inc i

func lwidth(s: seq[Rune], min: int): int =
  var i = min
  while i < s.len:
    result += lwidth(s[i])
    inc i

template kill(state: LineState, i: int) =
  state.space(i)
  state.backward(i)

template kill(state: LineState) =
  let w = min(state.news.lwidth(state.cursor), state.displen)
  state.kill(w)

proc backward(state: LineState, i: int) =
  if i > 0:
    if i == 1:
      print('\b')
    else:
      cursorBackward(i)

proc forward(state: LineState, i: int) =
  if i > 0:
    cursorForward(i)

proc begin(state: LineState) =
  print('\r')
  state.forward(state.minlen)

proc space(state: LineState, i: int) =
  print(' '.repeat(i))

proc redraw(state: var LineState) =
  var dispw = state.news.lwidth(state.shift, state.shift + state.displen)
  if state.shift + state.displen > state.news.len:
    state.displen = state.news.len - state.shift
  while dispw > state.maxlen - 1:
    dispw -= state.news[state.shift + state.displen - 1].lwidth()
    dec state.displen

  state.begin()
  let os = state.news.substr(state.shift, state.shift + state.displen)
  if state.hide:
    printesc('*'.repeat(os.lwidth()))
  else:
    printesc($os)
  state.space(max(state.maxlen - state.minlen - os.lwidth(), 0))

  state.begin()
  state.forward(state.news.lwidth(state.shift, state.cursor))

proc zeroShiftRedraw(state: var LineState) =
  state.shift = 0
  state.displen = state.maxlen - 1
  state.redraw()

proc fullRedraw(state: var LineState) =
  state.displen = state.maxlen - 1
  if state.cursor > state.shift:
    var shiftw = state.news.lwidth(state.shift, state.cursor)
    while shiftw > state.maxlen - 1:
      inc state.shift
      shiftw -= state.news[state.shift].lwidth()
  else:
    state.shift = max(state.cursor - 1, 0)

  state.redraw()

proc insertCharseq(state: var LineState, cs: var seq[Rune], disallowed: set[char]) =
  let escNext = state.escNext
  cs.keepIf((r) => (escNext or not r.isControlChar) and not (r.isAscii and char(r) in disallowed))
  state.escNext = false
  if cs.len == 0:
    return

  if state.cursor >= state.news.len and state.news.lwidth(state.shift, state.cursor) + cs.lwidth() < state.displen:
    state.news &= cs
    state.cursor += cs.len
    if state.hide:
      printesc('*'.repeat(cs.lwidth()))
    else:
      printesc($cs)
  else:
    state.news.insert(cs, state.cursor)
    state.cursor += cs.len
    state.fullRedraw()

proc readLine(state: var LineState): bool =
  printesc(state.prompt)
  if state.hide:
    printesc('*'.repeat(state.current.lwidth()))
  else:
    printesc(state.current)

  while true:
    if not state.feedNext:
      state.s = ""
    else:
      state.feedNext = false

    restoreStdin(state.tty.getFileHandle())
    let c = state.tty.readChar()
    state.s &= c

    var action = getLinedAction(state.config, state.s)
    if state.escNext:
      action = NO_ACTION
    case action
    of ACTION_LINED_CANCEL:
      return false
    of ACTION_LINED_SUBMIT:
      return true
    of ACTION_LINED_BACKSPACE:
      if state.cursor > 0:
        let w = state.news[state.cursor - 1].lwidth()
        state.news.delete(state.cursor - 1..state.cursor - 1)
        dec state.cursor
        if state.cursor == state.news.len and state.shift == 0:
          state.backward(w)
          state.kill(w)
        else:
          state.fullRedraw()
    of ACTION_LINED_DELETE:
      if state.cursor >= 0 and state.cursor < state.news.len:
        let w = state.news[state.cursor].lwidth()
        state.news.delete(state.cursor..state.cursor)
        if state.cursor == state.news.len and state.shift == 0:
          state.kill(w)
        else:
          state.fullRedraw()
    of ACTION_LINED_ESC:
      state.escNext = true
    of ACTION_LINED_CLEAR:
      if state.cursor > 0:
        state.news.delete(0..state.cursor - 1)
        state.cursor = 0
        state.zeroShiftRedraw()
    of ACTION_LINED_KILL:
      if state.cursor < state.news.len:
        state.kill()
        state.news.setLen(state.cursor)
    of ACTION_LINED_BACK:
      if state.cursor > 0:
        dec state.cursor
        if state.cursor > state.shift or state.shift == 0:
          state.backward(state.news[state.cursor].lwidth())
        else:
          state.fullRedraw()
    of ACTION_LINED_FORWARD:
      if state.cursor < state.news.len:
        inc state.cursor
        if state.news.lwidth(state.shift, state.cursor) < state.displen:
          var n = 1
          if state.news.len > state.cursor:
            n = state.news[state.cursor].lwidth()
          state.forward(n)
        else:
          state.fullRedraw()
    of ACTION_LINED_PREV_WORD:
      let oc = state.cursor
      while state.cursor > 0:
        dec state.cursor
        if state.news[state.cursor].breaksWord():
          break
      if state.cursor != oc:
        if state.cursor > state.shift or state.shift == 0:
          state.backward(state.news.lwidth(state.cursor, oc))
        else:
          state.fullRedraw()
    of ACTION_LINED_NEXT_WORD:
      let oc = state.cursor
      while state.cursor < state.news.len:
        inc state.cursor
        if state.cursor < state.news.len:
          if state.news[state.cursor].breaksWord():
            break

      if state.cursor != oc:
        let dw = state.news.lwidth(oc, state.cursor)
        if oc + dw - state.shift < state.displen:
          state.forward(dw)
        else:
          state.fullRedraw()
    of ACTION_LINED_KILL_WORD:
      var chars = 0
      if state.cursor > chars:
        inc chars

      while state.cursor > chars:
        inc chars
        if state.news[state.cursor - chars].breaksWord():
          dec chars
          break
      if chars > 0:
        let w = state.news.lwidth(state.cursor - chars, state.cursor)
        state.news.delete(state.cursor - chars..state.cursor - 1)
        state.cursor -= chars
        if state.cursor == state.news.len and state.shift == 0:
          state.backward(w)
          state.kill(w)
        else:
          state.fullRedraw()
    of ACTION_LINED_BEGIN:
      if state.cursor > 0:
        if state.shift == 0:
          state.backward(state.news.lwidth(0, state.cursor))
        else:
          state.fullRedraw()
        state.cursor = 0
    of ACTION_LINED_END:
      if state.cursor < state.news.len:
        if state.news.lwidth(state.shift, state.news.len) < state.maxlen:
          state.forward(state.news.lwidth(state.cursor, state.news.len))
        else:
          state.fullRedraw()
        state.cursor = state.news.len
    of ACTION_FEED_NEXT:
      state.feedNext = true
    elif validateUtf8(state.s) == -1:
      var cs = state.s.toRunes()
      state.insertCharseq(cs, state.disallowed)
      if state.callback(state):
        state.fullRedraw()
    else:
      state.feedNext = true

proc readLine*(prompt: string, current: var string, termwidth: int,
               disallowed: set[char], hide: bool, config: Config,
               tty: File, callback: proc(state: var LineState): bool): bool =
  var state: LineState

  state.prompt = prompt
  state.current = current
  state.news = current.toRunes()
  state.cursor = state.news.len
  state.minlen = prompt.lwidth()
  state.maxlen = termwidth - prompt.len
  state.displen = state.maxlen - 1
  state.disallowed = disallowed
  state.callback = callback
  state.hide = hide
  state.config = config
  state.tty = tty

  if state.readLine():
    current = $state.news
    return true
  return false

proc readLine*(prompt: string, current: var string, termwidth: int,
               disallowed: set[char] = {}, hide = false, config: Config,
               tty: File): bool =
  readLine(prompt, current, termwidth, disallowed, hide, config, tty, (proc(state: var LineState): bool = false))