@nghyane/arcane-tui 0.1.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 +3 -0
- package/README.md +704 -0
- package/package.json +72 -0
- package/src/autocomplete.ts +772 -0
- package/src/buffer/ansi-parser.ts +349 -0
- package/src/buffer/buffer.ts +120 -0
- package/src/buffer/cell.ts +103 -0
- package/src/buffer/index.ts +16 -0
- package/src/buffer/render.ts +149 -0
- package/src/components/box.ts +144 -0
- package/src/components/cancellable-loader.ts +39 -0
- package/src/components/editor.ts +2289 -0
- package/src/components/image.ts +86 -0
- package/src/components/input.ts +531 -0
- package/src/components/loader.ts +59 -0
- package/src/components/markdown.ts +858 -0
- package/src/components/select-list.ts +198 -0
- package/src/components/settings-list.ts +194 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +142 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +61 -0
- package/src/editor-component.ts +71 -0
- package/src/fuzzy.ts +143 -0
- package/src/index.ts +69 -0
- package/src/keybindings.ts +197 -0
- package/src/keys.ts +270 -0
- package/src/kill-ring.ts +46 -0
- package/src/mermaid.ts +140 -0
- package/src/stdin-buffer.ts +385 -0
- package/src/symbols.ts +24 -0
- package/src/terminal-capabilities.ts +393 -0
- package/src/terminal.ts +467 -0
- package/src/ttyid.ts +66 -0
- package/src/tui.ts +1134 -0
- package/src/utils.ts +149 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getImageDimensions,
|
|
3
|
+
type ImageDimensions,
|
|
4
|
+
imageFallback,
|
|
5
|
+
renderImage,
|
|
6
|
+
TERMINAL,
|
|
7
|
+
} from "../terminal-capabilities";
|
|
8
|
+
import type { Component } from "../tui";
|
|
9
|
+
|
|
10
|
+
export interface ImageTheme {
|
|
11
|
+
fallbackColor: (str: string) => string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ImageOptions {
|
|
15
|
+
maxWidthCells?: number;
|
|
16
|
+
maxHeightCells?: number;
|
|
17
|
+
filename?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class Image implements Component {
|
|
21
|
+
#base64Data: string;
|
|
22
|
+
#mimeType: string;
|
|
23
|
+
#dimensions: ImageDimensions;
|
|
24
|
+
#theme: ImageTheme;
|
|
25
|
+
#options: ImageOptions;
|
|
26
|
+
|
|
27
|
+
#cachedLines?: string[];
|
|
28
|
+
#cachedWidth?: number;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
base64Data: string,
|
|
32
|
+
mimeType: string,
|
|
33
|
+
theme: ImageTheme,
|
|
34
|
+
options: ImageOptions = {},
|
|
35
|
+
dimensions?: ImageDimensions,
|
|
36
|
+
) {
|
|
37
|
+
this.#base64Data = base64Data;
|
|
38
|
+
this.#mimeType = mimeType;
|
|
39
|
+
this.#theme = theme;
|
|
40
|
+
this.#options = options;
|
|
41
|
+
this.#dimensions = dimensions || getImageDimensions(base64Data, mimeType) || { widthPx: 800, heightPx: 600 };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
invalidate(): void {
|
|
45
|
+
this.#cachedLines = undefined;
|
|
46
|
+
this.#cachedWidth = undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
render(width: number): string[] {
|
|
50
|
+
if (this.#cachedLines && this.#cachedWidth === width) {
|
|
51
|
+
return this.#cachedLines;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const maxWidth = Math.min(width - 2, this.#options.maxWidthCells ?? 60);
|
|
55
|
+
|
|
56
|
+
let lines: string[];
|
|
57
|
+
|
|
58
|
+
if (TERMINAL.imageProtocol) {
|
|
59
|
+
const result = renderImage(this.#base64Data, this.#dimensions, { maxWidthCells: maxWidth });
|
|
60
|
+
|
|
61
|
+
if (result) {
|
|
62
|
+
// Return `rows` lines so TUI accounts for image height
|
|
63
|
+
// First (rows-1) lines are empty (TUI clears them)
|
|
64
|
+
// Last line: move cursor back up, then output image sequence
|
|
65
|
+
lines = [];
|
|
66
|
+
for (let i = 0; i < result.rows - 1; i++) {
|
|
67
|
+
lines.push("");
|
|
68
|
+
}
|
|
69
|
+
// Move cursor up to first row, then output image
|
|
70
|
+
const moveUp = result.rows > 1 ? `\x1b[${result.rows - 1}A` : "";
|
|
71
|
+
lines.push(moveUp + result.sequence);
|
|
72
|
+
} else {
|
|
73
|
+
const fallback = imageFallback(this.#mimeType, this.#dimensions, this.#options.filename);
|
|
74
|
+
lines = [this.#theme.fallbackColor(fallback)];
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
const fallback = imageFallback(this.#mimeType, this.#dimensions, this.#options.filename);
|
|
78
|
+
lines = [this.#theme.fallbackColor(fallback)];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.#cachedLines = lines;
|
|
82
|
+
this.#cachedWidth = width;
|
|
83
|
+
|
|
84
|
+
return lines;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { getEditorKeybindings } from "../keybindings";
|
|
2
|
+
import { KillRing } from "../kill-ring";
|
|
3
|
+
import { type Component, CURSOR_MARKER, type Focusable } from "../tui";
|
|
4
|
+
import { getSegmenter, isPunctuationChar, isWhitespaceChar, padding, visibleWidth } from "../utils";
|
|
5
|
+
|
|
6
|
+
const segmenter = getSegmenter();
|
|
7
|
+
|
|
8
|
+
interface InputState {
|
|
9
|
+
value: string;
|
|
10
|
+
cursor: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Input component - single-line text input with horizontal scrolling
|
|
15
|
+
*/
|
|
16
|
+
export class Input implements Component, Focusable {
|
|
17
|
+
#value: string = "";
|
|
18
|
+
#cursor: number = 0; // Cursor position in the value
|
|
19
|
+
onSubmit?: (value: string) => void;
|
|
20
|
+
onEscape?: () => void;
|
|
21
|
+
|
|
22
|
+
/** Focusable interface - set by TUI when focus changes */
|
|
23
|
+
focused: boolean = false;
|
|
24
|
+
|
|
25
|
+
// Bracketed paste mode buffering
|
|
26
|
+
#pasteBuffer: string = "";
|
|
27
|
+
#isInPaste: boolean = false;
|
|
28
|
+
|
|
29
|
+
// Kill ring for Emacs-style kill/yank operations
|
|
30
|
+
#killRing = new KillRing();
|
|
31
|
+
#lastAction: "kill" | "yank" | "type-word" | null = null;
|
|
32
|
+
|
|
33
|
+
// Undo support
|
|
34
|
+
#undoStack: InputState[] = [];
|
|
35
|
+
|
|
36
|
+
getValue(): string {
|
|
37
|
+
return this.#value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
setValue(value: string): void {
|
|
41
|
+
this.#value = value;
|
|
42
|
+
this.#cursor = Math.min(this.#cursor, value.length);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
handleInput(data: string): void {
|
|
46
|
+
// Handle bracketed paste mode
|
|
47
|
+
// Start of paste: \x1b[200~
|
|
48
|
+
// End of paste: \x1b[201~
|
|
49
|
+
|
|
50
|
+
// Check if we're starting a bracketed paste
|
|
51
|
+
if (data.includes("\x1b[200~")) {
|
|
52
|
+
this.#isInPaste = true;
|
|
53
|
+
this.#pasteBuffer = "";
|
|
54
|
+
data = data.replace("\x1b[200~", "");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// If we're in a paste, buffer the data
|
|
58
|
+
if (this.#isInPaste) {
|
|
59
|
+
// Check if this chunk contains the end marker
|
|
60
|
+
this.#pasteBuffer += data;
|
|
61
|
+
|
|
62
|
+
const endIndex = this.#pasteBuffer.indexOf("\x1b[201~");
|
|
63
|
+
if (endIndex !== -1) {
|
|
64
|
+
// Extract the pasted content
|
|
65
|
+
const pasteContent = this.#pasteBuffer.substring(0, endIndex);
|
|
66
|
+
|
|
67
|
+
// Process the complete paste
|
|
68
|
+
this.#handlePaste(pasteContent);
|
|
69
|
+
|
|
70
|
+
// Reset paste state
|
|
71
|
+
this.#isInPaste = false;
|
|
72
|
+
|
|
73
|
+
// Handle any remaining input after the paste marker
|
|
74
|
+
const remaining = this.#pasteBuffer.substring(endIndex + 6); // 6 = length of \x1b[201~
|
|
75
|
+
this.#pasteBuffer = "";
|
|
76
|
+
if (remaining) {
|
|
77
|
+
this.handleInput(remaining);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const kb = getEditorKeybindings();
|
|
84
|
+
|
|
85
|
+
// Escape/Cancel
|
|
86
|
+
if (kb.matches(data, "selectCancel")) {
|
|
87
|
+
if (this.onEscape) this.onEscape();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Undo
|
|
92
|
+
if (kb.matches(data, "undo")) {
|
|
93
|
+
this.#undo();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Submit
|
|
98
|
+
if (kb.matches(data, "submit") || data === "\n") {
|
|
99
|
+
if (this.onSubmit) this.onSubmit(this.#value);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Deletion
|
|
104
|
+
if (kb.matches(data, "deleteCharBackward")) {
|
|
105
|
+
this.#handleBackspace();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (kb.matches(data, "deleteCharForward")) {
|
|
110
|
+
this.#handleForwardDelete();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (kb.matches(data, "deleteWordBackward")) {
|
|
115
|
+
this.#deleteWordBackwards();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (kb.matches(data, "deleteWordForward")) {
|
|
120
|
+
this.#deleteWordForward();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (kb.matches(data, "deleteToLineStart")) {
|
|
125
|
+
this.#deleteToLineStart();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (kb.matches(data, "deleteToLineEnd")) {
|
|
130
|
+
this.#deleteToLineEnd();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Kill ring actions
|
|
135
|
+
if (kb.matches(data, "yank")) {
|
|
136
|
+
this.#yank();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (kb.matches(data, "yankPop")) {
|
|
140
|
+
this.#yankPop();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Cursor movement
|
|
145
|
+
if (kb.matches(data, "cursorLeft")) {
|
|
146
|
+
this.#lastAction = null;
|
|
147
|
+
if (this.#cursor > 0) {
|
|
148
|
+
const beforeCursor = this.#value.slice(0, this.#cursor);
|
|
149
|
+
const graphemes = [...segmenter.segment(beforeCursor)];
|
|
150
|
+
const lastGrapheme = graphemes[graphemes.length - 1];
|
|
151
|
+
this.#cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (kb.matches(data, "cursorRight")) {
|
|
157
|
+
this.#lastAction = null;
|
|
158
|
+
if (this.#cursor < this.#value.length) {
|
|
159
|
+
const afterCursor = this.#value.slice(this.#cursor);
|
|
160
|
+
const graphemes = [...segmenter.segment(afterCursor)];
|
|
161
|
+
const firstGrapheme = graphemes[0];
|
|
162
|
+
this.#cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
|
|
163
|
+
}
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (kb.matches(data, "cursorLineStart")) {
|
|
168
|
+
this.#lastAction = null;
|
|
169
|
+
this.#cursor = 0;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (kb.matches(data, "cursorLineEnd")) {
|
|
174
|
+
this.#lastAction = null;
|
|
175
|
+
this.#cursor = this.#value.length;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (kb.matches(data, "cursorWordLeft")) {
|
|
180
|
+
this.#moveWordBackwards();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (kb.matches(data, "cursorWordRight")) {
|
|
185
|
+
this.#moveWordForwards();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Regular character input - accept printable characters including Unicode,
|
|
190
|
+
// but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F)
|
|
191
|
+
const hasControlChars = [...data].some(ch => {
|
|
192
|
+
const code = ch.charCodeAt(0);
|
|
193
|
+
return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
|
|
194
|
+
});
|
|
195
|
+
if (!hasControlChars) {
|
|
196
|
+
this.#insertCharacter(data);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#insertCharacter(text: string): void {
|
|
201
|
+
const isWordChunk = [...text].every(ch => !isWhitespaceChar(ch));
|
|
202
|
+
// Undo coalescing: consecutive word typing coalesces into one undo unit.
|
|
203
|
+
if (!isWordChunk || this.#lastAction !== "type-word") {
|
|
204
|
+
this.#pushUndo();
|
|
205
|
+
}
|
|
206
|
+
this.#lastAction = "type-word";
|
|
207
|
+
|
|
208
|
+
this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
|
|
209
|
+
this.#cursor += text.length;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
#handleBackspace(): void {
|
|
213
|
+
this.#lastAction = null;
|
|
214
|
+
if (this.#cursor <= 0) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.#pushUndo();
|
|
219
|
+
|
|
220
|
+
const beforeCursor = this.#value.slice(0, this.#cursor);
|
|
221
|
+
const graphemes = [...segmenter.segment(beforeCursor)];
|
|
222
|
+
const lastGrapheme = graphemes[graphemes.length - 1];
|
|
223
|
+
const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
|
|
224
|
+
|
|
225
|
+
this.#value = this.#value.slice(0, this.#cursor - graphemeLength) + this.#value.slice(this.#cursor);
|
|
226
|
+
this.#cursor -= graphemeLength;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#handleForwardDelete(): void {
|
|
230
|
+
this.#lastAction = null;
|
|
231
|
+
if (this.#cursor >= this.#value.length) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
this.#pushUndo();
|
|
236
|
+
|
|
237
|
+
const afterCursor = this.#value.slice(this.#cursor);
|
|
238
|
+
const graphemes = [...segmenter.segment(afterCursor)];
|
|
239
|
+
const firstGrapheme = graphemes[0];
|
|
240
|
+
const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
|
|
241
|
+
|
|
242
|
+
this.#value = this.#value.slice(0, this.#cursor) + this.#value.slice(this.#cursor + graphemeLength);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#deleteToLineStart(): void {
|
|
246
|
+
if (this.#cursor === 0) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
this.#pushUndo();
|
|
251
|
+
const deletedText = this.#value.slice(0, this.#cursor);
|
|
252
|
+
this.#killRing.push(deletedText, { prepend: true, accumulate: this.#lastAction === "kill" });
|
|
253
|
+
this.#lastAction = "kill";
|
|
254
|
+
|
|
255
|
+
this.#value = this.#value.slice(this.#cursor);
|
|
256
|
+
this.#cursor = 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
#deleteToLineEnd(): void {
|
|
260
|
+
if (this.#cursor >= this.#value.length) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
this.#pushUndo();
|
|
265
|
+
const deletedText = this.#value.slice(this.#cursor);
|
|
266
|
+
this.#killRing.push(deletedText, { prepend: false, accumulate: this.#lastAction === "kill" });
|
|
267
|
+
this.#lastAction = "kill";
|
|
268
|
+
|
|
269
|
+
this.#value = this.#value.slice(0, this.#cursor);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#deleteWordBackwards(): void {
|
|
273
|
+
if (this.#cursor === 0) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Save state before cursor movement (moveWordBackwards resets lastAction).
|
|
278
|
+
const wasKill = this.#lastAction === "kill";
|
|
279
|
+
this.#pushUndo();
|
|
280
|
+
|
|
281
|
+
const oldCursor = this.#cursor;
|
|
282
|
+
this.#moveWordBackwards();
|
|
283
|
+
const deleteFrom = this.#cursor;
|
|
284
|
+
this.#cursor = oldCursor;
|
|
285
|
+
|
|
286
|
+
const deletedText = this.#value.slice(deleteFrom, this.#cursor);
|
|
287
|
+
this.#killRing.push(deletedText, { prepend: true, accumulate: wasKill });
|
|
288
|
+
this.#lastAction = "kill";
|
|
289
|
+
|
|
290
|
+
this.#value = this.#value.slice(0, deleteFrom) + this.#value.slice(this.#cursor);
|
|
291
|
+
this.#cursor = deleteFrom;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#deleteWordForward(): void {
|
|
295
|
+
if (this.#cursor >= this.#value.length) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Save state before cursor movement (moveWordForwards resets lastAction).
|
|
300
|
+
const wasKill = this.#lastAction === "kill";
|
|
301
|
+
this.#pushUndo();
|
|
302
|
+
|
|
303
|
+
const oldCursor = this.#cursor;
|
|
304
|
+
this.#moveWordForwards();
|
|
305
|
+
const deleteTo = this.#cursor;
|
|
306
|
+
this.#cursor = oldCursor;
|
|
307
|
+
|
|
308
|
+
const deletedText = this.#value.slice(this.#cursor, deleteTo);
|
|
309
|
+
this.#killRing.push(deletedText, { prepend: false, accumulate: wasKill });
|
|
310
|
+
this.#lastAction = "kill";
|
|
311
|
+
|
|
312
|
+
this.#value = this.#value.slice(0, this.#cursor) + this.#value.slice(deleteTo);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#yank(): void {
|
|
316
|
+
const text = this.#killRing.peek();
|
|
317
|
+
if (!text) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
this.#pushUndo();
|
|
322
|
+
this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
|
|
323
|
+
this.#cursor += text.length;
|
|
324
|
+
this.#lastAction = "yank";
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
#yankPop(): void {
|
|
328
|
+
if (this.#lastAction !== "yank" || this.#killRing.length <= 1) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
this.#pushUndo();
|
|
333
|
+
|
|
334
|
+
const prevText = this.#killRing.peek() ?? "";
|
|
335
|
+
this.#value = this.#value.slice(0, this.#cursor - prevText.length) + this.#value.slice(this.#cursor);
|
|
336
|
+
this.#cursor -= prevText.length;
|
|
337
|
+
|
|
338
|
+
this.#killRing.rotate();
|
|
339
|
+
const text = this.#killRing.peek() ?? "";
|
|
340
|
+
this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
|
|
341
|
+
this.#cursor += text.length;
|
|
342
|
+
this.#lastAction = "yank";
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#pushUndo(): void {
|
|
346
|
+
this.#undoStack.push({ value: this.#value, cursor: this.#cursor });
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
#undo(): void {
|
|
350
|
+
const snapshot = this.#undoStack.pop();
|
|
351
|
+
if (!snapshot) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
this.#value = snapshot.value;
|
|
355
|
+
this.#cursor = snapshot.cursor;
|
|
356
|
+
this.#lastAction = null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
#moveWordBackwards(): void {
|
|
360
|
+
if (this.#cursor === 0) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
this.#lastAction = null;
|
|
364
|
+
|
|
365
|
+
const textBeforeCursor = this.#value.slice(0, this.#cursor);
|
|
366
|
+
const graphemes = [...segmenter.segment(textBeforeCursor)];
|
|
367
|
+
|
|
368
|
+
// Skip trailing whitespace
|
|
369
|
+
while (graphemes.length > 0 && isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "")) {
|
|
370
|
+
this.#cursor -= graphemes.pop()?.segment.length || 0;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (graphemes.length > 0) {
|
|
374
|
+
const lastGrapheme = graphemes[graphemes.length - 1]?.segment || "";
|
|
375
|
+
if (isPunctuationChar(lastGrapheme)) {
|
|
376
|
+
// Skip punctuation run
|
|
377
|
+
while (graphemes.length > 0 && isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")) {
|
|
378
|
+
this.#cursor -= graphemes.pop()?.segment.length || 0;
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
// Skip word run
|
|
382
|
+
while (
|
|
383
|
+
graphemes.length > 0 &&
|
|
384
|
+
!isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "") &&
|
|
385
|
+
!isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")
|
|
386
|
+
) {
|
|
387
|
+
this.#cursor -= graphemes.pop()?.segment.length || 0;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
#moveWordForwards(): void {
|
|
394
|
+
if (this.#cursor >= this.#value.length) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
this.#lastAction = null;
|
|
399
|
+
const textAfterCursor = this.#value.slice(this.#cursor);
|
|
400
|
+
const segments = segmenter.segment(textAfterCursor);
|
|
401
|
+
const iterator = segments[Symbol.iterator]();
|
|
402
|
+
let next = iterator.next();
|
|
403
|
+
|
|
404
|
+
// Skip leading whitespace
|
|
405
|
+
while (!next.done && isWhitespaceChar(next.value.segment)) {
|
|
406
|
+
this.#cursor += next.value.segment.length;
|
|
407
|
+
next = iterator.next();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (!next.done) {
|
|
411
|
+
const firstGrapheme = next.value.segment;
|
|
412
|
+
if (isPunctuationChar(firstGrapheme)) {
|
|
413
|
+
// Skip punctuation run
|
|
414
|
+
while (!next.done && isPunctuationChar(next.value.segment)) {
|
|
415
|
+
this.#cursor += next.value.segment.length;
|
|
416
|
+
next = iterator.next();
|
|
417
|
+
}
|
|
418
|
+
} else {
|
|
419
|
+
// Skip word run
|
|
420
|
+
while (!next.done && !isWhitespaceChar(next.value.segment) && !isPunctuationChar(next.value.segment)) {
|
|
421
|
+
this.#cursor += next.value.segment.length;
|
|
422
|
+
next = iterator.next();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
#handlePaste(pastedText: string): void {
|
|
429
|
+
this.#lastAction = null;
|
|
430
|
+
this.#pushUndo();
|
|
431
|
+
|
|
432
|
+
// Clean the pasted text - remove newlines and carriage returns
|
|
433
|
+
const cleanText = pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, "");
|
|
434
|
+
|
|
435
|
+
// Insert at cursor position
|
|
436
|
+
this.#value = this.#value.slice(0, this.#cursor) + cleanText + this.#value.slice(this.#cursor);
|
|
437
|
+
this.#cursor += cleanText.length;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
invalidate(): void {
|
|
441
|
+
// No cached state to invalidate currently
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
render(width: number): string[] {
|
|
445
|
+
// Calculate visible window
|
|
446
|
+
const prompt = "> ";
|
|
447
|
+
const availableWidth = width - prompt.length;
|
|
448
|
+
|
|
449
|
+
if (availableWidth <= 0) {
|
|
450
|
+
return [prompt];
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
let visibleText = "";
|
|
454
|
+
let cursorDisplay = this.#cursor;
|
|
455
|
+
|
|
456
|
+
if (this.#value.length < availableWidth) {
|
|
457
|
+
// Everything fits (leave room for cursor at end)
|
|
458
|
+
visibleText = this.#value;
|
|
459
|
+
} else {
|
|
460
|
+
// Need horizontal scrolling
|
|
461
|
+
// Reserve one character for cursor if it's at the end
|
|
462
|
+
const scrollWidth = this.#cursor === this.#value.length ? availableWidth - 1 : availableWidth;
|
|
463
|
+
const halfWidth = Math.floor(scrollWidth / 2);
|
|
464
|
+
|
|
465
|
+
const findValidStart = (start: number) => {
|
|
466
|
+
while (start < this.#value.length) {
|
|
467
|
+
const charCode = this.#value.charCodeAt(start);
|
|
468
|
+
// this is low surrogate, not a valid start
|
|
469
|
+
if (charCode >= 0xdc00 && charCode < 0xe000) {
|
|
470
|
+
start++;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
return start;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
const findValidEnd = (end: number) => {
|
|
479
|
+
while (end > 0) {
|
|
480
|
+
const charCode = this.#value.charCodeAt(end - 1);
|
|
481
|
+
// this is high surrogate, might be split.
|
|
482
|
+
if (charCode >= 0xd800 && charCode < 0xdc00) {
|
|
483
|
+
end--;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
return end;
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
if (this.#cursor < halfWidth) {
|
|
492
|
+
// Cursor near start
|
|
493
|
+
visibleText = this.#value.slice(0, findValidEnd(scrollWidth));
|
|
494
|
+
cursorDisplay = this.#cursor;
|
|
495
|
+
} else if (this.#cursor > this.#value.length - halfWidth) {
|
|
496
|
+
// Cursor near end
|
|
497
|
+
const start = findValidStart(this.#value.length - scrollWidth);
|
|
498
|
+
visibleText = this.#value.slice(start);
|
|
499
|
+
cursorDisplay = this.#cursor - start;
|
|
500
|
+
} else {
|
|
501
|
+
// Cursor in middle
|
|
502
|
+
const start = findValidStart(this.#cursor - halfWidth);
|
|
503
|
+
visibleText = this.#value.slice(start, findValidEnd(start + scrollWidth));
|
|
504
|
+
cursorDisplay = this.#cursor - start;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Build line with fake cursor
|
|
509
|
+
// Insert cursor character at cursor position
|
|
510
|
+
const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];
|
|
511
|
+
const cursorGrapheme = graphemes[0];
|
|
512
|
+
|
|
513
|
+
const beforeCursor = visibleText.slice(0, cursorDisplay);
|
|
514
|
+
const atCursor = cursorGrapheme?.segment ?? " ";
|
|
515
|
+
const afterCursor = visibleText.slice(cursorDisplay + atCursor.length);
|
|
516
|
+
|
|
517
|
+
// Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
|
|
518
|
+
const marker = this.focused ? CURSOR_MARKER : "";
|
|
519
|
+
|
|
520
|
+
// Use inverse video to show cursor
|
|
521
|
+
const cursorChar = `\x1b[7m${atCursor}\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal
|
|
522
|
+
const textWithCursor = beforeCursor + marker + cursorChar + afterCursor;
|
|
523
|
+
|
|
524
|
+
// Calculate visual width
|
|
525
|
+
const visualLength = visibleWidth(textWithCursor);
|
|
526
|
+
const pad = padding(Math.max(0, availableWidth - visualLength));
|
|
527
|
+
const line = prompt + textWithCursor + pad;
|
|
528
|
+
|
|
529
|
+
return [line];
|
|
530
|
+
}
|
|
531
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { TUI } from "../tui";
|
|
2
|
+
import { Text } from "./text";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Loader component that updates every 80ms with spinning animation
|
|
6
|
+
*/
|
|
7
|
+
export class Loader extends Text {
|
|
8
|
+
#frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
9
|
+
#currentFrame = 0;
|
|
10
|
+
#intervalId?: NodeJS.Timeout;
|
|
11
|
+
#ui: TUI | null = null;
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
ui: TUI,
|
|
15
|
+
private spinnerColorFn: (str: string) => string,
|
|
16
|
+
private messageColorFn: (str: string) => string,
|
|
17
|
+
private message: string = "Loading...",
|
|
18
|
+
spinnerFrames?: string[],
|
|
19
|
+
) {
|
|
20
|
+
super("", 1, 0);
|
|
21
|
+
this.#ui = ui;
|
|
22
|
+
if (spinnerFrames && spinnerFrames.length > 0) {
|
|
23
|
+
this.#frames = spinnerFrames;
|
|
24
|
+
}
|
|
25
|
+
this.start();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
render(width: number): string[] {
|
|
29
|
+
return ["", ...super.render(width)];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
start() {
|
|
33
|
+
this.#updateDisplay();
|
|
34
|
+
this.#intervalId = setInterval(() => {
|
|
35
|
+
this.#currentFrame = (this.#currentFrame + 1) % this.#frames.length;
|
|
36
|
+
this.#updateDisplay();
|
|
37
|
+
}, 80);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
stop() {
|
|
41
|
+
if (this.#intervalId) {
|
|
42
|
+
clearInterval(this.#intervalId);
|
|
43
|
+
this.#intervalId = undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setMessage(message: string) {
|
|
48
|
+
this.message = message;
|
|
49
|
+
this.#updateDisplay();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#updateDisplay() {
|
|
53
|
+
const frame = this.#frames[this.#currentFrame];
|
|
54
|
+
this.setText(`${this.spinnerColorFn(frame)} ${this.messageColorFn(this.message)}`);
|
|
55
|
+
if (this.#ui) {
|
|
56
|
+
this.#ui.requestRender();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|