jeopi-tui 16.2.13
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.
- package/CHANGELOG.md +1861 -0
- package/README.md +705 -0
- package/dist/types/autocomplete.d.ts +99 -0
- package/dist/types/bracketed-paste.d.ts +51 -0
- package/dist/types/components/box.d.ts +31 -0
- package/dist/types/components/cancellable-loader.d.ts +21 -0
- package/dist/types/components/editor.d.ts +155 -0
- package/dist/types/components/image.d.ts +112 -0
- package/dist/types/components/input.d.ts +23 -0
- package/dist/types/components/loader.d.ts +20 -0
- package/dist/types/components/markdown.d.ts +64 -0
- package/dist/types/components/scroll-view.d.ts +62 -0
- package/dist/types/components/select-list.d.ts +68 -0
- package/dist/types/components/settings-list.d.ts +123 -0
- package/dist/types/components/spacer.d.ts +11 -0
- package/dist/types/components/tab-bar.d.ts +89 -0
- package/dist/types/components/text.d.ts +14 -0
- package/dist/types/components/truncated-text.d.ts +10 -0
- package/dist/types/deccara.d.ts +49 -0
- package/dist/types/desktop-notify.d.ts +51 -0
- package/dist/types/editor-component.d.ts +38 -0
- package/dist/types/fuzzy.d.ts +32 -0
- package/dist/types/index.d.ts +32 -0
- package/dist/types/keybindings.d.ts +191 -0
- package/dist/types/keys.d.ts +208 -0
- package/dist/types/kill-ring.d.ts +20 -0
- package/dist/types/kitty-graphics.d.ts +79 -0
- package/dist/types/latex-block.d.ts +7 -0
- package/dist/types/latex-to-unicode.d.ts +33 -0
- package/dist/types/loop-watchdog.d.ts +39 -0
- package/dist/types/mouse.d.ts +67 -0
- package/dist/types/stdin-buffer.d.ts +60 -0
- package/dist/types/symbols.d.ts +25 -0
- package/dist/types/terminal-capabilities.d.ts +284 -0
- package/dist/types/terminal.d.ts +107 -0
- package/dist/types/ttyid.d.ts +9 -0
- package/dist/types/tui.d.ts +423 -0
- package/dist/types/utils.d.ts +95 -0
- package/package.json +73 -0
- package/src/autocomplete.ts +1026 -0
- package/src/bracketed-paste.ts +123 -0
- package/src/components/box.ts +194 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +3092 -0
- package/src/components/image.ts +444 -0
- package/src/components/input.ts +474 -0
- package/src/components/loader.ts +103 -0
- package/src/components/markdown.ts +2068 -0
- package/src/components/scroll-view.ts +227 -0
- package/src/components/select-list.ts +531 -0
- package/src/components/settings-list.ts +793 -0
- package/src/components/spacer.ts +32 -0
- package/src/components/tab-bar.ts +300 -0
- package/src/components/text.ts +122 -0
- package/src/components/truncated-text.ts +69 -0
- package/src/deccara.ts +314 -0
- package/src/desktop-notify.ts +186 -0
- package/src/editor-component.ts +74 -0
- package/src/fuzzy.ts +356 -0
- package/src/index.ts +51 -0
- package/src/keybindings.ts +337 -0
- package/src/keys.ts +561 -0
- package/src/kill-ring.ts +51 -0
- package/src/kitty-graphics.ts +171 -0
- package/src/latex-block.ts +461 -0
- package/src/latex-to-unicode.ts +1994 -0
- package/src/loop-watchdog.ts +106 -0
- package/src/mouse.ts +105 -0
- package/src/stdin-buffer.ts +669 -0
- package/src/symbols.ts +26 -0
- package/src/terminal-capabilities.ts +1152 -0
- package/src/terminal.ts +1463 -0
- package/src/ttyid.ts +84 -0
- package/src/tui.ts +3901 -0
- package/src/utils.ts +570 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import { BracketedPasteHandler, decodeReencodedPasteControls } from "../bracketed-paste";
|
|
2
|
+
import { getKeybindings } from "../keybindings";
|
|
3
|
+
import { extractPrintableText } from "../keys";
|
|
4
|
+
import { KillRing } from "../kill-ring";
|
|
5
|
+
import { type Component, CURSOR_MARKER, type Focusable } from "../tui";
|
|
6
|
+
import {
|
|
7
|
+
getSegmenter,
|
|
8
|
+
getWordNavKind,
|
|
9
|
+
moveWordLeft,
|
|
10
|
+
moveWordRight,
|
|
11
|
+
padding,
|
|
12
|
+
replaceTabs,
|
|
13
|
+
sliceWithWidth,
|
|
14
|
+
visibleWidth,
|
|
15
|
+
} from "../utils";
|
|
16
|
+
|
|
17
|
+
const segmenter = getSegmenter();
|
|
18
|
+
|
|
19
|
+
interface InputState {
|
|
20
|
+
value: string;
|
|
21
|
+
cursor: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Input component - single-line text input with horizontal scrolling
|
|
26
|
+
*/
|
|
27
|
+
export class Input implements Component, Focusable {
|
|
28
|
+
#value: string = "";
|
|
29
|
+
#cursor: number = 0; // Cursor position in the value
|
|
30
|
+
#useTerminalCursor = false;
|
|
31
|
+
/** Rendered before the editable area; set to "" for chrome-less embedding. */
|
|
32
|
+
prompt = "> ";
|
|
33
|
+
onSubmit?: (value: string) => void;
|
|
34
|
+
onEscape?: () => void;
|
|
35
|
+
|
|
36
|
+
/** Focusable interface - set by TUI when focus changes */
|
|
37
|
+
focused: boolean = false;
|
|
38
|
+
|
|
39
|
+
// Bracketed paste mode buffering
|
|
40
|
+
#pasteHandler = new BracketedPasteHandler();
|
|
41
|
+
|
|
42
|
+
// Kill ring for Emacs-style kill/yank operations
|
|
43
|
+
#killRing = new KillRing();
|
|
44
|
+
#lastAction: "kill" | "yank" | "type-word" | null = null;
|
|
45
|
+
|
|
46
|
+
// Undo support
|
|
47
|
+
#undoStack: InputState[] = [];
|
|
48
|
+
|
|
49
|
+
getValue(): string {
|
|
50
|
+
return this.#value;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
setValue(value: string): void {
|
|
54
|
+
this.#value = value;
|
|
55
|
+
// Callers seed or replace the value wholesale; typing continues at the end.
|
|
56
|
+
this.#cursor = value.length;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setUseTerminalCursor(useTerminalCursor: boolean): void {
|
|
60
|
+
this.#useTerminalCursor = useTerminalCursor;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
getUseTerminalCursor(): boolean {
|
|
64
|
+
return this.#useTerminalCursor;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
handleInput(data: string): void {
|
|
68
|
+
// Handle bracketed paste mode
|
|
69
|
+
const paste = this.#pasteHandler.process(data);
|
|
70
|
+
if (paste.handled) {
|
|
71
|
+
if (paste.pasteContent !== undefined) {
|
|
72
|
+
this.#handlePaste(paste.pasteContent);
|
|
73
|
+
if (paste.remaining.length > 0) {
|
|
74
|
+
this.handleInput(paste.remaining);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const kb = getKeybindings();
|
|
81
|
+
|
|
82
|
+
// Escape/Cancel
|
|
83
|
+
if (kb.matches(data, "tui.select.cancel")) {
|
|
84
|
+
if (this.onEscape) this.onEscape();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Undo
|
|
89
|
+
if (kb.matches(data, "tui.editor.undo")) {
|
|
90
|
+
this.#undo();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Submit
|
|
95
|
+
if (kb.matches(data, "tui.input.submit") || data === "\n") {
|
|
96
|
+
if (this.onSubmit) this.onSubmit(this.#value);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Deletion
|
|
101
|
+
if (kb.matches(data, "tui.editor.deleteCharBackward")) {
|
|
102
|
+
this.#handleBackspace();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (kb.matches(data, "tui.editor.deleteCharForward")) {
|
|
107
|
+
this.#handleForwardDelete();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (kb.matches(data, "tui.editor.deleteWordBackward")) {
|
|
112
|
+
this.#deleteWordBackwards();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (kb.matches(data, "tui.editor.deleteWordForward")) {
|
|
117
|
+
this.#deleteWordForward();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (kb.matches(data, "tui.editor.deleteToLineStart")) {
|
|
122
|
+
this.#deleteToLineStart();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (kb.matches(data, "tui.editor.deleteToLineEnd")) {
|
|
127
|
+
this.#deleteToLineEnd();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Kill ring actions
|
|
132
|
+
if (kb.matches(data, "tui.editor.yank")) {
|
|
133
|
+
this.#yank();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (kb.matches(data, "tui.editor.yankPop")) {
|
|
137
|
+
this.#yankPop();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Cursor movement
|
|
142
|
+
if (kb.matches(data, "tui.editor.cursorLeft")) {
|
|
143
|
+
this.#lastAction = null;
|
|
144
|
+
if (this.#cursor > 0) {
|
|
145
|
+
const beforeCursor = this.#value.slice(0, this.#cursor);
|
|
146
|
+
const graphemes = [...segmenter.segment(beforeCursor)];
|
|
147
|
+
const lastGrapheme = graphemes[graphemes.length - 1];
|
|
148
|
+
this.#cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
|
|
149
|
+
}
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (kb.matches(data, "tui.editor.cursorRight")) {
|
|
154
|
+
this.#lastAction = null;
|
|
155
|
+
if (this.#cursor < this.#value.length) {
|
|
156
|
+
const afterCursor = this.#value.slice(this.#cursor);
|
|
157
|
+
const graphemes = [...segmenter.segment(afterCursor)];
|
|
158
|
+
const firstGrapheme = graphemes[0];
|
|
159
|
+
this.#cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (kb.matches(data, "tui.editor.cursorLineStart")) {
|
|
165
|
+
this.#lastAction = null;
|
|
166
|
+
this.#cursor = 0;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (kb.matches(data, "tui.editor.cursorLineEnd")) {
|
|
171
|
+
this.#lastAction = null;
|
|
172
|
+
this.#cursor = this.#value.length;
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (kb.matches(data, "tui.editor.cursorWordLeft")) {
|
|
177
|
+
this.#moveWordBackwards();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (kb.matches(data, "tui.editor.cursorWordRight")) {
|
|
182
|
+
this.#moveWordForwards();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Regular character input, including Kitty CSI-u text-producing sequences.
|
|
187
|
+
const printableText = extractPrintableText(data);
|
|
188
|
+
if (printableText) {
|
|
189
|
+
this.#insertCharacter(printableText);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Apply terminal paste semantics to text from non-bracketed paste transports
|
|
194
|
+
* (e.g. kitty's OSC 5522 enhanced clipboard read). Mirrors `Editor.pasteText`. */
|
|
195
|
+
pasteText(text: string): void {
|
|
196
|
+
this.#handlePaste(text);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
#insertCharacter(text: string): void {
|
|
200
|
+
const isWordChunk = [...segmenter.segment(text)].every(seg => getWordNavKind(seg.segment) !== "whitespace");
|
|
201
|
+
// Undo coalescing: consecutive word typing coalesces into one undo unit.
|
|
202
|
+
if (!isWordChunk || this.#lastAction !== "type-word") {
|
|
203
|
+
this.#pushUndo();
|
|
204
|
+
}
|
|
205
|
+
this.#lastAction = "type-word";
|
|
206
|
+
|
|
207
|
+
this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
|
|
208
|
+
this.#cursor += text.length;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#handleBackspace(): void {
|
|
212
|
+
this.#lastAction = null;
|
|
213
|
+
if (this.#cursor <= 0) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
this.#pushUndo();
|
|
218
|
+
|
|
219
|
+
const beforeCursor = this.#value.slice(0, this.#cursor);
|
|
220
|
+
const graphemes = [...segmenter.segment(beforeCursor)];
|
|
221
|
+
const lastGrapheme = graphemes[graphemes.length - 1];
|
|
222
|
+
const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
|
|
223
|
+
|
|
224
|
+
this.#value = this.#value.slice(0, this.#cursor - graphemeLength) + this.#value.slice(this.#cursor);
|
|
225
|
+
this.#cursor -= graphemeLength;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
#handleForwardDelete(): void {
|
|
229
|
+
this.#lastAction = null;
|
|
230
|
+
if (this.#cursor >= this.#value.length) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
this.#pushUndo();
|
|
235
|
+
|
|
236
|
+
const afterCursor = this.#value.slice(this.#cursor);
|
|
237
|
+
const graphemes = [...segmenter.segment(afterCursor)];
|
|
238
|
+
const firstGrapheme = graphemes[0];
|
|
239
|
+
const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
|
|
240
|
+
|
|
241
|
+
this.#value = this.#value.slice(0, this.#cursor) + this.#value.slice(this.#cursor + graphemeLength);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
#deleteToLineStart(): void {
|
|
245
|
+
if (this.#cursor === 0) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
this.#pushUndo();
|
|
250
|
+
const deletedText = this.#value.slice(0, this.#cursor);
|
|
251
|
+
this.#killRing.push(deletedText, { prepend: true, accumulate: this.#lastAction === "kill" });
|
|
252
|
+
this.#lastAction = "kill";
|
|
253
|
+
|
|
254
|
+
this.#value = this.#value.slice(this.#cursor);
|
|
255
|
+
this.#cursor = 0;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#deleteToLineEnd(): void {
|
|
259
|
+
if (this.#cursor >= this.#value.length) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
this.#pushUndo();
|
|
264
|
+
const deletedText = this.#value.slice(this.#cursor);
|
|
265
|
+
this.#killRing.push(deletedText, { prepend: false, accumulate: this.#lastAction === "kill" });
|
|
266
|
+
this.#lastAction = "kill";
|
|
267
|
+
|
|
268
|
+
this.#value = this.#value.slice(0, this.#cursor);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#deleteWordBackwards(): void {
|
|
272
|
+
if (this.#cursor === 0) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Save state before cursor movement (moveWordBackwards resets lastAction).
|
|
277
|
+
const wasKill = this.#lastAction === "kill";
|
|
278
|
+
this.#pushUndo();
|
|
279
|
+
|
|
280
|
+
const oldCursor = this.#cursor;
|
|
281
|
+
this.#moveWordBackwards();
|
|
282
|
+
const deleteFrom = this.#cursor;
|
|
283
|
+
this.#cursor = oldCursor;
|
|
284
|
+
|
|
285
|
+
const deletedText = this.#value.slice(deleteFrom, this.#cursor);
|
|
286
|
+
this.#killRing.push(deletedText, { prepend: true, accumulate: wasKill });
|
|
287
|
+
this.#lastAction = "kill";
|
|
288
|
+
|
|
289
|
+
this.#value = this.#value.slice(0, deleteFrom) + this.#value.slice(this.#cursor);
|
|
290
|
+
this.#cursor = deleteFrom;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
#deleteWordForward(): void {
|
|
294
|
+
if (this.#cursor >= this.#value.length) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Save state before cursor movement (moveWordForwards resets lastAction).
|
|
299
|
+
const wasKill = this.#lastAction === "kill";
|
|
300
|
+
this.#pushUndo();
|
|
301
|
+
|
|
302
|
+
const oldCursor = this.#cursor;
|
|
303
|
+
this.#moveWordForwards();
|
|
304
|
+
const deleteTo = this.#cursor;
|
|
305
|
+
this.#cursor = oldCursor;
|
|
306
|
+
|
|
307
|
+
const deletedText = this.#value.slice(this.#cursor, deleteTo);
|
|
308
|
+
this.#killRing.push(deletedText, { prepend: false, accumulate: wasKill });
|
|
309
|
+
this.#lastAction = "kill";
|
|
310
|
+
|
|
311
|
+
this.#value = this.#value.slice(0, this.#cursor) + this.#value.slice(deleteTo);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
#yank(): void {
|
|
315
|
+
const text = this.#killRing.peek();
|
|
316
|
+
if (!text) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
this.#pushUndo();
|
|
321
|
+
this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
|
|
322
|
+
this.#cursor += text.length;
|
|
323
|
+
this.#lastAction = "yank";
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
#yankPop(): void {
|
|
327
|
+
if (this.#lastAction !== "yank" || this.#killRing.length <= 1) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
this.#pushUndo();
|
|
332
|
+
|
|
333
|
+
const prevText = this.#killRing.peek() ?? "";
|
|
334
|
+
this.#value = this.#value.slice(0, this.#cursor - prevText.length) + this.#value.slice(this.#cursor);
|
|
335
|
+
this.#cursor -= prevText.length;
|
|
336
|
+
|
|
337
|
+
this.#killRing.rotate();
|
|
338
|
+
const text = this.#killRing.peek() ?? "";
|
|
339
|
+
this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
|
|
340
|
+
this.#cursor += text.length;
|
|
341
|
+
this.#lastAction = "yank";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
#pushUndo(): void {
|
|
345
|
+
this.#undoStack.push({ value: this.#value, cursor: this.#cursor });
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
#undo(): void {
|
|
349
|
+
const snapshot = this.#undoStack.pop();
|
|
350
|
+
if (!snapshot) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
this.#value = snapshot.value;
|
|
354
|
+
this.#cursor = snapshot.cursor;
|
|
355
|
+
this.#lastAction = null;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
#moveWordBackwards(): void {
|
|
359
|
+
if (this.#cursor === 0) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
this.#lastAction = null;
|
|
363
|
+
this.#cursor = moveWordLeft(this.#value, this.#cursor);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
#moveWordForwards(): void {
|
|
367
|
+
if (this.#cursor >= this.#value.length) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
this.#lastAction = null;
|
|
371
|
+
this.#cursor = moveWordRight(this.#value, this.#cursor);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
#handlePaste(pastedText: string): void {
|
|
375
|
+
this.#lastAction = null;
|
|
376
|
+
this.#pushUndo();
|
|
377
|
+
|
|
378
|
+
// Clean the pasted text — decode tmux's re-encoded control bytes (both
|
|
379
|
+
// extended-keys formats, e.g. Ctrl+J → "\n") back to literal bytes so the escape
|
|
380
|
+
// tail does not leak in, remove newlines/carriage returns, expand tabs, NFC-normalize,
|
|
381
|
+
// then strip any remaining control bytes. The decoder can synthesize Ctrl+A..Ctrl+Z
|
|
382
|
+
// (0x01..0x1A) from a paste, and a single-line value must hold none of them — newlines
|
|
383
|
+
// are already gone and tabs are already spaces by the time the C0/DEL strip runs.
|
|
384
|
+
//
|
|
385
|
+
// NFC normalization rationale: macOS Finder drag-drops file paths in NFD
|
|
386
|
+
// (Conjoining Jamo, U+1100..U+11FF). `Bun.stringWidth` counts each
|
|
387
|
+
// conjoining jamo as a separate cell — a Korean syllable like `화` is
|
|
388
|
+
// 1 char and 2 cells in NFC, but 2 chars and 3 cells in NFD (ᄒ=2 cells
|
|
389
|
+
// + ᅪ=1 cell). The terminal renders the NFD sequence as a single
|
|
390
|
+
// combined syllable (2 cells visible), so the width mismatch shows up
|
|
391
|
+
// as cursor drift past the visible filename — N×~1.5 cells for a path
|
|
392
|
+
// with N Korean syllables. NFC normalization at paste time stores the
|
|
393
|
+
// value in the same form everything else in the codebase assumes.
|
|
394
|
+
const cleanText = replaceTabs(
|
|
395
|
+
decodeReencodedPasteControls(pastedText).replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, ""),
|
|
396
|
+
)
|
|
397
|
+
.normalize("NFC")
|
|
398
|
+
.replace(/[\x00-\x1F\x7F]/g, "");
|
|
399
|
+
|
|
400
|
+
// Insert at cursor position
|
|
401
|
+
this.#value = this.#value.slice(0, this.#cursor) + cleanText + this.#value.slice(this.#cursor);
|
|
402
|
+
this.#cursor += cleanText.length;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
invalidate(): void {
|
|
406
|
+
// No cached state to invalidate currently
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
render(width: number): readonly string[] {
|
|
410
|
+
// Calculate visible window
|
|
411
|
+
const prompt = this.prompt;
|
|
412
|
+
const availableWidth = width - visibleWidth(prompt);
|
|
413
|
+
|
|
414
|
+
if (availableWidth <= 0) {
|
|
415
|
+
return [prompt];
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const cursorIndex = this.#cursor;
|
|
419
|
+
// Ensure we always have a grapheme to invert at the cursor (space at end).
|
|
420
|
+
const displayValue = cursorIndex >= this.#value.length ? `${this.#value} ` : this.#value;
|
|
421
|
+
|
|
422
|
+
const totalCols = visibleWidth(displayValue);
|
|
423
|
+
const cursorCols = visibleWidth(displayValue.slice(0, cursorIndex));
|
|
424
|
+
|
|
425
|
+
// Width of the grapheme at the cursor, for ensuring it fits in the viewport.
|
|
426
|
+
const cursorIter = segmenter.segment(displayValue.slice(cursorIndex))[Symbol.iterator]();
|
|
427
|
+
const cursorG = cursorIter.next().value?.segment ?? " ";
|
|
428
|
+
const cursorGWidth = visibleWidth(cursorG);
|
|
429
|
+
|
|
430
|
+
const maxStart = Math.max(0, totalCols - availableWidth);
|
|
431
|
+
let startCol = 0;
|
|
432
|
+
if (totalCols > availableWidth) {
|
|
433
|
+
const half = Math.floor(availableWidth / 2);
|
|
434
|
+
startCol = Math.max(0, Math.min(maxStart, cursorCols - half));
|
|
435
|
+
|
|
436
|
+
// Ensure the cursor grapheme is inside the viewport (and fits fully if wide).
|
|
437
|
+
const maxCursorRel = Math.max(0, availableWidth - cursorGWidth);
|
|
438
|
+
const cursorRel = cursorCols - startCol;
|
|
439
|
+
if (cursorRel > maxCursorRel) {
|
|
440
|
+
startCol = Math.max(0, Math.min(maxStart, cursorCols - maxCursorRel));
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const visibleText = sliceWithWidth(displayValue, startCol, availableWidth, true).text;
|
|
445
|
+
const prefixText = sliceWithWidth(displayValue, startCol, Math.max(0, cursorCols - startCol), true).text;
|
|
446
|
+
let cursorDisplay = prefixText.length;
|
|
447
|
+
cursorDisplay = Math.max(0, Math.min(cursorDisplay, visibleText.length));
|
|
448
|
+
|
|
449
|
+
// Build the visible line and insert the cursor marker at the buffer cursor.
|
|
450
|
+
const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];
|
|
451
|
+
const cursorGrapheme = graphemes[0];
|
|
452
|
+
|
|
453
|
+
const beforeCursor = visibleText.slice(0, cursorDisplay);
|
|
454
|
+
const atCursor = cursorGrapheme?.segment ?? "";
|
|
455
|
+
const afterCursor = visibleText.slice(cursorDisplay + atCursor.length);
|
|
456
|
+
|
|
457
|
+
// Hardware cursor marker (zero-width, emitted before the cursor cell for IME positioning)
|
|
458
|
+
const marker = this.focused ? CURSOR_MARKER : "";
|
|
459
|
+
const cursorChar = this.#useTerminalCursor ? atCursor : `\x1b[7m${atCursor || " "}\x1b[27m`;
|
|
460
|
+
|
|
461
|
+
// Clamp only the trailing text (measured in terminal cells), keeping the cursor marker intact.
|
|
462
|
+
const beforeWidth = visibleWidth(beforeCursor);
|
|
463
|
+
const cursorWidth = this.#useTerminalCursor ? visibleWidth(atCursor) : visibleWidth(atCursor || " ");
|
|
464
|
+
const remainingAfterWidth = Math.max(0, availableWidth - beforeWidth - cursorWidth);
|
|
465
|
+
const clampedAfterCursor = sliceWithWidth(afterCursor, 0, remainingAfterWidth, true).text;
|
|
466
|
+
const renderedNoMarker = beforeCursor + cursorChar + clampedAfterCursor;
|
|
467
|
+
const textWithCursor = beforeCursor + marker + cursorChar + clampedAfterCursor;
|
|
468
|
+
|
|
469
|
+
const visualLength = visibleWidth(renderedNoMarker);
|
|
470
|
+
const pad = padding(Math.max(0, availableWidth - visualLength));
|
|
471
|
+
const line = prompt + textWithCursor + pad;
|
|
472
|
+
return [line];
|
|
473
|
+
}
|
|
474
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { TUI } from "../tui";
|
|
2
|
+
import { sliceByColumn, visibleWidth } from "../utils";
|
|
3
|
+
import { Text } from "./text";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Loader component. Spinner frames advance at `SPINNER_ADVANCE_MS`.
|
|
7
|
+
*
|
|
8
|
+
* Message colorizers that are time-dependent can opt into 30fps redraws by
|
|
9
|
+
* setting `animated` to `true` on the function object.
|
|
10
|
+
*/
|
|
11
|
+
const RENDER_INTERVAL_MS = 1000 / 30;
|
|
12
|
+
const SPINNER_ADVANCE_MS = 80;
|
|
13
|
+
|
|
14
|
+
type ColorFn = (str: string) => string;
|
|
15
|
+
|
|
16
|
+
export type LoaderMessageColorFn = ColorFn & {
|
|
17
|
+
readonly animated?: true;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export class Loader extends Text {
|
|
21
|
+
#frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
22
|
+
#currentFrame = 0;
|
|
23
|
+
#intervalId?: NodeJS.Timeout;
|
|
24
|
+
#ui: TUI | null = null;
|
|
25
|
+
#lastSpinnerTick = 0;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
ui: TUI,
|
|
29
|
+
private spinnerColorFn: ColorFn,
|
|
30
|
+
private messageColorFn: LoaderMessageColorFn,
|
|
31
|
+
private message: string = "Loading...",
|
|
32
|
+
spinnerFrames?: string[],
|
|
33
|
+
) {
|
|
34
|
+
super("", 1, 0);
|
|
35
|
+
this.#ui = ui;
|
|
36
|
+
if (spinnerFrames && spinnerFrames.length > 0) {
|
|
37
|
+
this.#frames = spinnerFrames;
|
|
38
|
+
}
|
|
39
|
+
this.start();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
render(width: number): readonly string[] {
|
|
43
|
+
const lines = ["", ...super.render(width)];
|
|
44
|
+
for (let i = 0; i < lines.length; i++) {
|
|
45
|
+
const line = lines[i];
|
|
46
|
+
if (visibleWidth(line) > width) {
|
|
47
|
+
lines[i] = sliceByColumn(line, 0, width, true);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return lines;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
start() {
|
|
54
|
+
this.#lastSpinnerTick = performance.now();
|
|
55
|
+
this.#updateDisplay();
|
|
56
|
+
const intervalMs = this.messageColorFn.animated === true ? RENDER_INTERVAL_MS : SPINNER_ADVANCE_MS;
|
|
57
|
+
this.#intervalId = setInterval(() => {
|
|
58
|
+
const now = performance.now();
|
|
59
|
+
const elapsed = now - this.#lastSpinnerTick;
|
|
60
|
+
const shouldAdvanceSpinner = elapsed >= SPINNER_ADVANCE_MS;
|
|
61
|
+
if (shouldAdvanceSpinner) {
|
|
62
|
+
const steps = Math.floor(elapsed / SPINNER_ADVANCE_MS);
|
|
63
|
+
this.#currentFrame = (this.#currentFrame + steps) % this.#frames.length;
|
|
64
|
+
this.#lastSpinnerTick += steps * SPINNER_ADVANCE_MS;
|
|
65
|
+
}
|
|
66
|
+
if (shouldAdvanceSpinner || this.#ui?.synchronizedOutput === true) {
|
|
67
|
+
this.#updateDisplay();
|
|
68
|
+
}
|
|
69
|
+
}, intervalMs);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
stop() {
|
|
73
|
+
if (this.#intervalId) {
|
|
74
|
+
clearInterval(this.#intervalId);
|
|
75
|
+
this.#intervalId = undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Lifecycle teardown: stop the animation timer. Idempotent. */
|
|
80
|
+
dispose() {
|
|
81
|
+
this.stop();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setMessage(message: string) {
|
|
85
|
+
if (message === this.message) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
this.message = message;
|
|
89
|
+
this.#updateDisplay();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#updateDisplay() {
|
|
93
|
+
const frame = this.#frames[this.#currentFrame];
|
|
94
|
+
const text = `${this.spinnerColorFn(frame)} ${this.messageColorFn(this.message)}`;
|
|
95
|
+
if (this.setText(text) && this.#ui) {
|
|
96
|
+
// Component-scoped: a spinner tick changes only this component, so
|
|
97
|
+
// the TUI may reuse every other root subtree instead of re-walking
|
|
98
|
+
// the whole tree (full repaints at 12.5 Hz made huge transcripts
|
|
99
|
+
// lag as soon as the loader appeared).
|
|
100
|
+
this.#ui.requestComponentRender(this);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|