@yaks/tui 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/Frame.js +52 -0
  2. package/Frame.js.map +1 -0
  3. package/Frame.ts +92 -0
  4. package/Image.js +10 -0
  5. package/Image.js.map +1 -0
  6. package/Image.ts +22 -0
  7. package/LICENSE +202 -0
  8. package/README.md +524 -2
  9. package/RenderedCursor.js +103 -0
  10. package/RenderedCursor.js.map +1 -0
  11. package/RenderedCursor.ts +127 -0
  12. package/Scroll.js +88 -0
  13. package/Scroll.js.map +1 -0
  14. package/Scroll.ts +155 -0
  15. package/Textarea.js +186 -0
  16. package/Textarea.js.map +1 -0
  17. package/Textarea.ts +234 -0
  18. package/VirtualList.js +729 -0
  19. package/VirtualList.js.map +1 -0
  20. package/VirtualList.ts +838 -0
  21. package/_dist/Frame.d.ts +25 -0
  22. package/_dist/Frame.d.ts.map +1 -0
  23. package/_dist/Image.d.ts +13 -0
  24. package/_dist/Image.d.ts.map +1 -0
  25. package/_dist/RenderedCursor.d.ts +9 -0
  26. package/_dist/RenderedCursor.d.ts.map +1 -0
  27. package/_dist/Scroll.d.ts +30 -0
  28. package/_dist/Scroll.d.ts.map +1 -0
  29. package/_dist/Textarea.d.ts +26 -0
  30. package/_dist/Textarea.d.ts.map +1 -0
  31. package/_dist/VirtualList.d.ts +103 -0
  32. package/_dist/VirtualList.d.ts.map +1 -0
  33. package/_dist/guide.d.ts +21 -0
  34. package/_dist/guide.d.ts.map +1 -0
  35. package/_dist/history.d.ts +12 -0
  36. package/_dist/history.d.ts.map +1 -0
  37. package/_dist/input.d.ts +47 -0
  38. package/_dist/input.d.ts.map +1 -0
  39. package/_dist/interaction.d.ts +15 -0
  40. package/_dist/interaction.d.ts.map +1 -0
  41. package/_dist/keymap.d.ts +4 -0
  42. package/_dist/keymap.d.ts.map +1 -0
  43. package/_dist/mod.d.ts +42 -0
  44. package/_dist/mod.d.ts.map +1 -0
  45. package/_dist/mouse.d.ts +15 -0
  46. package/_dist/mouse.d.ts.map +1 -0
  47. package/_dist/paint.d.ts +86 -0
  48. package/_dist/paint.d.ts.map +1 -0
  49. package/_dist/print.d.ts +20 -0
  50. package/_dist/print.d.ts.map +1 -0
  51. package/_dist/run.d.ts +19 -0
  52. package/_dist/run.d.ts.map +1 -0
  53. package/_dist/screen.d.ts +22 -0
  54. package/_dist/screen.d.ts.map +1 -0
  55. package/_dist/scrollbar.d.ts +7 -0
  56. package/_dist/scrollbar.d.ts.map +1 -0
  57. package/_dist/testing.d.ts +19 -0
  58. package/_dist/testing.d.ts.map +1 -0
  59. package/_dist/theme.d.ts +11 -0
  60. package/_dist/theme.d.ts.map +1 -0
  61. package/_dist/visual.d.ts +28 -0
  62. package/_dist/visual.d.ts.map +1 -0
  63. package/deno.json +1 -0
  64. package/graphics.js +157 -0
  65. package/graphics.js.map +1 -0
  66. package/graphics.ts +191 -0
  67. package/grid.js +38 -0
  68. package/grid.js.map +1 -0
  69. package/grid.ts +46 -0
  70. package/guide.js +144 -0
  71. package/guide.js.map +1 -0
  72. package/guide.ts +169 -0
  73. package/history.js +58 -0
  74. package/history.js.map +1 -0
  75. package/history.ts +59 -0
  76. package/input.js +356 -0
  77. package/input.js.map +1 -0
  78. package/input.ts +324 -0
  79. package/interaction.js +180 -0
  80. package/interaction.js.map +1 -0
  81. package/interaction.ts +185 -0
  82. package/keymap.js +20 -0
  83. package/keymap.js.map +1 -0
  84. package/keymap.ts +23 -0
  85. package/mod.js +39 -0
  86. package/mod.js.map +1 -0
  87. package/mod.ts +92 -0
  88. package/mouse.js +65 -0
  89. package/mouse.js.map +1 -0
  90. package/mouse.ts +98 -0
  91. package/package.json +49 -4
  92. package/paint.js +1010 -0
  93. package/paint.js.map +1 -0
  94. package/paint.ts +1063 -0
  95. package/print.js +31 -0
  96. package/print.js.map +1 -0
  97. package/print.ts +40 -0
  98. package/run.js +220 -0
  99. package/run.js.map +1 -0
  100. package/run.ts +237 -0
  101. package/screen.js +97 -0
  102. package/screen.js.map +1 -0
  103. package/screen.ts +139 -0
  104. package/scrollbar.js +30 -0
  105. package/scrollbar.js.map +1 -0
  106. package/scrollbar.ts +33 -0
  107. package/table.js +134 -0
  108. package/table.js.map +1 -0
  109. package/table.ts +152 -0
  110. package/testing.js +86 -0
  111. package/testing.js.map +1 -0
  112. package/testing.ts +95 -0
  113. package/textRows.js +38 -0
  114. package/textRows.js.map +1 -0
  115. package/textRows.ts +31 -0
  116. package/theme.js +50 -0
  117. package/theme.js.map +1 -0
  118. package/theme.ts +39 -0
  119. package/visual.js +195 -0
  120. package/visual.js.map +1 -0
  121. package/visual.ts +209 -0
package/Textarea.ts ADDED
@@ -0,0 +1,234 @@
1
+ import { visualRows, visualSpot } from './textRows.js'
2
+ export { type VisualRow, visualRows, visualSpot } from './textRows.js'
3
+ /**
4
+ * The multi-line input box. `edit()` is the whole editor as one pure function
5
+ * — a `{text, at}` and a key in, the next `{text, at}` out, null when the key
6
+ * is none of its business — so every binding is a table row in the test rather
7
+ * than a branch in a component. Enter submits and Shift+Enter opens a line,
8
+ * which is the one thing a terminal cannot say on its own and the reason the
9
+ * kitty keyboard flag is pushed at startup. The box grows with its text to
10
+ * `max` rows and then scrolls, keeping the cursor in view.
11
+ *
12
+ * @module
13
+ */
14
+
15
+ import { useTextSurface } from './visual.js'
16
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h, type JSX, useRef, useState } from 'react'
17
+ import type { Key } from './input.ts'
18
+ import {
19
+ size,
20
+ terminalFocused,
21
+ useKeys,
22
+ useMetric,
23
+ useScreen,
24
+ } from './screen.js'
25
+
26
+ /** The editor's whole state: the text, and where the cursor sits in it. */
27
+ export type Edit = { text: string; at: number }
28
+
29
+ let word = /\s/
30
+
31
+ let ins = (s: Edit, t: string): Edit => ({
32
+ text: s.text.slice(0, s.at) + t + s.text.slice(s.at),
33
+ at: s.at + t.length,
34
+ })
35
+ let cut = (s: Edit, from: number, to: number): Edit => ({
36
+ text: s.text.slice(0, Math.max(0, from)) + s.text.slice(to),
37
+ at: Math.max(0, from),
38
+ })
39
+ let at = (s: Edit, i: number): Edit => ({
40
+ text: s.text,
41
+ at: Math.min(Math.max(0, i), s.text.length),
42
+ })
43
+
44
+ /** The offset of the start of the line the cursor is on. */
45
+ export let bol = (t: string, i: number): number =>
46
+ t.lastIndexOf('\n', i - 1) + 1
47
+ /** The offset of the end of the line the cursor is on. */
48
+ export let eol = (t: string, i: number): number => {
49
+ let n = t.indexOf('\n', i)
50
+ return n < 0 ? t.length : n
51
+ }
52
+ let back = (t: string, i: number) => {
53
+ while (i > 0 && word.test(t[i - 1])) i--
54
+ while (i > 0 && !word.test(t[i - 1])) i--
55
+ return i
56
+ }
57
+ let fwd = (t: string, i: number) => {
58
+ while (i < t.length && word.test(t[i])) i++
59
+ while (i < t.length && !word.test(t[i])) i++
60
+ return i
61
+ }
62
+ // Up and down keep the column, the way every editor does.
63
+ let vertical = (s: Edit, up: boolean): Edit => {
64
+ let start = bol(s.text, s.at)
65
+ let col = s.at - start
66
+ if (up) {
67
+ if (!start) return at(s, 0)
68
+ let prev = bol(s.text, start - 1)
69
+ return at(s, Math.min(prev + col, start - 1))
70
+ }
71
+ let end = eol(s.text, s.at)
72
+ if (end == s.text.length) return at(s, s.text.length)
73
+ return at(s, Math.min(end + 1 + col, eol(s.text, end + 1)))
74
+ }
75
+
76
+ /** Apply one key to the editor; null when the key is not an edit. */
77
+ export let edit = (s: Edit, k: Key): Edit | null => {
78
+ let jump = k.alt || k.ctrl
79
+ if (k.name == 'char' && k.ctrl && !k.alt) {
80
+ let c = k.text
81
+ if (c == 'a') return at(s, bol(s.text, s.at))
82
+ if (c == 'e') return at(s, eol(s.text, s.at))
83
+ if (c == 'u') return cut(s, bol(s.text, s.at), s.at)
84
+ if (c == 'k') return cut(s, s.at, eol(s.text, s.at))
85
+ if (c == 'w') return cut(s, back(s.text, s.at), s.at)
86
+ if (c == 'b') return at(s, s.at - 1)
87
+ if (c == 'f') return at(s, s.at + 1)
88
+ return null
89
+ }
90
+ if (k.name == 'char' && k.alt) {
91
+ if (k.text == 'b') return at(s, back(s.text, s.at))
92
+ if (k.text == 'f') return at(s, fwd(s.text, s.at))
93
+ if (k.text == 'd') return cut(s, s.at, fwd(s.text, s.at))
94
+ return null
95
+ }
96
+ if (k.name == 'char') return ins(s, k.text ?? '')
97
+ if (k.name == 'paste') return ins(s, (k.text ?? '').replaceAll('\r\n', '\n'))
98
+ if (k.name == 'enter') return k.shift || k.alt ? ins(s, '\n') : null
99
+ if (k.name == 'backspace') {
100
+ return k.alt ? cut(s, back(s.text, s.at), s.at) : cut(s, s.at - 1, s.at)
101
+ }
102
+ if (k.name == 'delete') {
103
+ return k.alt ? cut(s, s.at, fwd(s.text, s.at)) : cut(s, s.at, s.at + 1)
104
+ }
105
+ if (k.name == 'left') return at(s, jump ? back(s.text, s.at) : s.at - 1)
106
+ if (k.name == 'right') return at(s, jump ? fwd(s.text, s.at) : s.at + 1)
107
+ if (k.name == 'up') return vertical(s, true)
108
+ if (k.name == 'down') return vertical(s, false)
109
+ if (k.name == 'home') return at(s, k.ctrl ? 0 : bol(s.text, s.at))
110
+ if (k.name == 'end') {
111
+ return at(s, k.ctrl ? s.text.length : eol(s.text, s.at))
112
+ }
113
+ return null
114
+ }
115
+
116
+ /** Visual rows retain source offsets; whitespace and submitted text are never rewritten. */
117
+ /** Vertical arrows and Home/End address displayed rows, not just hard lines. */
118
+ export let visualEdit = (s: Edit, k: Key, width: number): Edit | null => {
119
+ let rows = visualRows(s.text, width)
120
+ let { row, col } = visualSpot(rows, s.at)
121
+ let end = (i: number) =>
122
+ rows[i + 1]?.start == rows[i].end
123
+ ? Math.max(rows[i].start, rows[i].end - 1)
124
+ : rows[i].end
125
+ if (k.name == 'up' || k.name == 'down') {
126
+ let target = row + (k.name == 'up' ? -1 : 1)
127
+ if (target < 0) return at(s, 0)
128
+ if (target >= rows.length) return at(s, s.text.length)
129
+ return at(s, Math.min(rows[target].start + col, end(target)))
130
+ }
131
+ if (!k.ctrl && k.name == 'home') return at(s, rows[row].start)
132
+ if (!k.ctrl && k.name == 'end') return at(s, end(row))
133
+ return edit(s, k)
134
+ }
135
+
136
+ /** Where the cursor is, as a row and a column into the text's lines. */
137
+ export let spot = (s: Edit): { row: number; col: number } => {
138
+ let before = s.text.slice(0, s.at).split('\n')
139
+ return { row: before.length - 1, col: before[before.length - 1].length }
140
+ }
141
+
142
+ /** The editing box: `onSubmit` gets the text on Enter, and it clears. */
143
+ export let Textarea = (
144
+ {
145
+ id = 'input',
146
+ active = true,
147
+ max = 8,
148
+ prompt = '> ',
149
+ onSubmit,
150
+ onChange,
151
+ value,
152
+ onEdit,
153
+ passKey,
154
+ }: {
155
+ active?: boolean
156
+ id?: string
157
+ max?: number
158
+ prompt?: string
159
+ /** Return false to retain a rejected draft; other return values clear it. */
160
+ onSubmit?: (text: string) => unknown
161
+ onChange?: (text: string) => void
162
+ /** Controlled editor state; onEdit receives cursor-only changes too. */
163
+ value?: Edit
164
+ onEdit?: (next: Edit) => void
165
+ /** Lets the surrounding app reserve shortcuts for itself without changing
166
+ * the editor's own key bindings. */
167
+ passKey?: (key: Key) => boolean
168
+ },
169
+ ): JSX.Element => {
170
+ let [local, set] = useState<Edit>({ text: '', at: 0 })
171
+ let s = value ?? local
172
+ // One read of stdin can carry several keys, and Preact re-renders only after
173
+ // all of them are handled — so the handler edits from this ref rather than
174
+ // from the state its render closed over, or every key but the last would be
175
+ // lost.
176
+ let live = useRef(s)
177
+ live.current = s
178
+ let take = (next: Edit) => {
179
+ let was = live.current
180
+ live.current = next
181
+ if (value == null) set(next)
182
+ onEdit?.(next)
183
+ if (next.text != was.text) onChange?.(next.text)
184
+ }
185
+ let screen = useScreen(size)
186
+ let focused = useScreen(terminalFocused)
187
+ let columns = useMetric(id, 'width') || screen.columns
188
+ let hint = prompt.slice(0, Math.max(0, columns - 1))
189
+ let width = Math.max(1, columns - hint.length)
190
+ useKeys((k) => {
191
+ if (passKey?.(k)) return false
192
+ // Transcript navigation: plain End still edits the input.
193
+ if (k.ctrl && (k.name == 'end' || k.name == 'home')) return false
194
+ let now = live.current
195
+ if (k.name == 'enter' && !k.shift && !k.alt) {
196
+ const accepted = now.text ? onSubmit?.(now.text) : undefined
197
+ if (accepted !== false) take({ text: '', at: 0 })
198
+ return true
199
+ }
200
+ let next = visualEdit(now, k, width)
201
+ if (!next) return false
202
+ take(next)
203
+ return true
204
+ })
205
+ useTextSurface({ id, snapshot: () => live.current, width: () => width })
206
+ let rows = visualRows(s.text, width)
207
+ let lines = rows.map((r) => s.text.slice(r.start, r.end))
208
+ let { row, col } = visualSpot(rows, s.at)
209
+ let height = Math.max(1, Math.min(lines.length, max, screen.rows))
210
+ let top = Math.min(
211
+ Math.max(0, row - height + 1),
212
+ Math.max(0, lines.length - height),
213
+ )
214
+ let gutter = (i: number) =>
215
+ h('span', { className: 'Entry_Hint' }, i ? ' '.repeat(hint.length) : hint)
216
+ return h(
217
+ 'div',
218
+ { id, className: 'Entry', height: String(height), scroll: String(top) },
219
+ ...lines.map((line, i) =>
220
+ h(
221
+ 'div',
222
+ { key: i },
223
+ gutter(i),
224
+ active && focused && i == row
225
+ ? [
226
+ h('span', {}, line.slice(0, col)),
227
+ h('span', { className: 'Cursor' }, line[col] ?? ' '),
228
+ h('span', {}, line.slice(col + 1)),
229
+ ]
230
+ : h('span', {}, line),
231
+ )
232
+ ),
233
+ )
234
+ }