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