@lenylvt/pi-tui 0.62.5
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 +767 -0
- package/dist/autocomplete.d.ts +50 -0
- package/dist/autocomplete.d.ts.map +1 -0
- package/dist/autocomplete.js +623 -0
- package/dist/autocomplete.js.map +1 -0
- package/dist/components/box.d.ts +22 -0
- package/dist/components/box.d.ts.map +1 -0
- package/dist/components/box.js +104 -0
- package/dist/components/box.js.map +1 -0
- package/dist/components/cancellable-loader.d.ts +22 -0
- package/dist/components/cancellable-loader.d.ts.map +1 -0
- package/dist/components/cancellable-loader.js +35 -0
- package/dist/components/cancellable-loader.js.map +1 -0
- package/dist/components/editor.d.ts +244 -0
- package/dist/components/editor.d.ts.map +1 -0
- package/dist/components/editor.js +1861 -0
- package/dist/components/editor.js.map +1 -0
- package/dist/components/image.d.ts +28 -0
- package/dist/components/image.d.ts.map +1 -0
- package/dist/components/image.js +69 -0
- package/dist/components/image.js.map +1 -0
- package/dist/components/input.d.ts +37 -0
- package/dist/components/input.d.ts.map +1 -0
- package/dist/components/input.js +426 -0
- package/dist/components/input.js.map +1 -0
- package/dist/components/loader.d.ts +21 -0
- package/dist/components/loader.d.ts.map +1 -0
- package/dist/components/loader.js +49 -0
- package/dist/components/loader.js.map +1 -0
- package/dist/components/markdown.d.ts +95 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +660 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/select-list.d.ts +50 -0
- package/dist/components/select-list.d.ts.map +1 -0
- package/dist/components/select-list.js +159 -0
- package/dist/components/select-list.js.map +1 -0
- package/dist/components/settings-list.d.ts +50 -0
- package/dist/components/settings-list.d.ts.map +1 -0
- package/dist/components/settings-list.js +185 -0
- package/dist/components/settings-list.js.map +1 -0
- package/dist/components/spacer.d.ts +12 -0
- package/dist/components/spacer.d.ts.map +1 -0
- package/dist/components/spacer.js +23 -0
- package/dist/components/spacer.js.map +1 -0
- package/dist/components/text.d.ts +19 -0
- package/dist/components/text.d.ts.map +1 -0
- package/dist/components/text.js +89 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/truncated-text.d.ts +13 -0
- package/dist/components/truncated-text.d.ts.map +1 -0
- package/dist/components/truncated-text.js +51 -0
- package/dist/components/truncated-text.js.map +1 -0
- package/dist/editor-component.d.ts +39 -0
- package/dist/editor-component.d.ts.map +1 -0
- package/dist/editor-component.js +2 -0
- package/dist/editor-component.js.map +1 -0
- package/dist/fuzzy.d.ts +16 -0
- package/dist/fuzzy.d.ts.map +1 -0
- package/dist/fuzzy.js +107 -0
- package/dist/fuzzy.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/keybindings.d.ts +193 -0
- package/dist/keybindings.d.ts.map +1 -0
- package/dist/keybindings.js +174 -0
- package/dist/keybindings.js.map +1 -0
- package/dist/keys.d.ts +170 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +1124 -0
- package/dist/keys.js.map +1 -0
- package/dist/kill-ring.d.ts +28 -0
- package/dist/kill-ring.d.ts.map +1 -0
- package/dist/kill-ring.js +44 -0
- package/dist/kill-ring.js.map +1 -0
- package/dist/stdin-buffer.d.ts +48 -0
- package/dist/stdin-buffer.d.ts.map +1 -0
- package/dist/stdin-buffer.js +317 -0
- package/dist/stdin-buffer.js.map +1 -0
- package/dist/terminal-image.d.ts +68 -0
- package/dist/terminal-image.d.ts.map +1 -0
- package/dist/terminal-image.js +288 -0
- package/dist/terminal-image.js.map +1 -0
- package/dist/terminal.d.ts +84 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +285 -0
- package/dist/terminal.js.map +1 -0
- package/dist/tui.d.ts +218 -0
- package/dist/tui.d.ts.map +1 -0
- package/dist/tui.js +966 -0
- package/dist/tui.js.map +1 -0
- package/dist/undo-stack.d.ts +17 -0
- package/dist/undo-stack.d.ts.map +1 -0
- package/dist/undo-stack.js +25 -0
- package/dist/undo-stack.js.map +1 -0
- package/dist/utils.d.ts +78 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +960 -0
- package/dist/utils.js.map +1 -0
- package/package.json +55 -0
- package/src/autocomplete.ts +771 -0
- package/src/components/box.ts +137 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +2230 -0
- package/src/components/image.ts +104 -0
- package/src/components/input.ts +503 -0
- package/src/components/loader.ts +55 -0
- package/src/components/markdown.ts +820 -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 +133 -0
- package/src/index.ts +104 -0
- package/src/keybindings.ts +244 -0
- package/src/keys.ts +1356 -0
- package/src/kill-ring.ts +46 -0
- package/src/stdin-buffer.ts +386 -0
- package/src/terminal-image.ts +381 -0
- package/src/terminal.ts +360 -0
- package/src/tui.ts +1200 -0
- package/src/undo-stack.ts +28 -0
- package/src/utils.ts +1068 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getCapabilities,
|
|
3
|
+
getImageDimensions,
|
|
4
|
+
type ImageDimensions,
|
|
5
|
+
imageFallback,
|
|
6
|
+
renderImage,
|
|
7
|
+
} from "../terminal-image.js";
|
|
8
|
+
import type { Component } from "../tui.js";
|
|
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
|
+
/** Kitty image ID. If provided, reuses this ID (for animations/updates). */
|
|
19
|
+
imageId?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class Image implements Component {
|
|
23
|
+
private base64Data: string;
|
|
24
|
+
private mimeType: string;
|
|
25
|
+
private dimensions: ImageDimensions;
|
|
26
|
+
private theme: ImageTheme;
|
|
27
|
+
private options: ImageOptions;
|
|
28
|
+
private imageId?: number;
|
|
29
|
+
|
|
30
|
+
private cachedLines?: string[];
|
|
31
|
+
private cachedWidth?: number;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
base64Data: string,
|
|
35
|
+
mimeType: string,
|
|
36
|
+
theme: ImageTheme,
|
|
37
|
+
options: ImageOptions = {},
|
|
38
|
+
dimensions?: ImageDimensions,
|
|
39
|
+
) {
|
|
40
|
+
this.base64Data = base64Data;
|
|
41
|
+
this.mimeType = mimeType;
|
|
42
|
+
this.theme = theme;
|
|
43
|
+
this.options = options;
|
|
44
|
+
this.dimensions = dimensions || getImageDimensions(base64Data, mimeType) || { widthPx: 800, heightPx: 600 };
|
|
45
|
+
this.imageId = options.imageId;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Get the Kitty image ID used by this image (if any). */
|
|
49
|
+
getImageId(): number | undefined {
|
|
50
|
+
return this.imageId;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
invalidate(): void {
|
|
54
|
+
this.cachedLines = undefined;
|
|
55
|
+
this.cachedWidth = undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
render(width: number): string[] {
|
|
59
|
+
if (this.cachedLines && this.cachedWidth === width) {
|
|
60
|
+
return this.cachedLines;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const maxWidth = Math.min(width - 2, this.options.maxWidthCells ?? 60);
|
|
64
|
+
|
|
65
|
+
const caps = getCapabilities();
|
|
66
|
+
let lines: string[];
|
|
67
|
+
|
|
68
|
+
if (caps.images) {
|
|
69
|
+
const result = renderImage(this.base64Data, this.dimensions, {
|
|
70
|
+
maxWidthCells: maxWidth,
|
|
71
|
+
imageId: this.imageId,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (result) {
|
|
75
|
+
// Store the image ID for later cleanup
|
|
76
|
+
if (result.imageId) {
|
|
77
|
+
this.imageId = result.imageId;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Return `rows` lines so TUI accounts for image height
|
|
81
|
+
// First (rows-1) lines are empty (TUI clears them)
|
|
82
|
+
// Last line: move cursor back up, then output image sequence
|
|
83
|
+
lines = [];
|
|
84
|
+
for (let i = 0; i < result.rows - 1; i++) {
|
|
85
|
+
lines.push("");
|
|
86
|
+
}
|
|
87
|
+
// Move cursor up to first row, then output image
|
|
88
|
+
const moveUp = result.rows > 1 ? `\x1b[${result.rows - 1}A` : "";
|
|
89
|
+
lines.push(moveUp + result.sequence);
|
|
90
|
+
} else {
|
|
91
|
+
const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
|
|
92
|
+
lines = [this.theme.fallbackColor(fallback)];
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
|
|
96
|
+
lines = [this.theme.fallbackColor(fallback)];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.cachedLines = lines;
|
|
100
|
+
this.cachedWidth = width;
|
|
101
|
+
|
|
102
|
+
return lines;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -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,55 @@
|
|
|
1
|
+
import type { TUI } from "../tui.js";
|
|
2
|
+
import { Text } from "./text.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Loader component that updates every 80ms with spinning animation
|
|
6
|
+
*/
|
|
7
|
+
export class Loader extends Text {
|
|
8
|
+
private frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
9
|
+
private currentFrame = 0;
|
|
10
|
+
private intervalId: NodeJS.Timeout | null = null;
|
|
11
|
+
private 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
|
+
) {
|
|
19
|
+
super("", 1, 0);
|
|
20
|
+
this.ui = ui;
|
|
21
|
+
this.start();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
render(width: number): string[] {
|
|
25
|
+
return ["", ...super.render(width)];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
start() {
|
|
29
|
+
this.updateDisplay();
|
|
30
|
+
this.intervalId = setInterval(() => {
|
|
31
|
+
this.currentFrame = (this.currentFrame + 1) % this.frames.length;
|
|
32
|
+
this.updateDisplay();
|
|
33
|
+
}, 80);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
stop() {
|
|
37
|
+
if (this.intervalId) {
|
|
38
|
+
clearInterval(this.intervalId);
|
|
39
|
+
this.intervalId = null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setMessage(message: string) {
|
|
44
|
+
this.message = message;
|
|
45
|
+
this.updateDisplay();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private updateDisplay() {
|
|
49
|
+
const frame = this.frames[this.currentFrame];
|
|
50
|
+
this.setText(`${this.spinnerColorFn(frame)} ${this.messageColorFn(this.message)}`);
|
|
51
|
+
if (this.ui) {
|
|
52
|
+
this.ui.requestRender();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|