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