@f5xc-salesdemos/pi-tui 14.0.2

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.
@@ -0,0 +1,90 @@
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 cap = this.#options.maxWidthCells;
55
+ const maxWidth = cap != null && cap > 0 ? Math.min(width - 2, cap) : width - 2;
56
+
57
+ let lines: string[];
58
+
59
+ if (TERMINAL.imageProtocol) {
60
+ const result = renderImage(this.#base64Data, this.#dimensions, {
61
+ maxWidthCells: maxWidth,
62
+ maxHeightCells: this.#options.maxHeightCells,
63
+ });
64
+
65
+ if (result) {
66
+ // Return `rows` lines so TUI accounts for image height
67
+ // First (rows-1) lines are empty (TUI clears them)
68
+ // Last line: move cursor back up, then output image sequence
69
+ lines = [];
70
+ for (let i = 0; i < result.rows - 1; i++) {
71
+ lines.push("");
72
+ }
73
+ // Move cursor up to first row, then output image
74
+ const moveUp = result.rows > 1 ? `\x1b[${result.rows - 1}A` : "";
75
+ lines.push(moveUp + result.sequence);
76
+ } else {
77
+ const fallback = imageFallback(this.#mimeType, this.#dimensions, this.#options.filename);
78
+ lines = [this.#theme.fallbackColor(fallback)];
79
+ }
80
+ } else {
81
+ const fallback = imageFallback(this.#mimeType, this.#dimensions, this.#options.filename);
82
+ lines = [this.#theme.fallbackColor(fallback)];
83
+ }
84
+
85
+ this.#cachedLines = lines;
86
+ this.#cachedWidth = width;
87
+
88
+ return lines;
89
+ }
90
+ }
@@ -0,0 +1,439 @@
1
+ import { BracketedPasteHandler } 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
+ onSubmit?: (value: string) => void;
31
+ onEscape?: () => void;
32
+
33
+ /** Focusable interface - set by TUI when focus changes */
34
+ focused: boolean = false;
35
+
36
+ // Bracketed paste mode buffering
37
+ #pasteHandler = new BracketedPasteHandler();
38
+
39
+ // Kill ring for Emacs-style kill/yank operations
40
+ #killRing = new KillRing();
41
+ #lastAction: "kill" | "yank" | "type-word" | null = null;
42
+
43
+ // Undo support
44
+ #undoStack: InputState[] = [];
45
+
46
+ getValue(): string {
47
+ return this.#value;
48
+ }
49
+
50
+ setValue(value: string): void {
51
+ this.#value = value;
52
+ this.#cursor = Math.min(this.#cursor, value.length);
53
+ }
54
+
55
+ handleInput(data: string): void {
56
+ // Handle bracketed paste mode
57
+ const paste = this.#pasteHandler.process(data);
58
+ if (paste.handled) {
59
+ if (paste.pasteContent !== undefined) {
60
+ this.#handlePaste(paste.pasteContent);
61
+ if (paste.remaining.length > 0) {
62
+ this.handleInput(paste.remaining);
63
+ }
64
+ }
65
+ return;
66
+ }
67
+
68
+ const kb = getKeybindings();
69
+
70
+ // Escape/Cancel
71
+ if (kb.matches(data, "tui.select.cancel")) {
72
+ if (this.onEscape) this.onEscape();
73
+ return;
74
+ }
75
+
76
+ // Undo
77
+ if (kb.matches(data, "tui.editor.undo")) {
78
+ this.#undo();
79
+ return;
80
+ }
81
+
82
+ // Submit
83
+ if (kb.matches(data, "tui.input.submit") || data === "\n") {
84
+ if (this.onSubmit) this.onSubmit(this.#value);
85
+ return;
86
+ }
87
+
88
+ // Deletion
89
+ if (kb.matches(data, "tui.editor.deleteCharBackward")) {
90
+ this.#handleBackspace();
91
+ return;
92
+ }
93
+
94
+ if (kb.matches(data, "tui.editor.deleteCharForward")) {
95
+ this.#handleForwardDelete();
96
+ return;
97
+ }
98
+
99
+ if (kb.matches(data, "tui.editor.deleteWordBackward")) {
100
+ this.#deleteWordBackwards();
101
+ return;
102
+ }
103
+
104
+ if (kb.matches(data, "tui.editor.deleteWordForward")) {
105
+ this.#deleteWordForward();
106
+ return;
107
+ }
108
+
109
+ if (kb.matches(data, "tui.editor.deleteToLineStart")) {
110
+ this.#deleteToLineStart();
111
+ return;
112
+ }
113
+
114
+ if (kb.matches(data, "tui.editor.deleteToLineEnd")) {
115
+ this.#deleteToLineEnd();
116
+ return;
117
+ }
118
+
119
+ // Kill ring actions
120
+ if (kb.matches(data, "tui.editor.yank")) {
121
+ this.#yank();
122
+ return;
123
+ }
124
+ if (kb.matches(data, "tui.editor.yankPop")) {
125
+ this.#yankPop();
126
+ return;
127
+ }
128
+
129
+ // Cursor movement
130
+ if (kb.matches(data, "tui.editor.cursorLeft")) {
131
+ this.#lastAction = null;
132
+ if (this.#cursor > 0) {
133
+ const beforeCursor = this.#value.slice(0, this.#cursor);
134
+ const graphemes = [...segmenter.segment(beforeCursor)];
135
+ const lastGrapheme = graphemes[graphemes.length - 1];
136
+ this.#cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
137
+ }
138
+ return;
139
+ }
140
+
141
+ if (kb.matches(data, "tui.editor.cursorRight")) {
142
+ this.#lastAction = null;
143
+ if (this.#cursor < this.#value.length) {
144
+ const afterCursor = this.#value.slice(this.#cursor);
145
+ const graphemes = [...segmenter.segment(afterCursor)];
146
+ const firstGrapheme = graphemes[0];
147
+ this.#cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
148
+ }
149
+ return;
150
+ }
151
+
152
+ if (kb.matches(data, "tui.editor.cursorLineStart")) {
153
+ this.#lastAction = null;
154
+ this.#cursor = 0;
155
+ return;
156
+ }
157
+
158
+ if (kb.matches(data, "tui.editor.cursorLineEnd")) {
159
+ this.#lastAction = null;
160
+ this.#cursor = this.#value.length;
161
+ return;
162
+ }
163
+
164
+ if (kb.matches(data, "tui.editor.cursorWordLeft")) {
165
+ this.#moveWordBackwards();
166
+ return;
167
+ }
168
+
169
+ if (kb.matches(data, "tui.editor.cursorWordRight")) {
170
+ this.#moveWordForwards();
171
+ return;
172
+ }
173
+
174
+ // Regular character input, including Kitty CSI-u text-producing sequences.
175
+ const printableText = extractPrintableText(data);
176
+ if (printableText) {
177
+ this.#insertCharacter(printableText);
178
+ }
179
+ }
180
+
181
+ #insertCharacter(text: string): void {
182
+ const isWordChunk = [...segmenter.segment(text)].every(seg => getWordNavKind(seg.segment) !== "whitespace");
183
+ // Undo coalescing: consecutive word typing coalesces into one undo unit.
184
+ if (!isWordChunk || this.#lastAction !== "type-word") {
185
+ this.#pushUndo();
186
+ }
187
+ this.#lastAction = "type-word";
188
+
189
+ this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
190
+ this.#cursor += text.length;
191
+ }
192
+
193
+ #handleBackspace(): void {
194
+ this.#lastAction = null;
195
+ if (this.#cursor <= 0) {
196
+ return;
197
+ }
198
+
199
+ this.#pushUndo();
200
+
201
+ const beforeCursor = this.#value.slice(0, this.#cursor);
202
+ const graphemes = [...segmenter.segment(beforeCursor)];
203
+ const lastGrapheme = graphemes[graphemes.length - 1];
204
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
205
+
206
+ this.#value = this.#value.slice(0, this.#cursor - graphemeLength) + this.#value.slice(this.#cursor);
207
+ this.#cursor -= graphemeLength;
208
+ }
209
+
210
+ #handleForwardDelete(): void {
211
+ this.#lastAction = null;
212
+ if (this.#cursor >= this.#value.length) {
213
+ return;
214
+ }
215
+
216
+ this.#pushUndo();
217
+
218
+ const afterCursor = this.#value.slice(this.#cursor);
219
+ const graphemes = [...segmenter.segment(afterCursor)];
220
+ const firstGrapheme = graphemes[0];
221
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
222
+
223
+ this.#value = this.#value.slice(0, this.#cursor) + this.#value.slice(this.#cursor + graphemeLength);
224
+ }
225
+
226
+ #deleteToLineStart(): void {
227
+ if (this.#cursor === 0) {
228
+ return;
229
+ }
230
+
231
+ this.#pushUndo();
232
+ const deletedText = this.#value.slice(0, this.#cursor);
233
+ this.#killRing.push(deletedText, { prepend: true, accumulate: this.#lastAction === "kill" });
234
+ this.#lastAction = "kill";
235
+
236
+ this.#value = this.#value.slice(this.#cursor);
237
+ this.#cursor = 0;
238
+ }
239
+
240
+ #deleteToLineEnd(): void {
241
+ if (this.#cursor >= this.#value.length) {
242
+ return;
243
+ }
244
+
245
+ this.#pushUndo();
246
+ const deletedText = this.#value.slice(this.#cursor);
247
+ this.#killRing.push(deletedText, { prepend: false, accumulate: this.#lastAction === "kill" });
248
+ this.#lastAction = "kill";
249
+
250
+ this.#value = this.#value.slice(0, this.#cursor);
251
+ }
252
+
253
+ #deleteWordBackwards(): void {
254
+ if (this.#cursor === 0) {
255
+ return;
256
+ }
257
+
258
+ // Save state before cursor movement (moveWordBackwards resets lastAction).
259
+ const wasKill = this.#lastAction === "kill";
260
+ this.#pushUndo();
261
+
262
+ const oldCursor = this.#cursor;
263
+ this.#moveWordBackwards();
264
+ const deleteFrom = this.#cursor;
265
+ this.#cursor = oldCursor;
266
+
267
+ const deletedText = this.#value.slice(deleteFrom, this.#cursor);
268
+ this.#killRing.push(deletedText, { prepend: true, accumulate: wasKill });
269
+ this.#lastAction = "kill";
270
+
271
+ this.#value = this.#value.slice(0, deleteFrom) + this.#value.slice(this.#cursor);
272
+ this.#cursor = deleteFrom;
273
+ }
274
+
275
+ #deleteWordForward(): void {
276
+ if (this.#cursor >= this.#value.length) {
277
+ return;
278
+ }
279
+
280
+ // Save state before cursor movement (moveWordForwards resets lastAction).
281
+ const wasKill = this.#lastAction === "kill";
282
+ this.#pushUndo();
283
+
284
+ const oldCursor = this.#cursor;
285
+ this.#moveWordForwards();
286
+ const deleteTo = this.#cursor;
287
+ this.#cursor = oldCursor;
288
+
289
+ const deletedText = this.#value.slice(this.#cursor, deleteTo);
290
+ this.#killRing.push(deletedText, { prepend: false, accumulate: wasKill });
291
+ this.#lastAction = "kill";
292
+
293
+ this.#value = this.#value.slice(0, this.#cursor) + this.#value.slice(deleteTo);
294
+ }
295
+
296
+ #yank(): void {
297
+ const text = this.#killRing.peek();
298
+ if (!text) {
299
+ return;
300
+ }
301
+
302
+ this.#pushUndo();
303
+ this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
304
+ this.#cursor += text.length;
305
+ this.#lastAction = "yank";
306
+ }
307
+
308
+ #yankPop(): void {
309
+ if (this.#lastAction !== "yank" || this.#killRing.length <= 1) {
310
+ return;
311
+ }
312
+
313
+ this.#pushUndo();
314
+
315
+ const prevText = this.#killRing.peek() ?? "";
316
+ this.#value = this.#value.slice(0, this.#cursor - prevText.length) + this.#value.slice(this.#cursor);
317
+ this.#cursor -= prevText.length;
318
+
319
+ this.#killRing.rotate();
320
+ const text = this.#killRing.peek() ?? "";
321
+ this.#value = this.#value.slice(0, this.#cursor) + text + this.#value.slice(this.#cursor);
322
+ this.#cursor += text.length;
323
+ this.#lastAction = "yank";
324
+ }
325
+
326
+ #pushUndo(): void {
327
+ this.#undoStack.push({ value: this.#value, cursor: this.#cursor });
328
+ }
329
+
330
+ #undo(): void {
331
+ const snapshot = this.#undoStack.pop();
332
+ if (!snapshot) {
333
+ return;
334
+ }
335
+ this.#value = snapshot.value;
336
+ this.#cursor = snapshot.cursor;
337
+ this.#lastAction = null;
338
+ }
339
+
340
+ #moveWordBackwards(): void {
341
+ if (this.#cursor === 0) {
342
+ return;
343
+ }
344
+ this.#lastAction = null;
345
+ this.#cursor = moveWordLeft(this.#value, this.#cursor);
346
+ }
347
+
348
+ #moveWordForwards(): void {
349
+ if (this.#cursor >= this.#value.length) {
350
+ return;
351
+ }
352
+ this.#lastAction = null;
353
+ this.#cursor = moveWordRight(this.#value, this.#cursor);
354
+ }
355
+
356
+ #handlePaste(pastedText: string): void {
357
+ this.#lastAction = null;
358
+ this.#pushUndo();
359
+
360
+ // Clean the pasted text - remove newlines and carriage returns, then normalize tabs.
361
+ const cleanText = replaceTabs(pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, ""));
362
+
363
+ // Insert at cursor position
364
+ this.#value = this.#value.slice(0, this.#cursor) + cleanText + this.#value.slice(this.#cursor);
365
+ this.#cursor += cleanText.length;
366
+ }
367
+
368
+ invalidate(): void {
369
+ // No cached state to invalidate currently
370
+ }
371
+
372
+ render(width: number): string[] {
373
+ // Calculate visible window
374
+ const prompt = "> ";
375
+ const availableWidth = width - prompt.length;
376
+
377
+ if (availableWidth <= 0) {
378
+ return [prompt];
379
+ }
380
+
381
+ const cursorIndex = this.#cursor;
382
+ // Ensure we always have a grapheme to invert at the cursor (space at end).
383
+ const displayValue = cursorIndex >= this.#value.length ? `${this.#value} ` : this.#value;
384
+
385
+ const totalCols = visibleWidth(displayValue);
386
+ const cursorCols = visibleWidth(displayValue.slice(0, cursorIndex));
387
+
388
+ // Width of the grapheme at the cursor, for ensuring it fits in the viewport.
389
+ const cursorIter = segmenter.segment(displayValue.slice(cursorIndex))[Symbol.iterator]();
390
+ const cursorG = cursorIter.next().value?.segment ?? " ";
391
+ const cursorGWidth = visibleWidth(cursorG);
392
+
393
+ const maxStart = Math.max(0, totalCols - availableWidth);
394
+ let startCol = 0;
395
+ if (totalCols > availableWidth) {
396
+ const half = Math.floor(availableWidth / 2);
397
+ startCol = Math.max(0, Math.min(maxStart, cursorCols - half));
398
+
399
+ // Ensure the cursor grapheme is inside the viewport (and fits fully if wide).
400
+ const maxCursorRel = Math.max(0, availableWidth - cursorGWidth);
401
+ const cursorRel = cursorCols - startCol;
402
+ if (cursorRel > maxCursorRel) {
403
+ startCol = Math.max(0, Math.min(maxStart, cursorCols - maxCursorRel));
404
+ }
405
+ }
406
+
407
+ const visibleText = sliceWithWidth(displayValue, startCol, availableWidth, true).text;
408
+ const prefixText = sliceWithWidth(displayValue, startCol, Math.max(0, cursorCols - startCol), true).text;
409
+ let cursorDisplay = prefixText.length;
410
+ cursorDisplay = Math.max(0, Math.min(cursorDisplay, visibleText.length));
411
+
412
+ // Build line with fake cursor
413
+ // Insert cursor character at cursor position
414
+ const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];
415
+ const cursorGrapheme = graphemes[0];
416
+
417
+ const beforeCursor = visibleText.slice(0, cursorDisplay);
418
+ const atCursor = cursorGrapheme?.segment ?? " ";
419
+ const afterCursor = visibleText.slice(cursorDisplay + atCursor.length);
420
+
421
+ // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
422
+ const marker = this.focused ? CURSOR_MARKER : "";
423
+ // Use inverse video to show cursor
424
+ const cursorChar = `\x1b[7m${atCursor}\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal
425
+
426
+ // Clamp only the trailing text (measured in terminal cells), keeping the cursor marker intact.
427
+ const beforeWidth = visibleWidth(beforeCursor);
428
+ const cursorWidth = visibleWidth(atCursor);
429
+ const remainingAfterWidth = Math.max(0, availableWidth - beforeWidth - cursorWidth);
430
+ const clampedAfterCursor = sliceWithWidth(afterCursor, 0, remainingAfterWidth, true).text;
431
+ const renderedNoMarker = beforeCursor + cursorChar + clampedAfterCursor;
432
+ const textWithCursor = beforeCursor + marker + cursorChar + clampedAfterCursor;
433
+
434
+ const visualLength = visibleWidth(renderedNoMarker);
435
+ const pad = padding(Math.max(0, availableWidth - visualLength));
436
+ const line = prompt + textWithCursor + pad;
437
+ return [line];
438
+ }
439
+ }
@@ -0,0 +1,67 @@
1
+ import type { TUI } from "../tui";
2
+ import { sliceByColumn, visibleWidth } from "../utils";
3
+ import { Text } from "./text";
4
+
5
+ /**
6
+ * Loader component that updates every 80ms with spinning animation
7
+ */
8
+ export class Loader extends Text {
9
+ #frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
10
+ #currentFrame = 0;
11
+ #intervalId?: NodeJS.Timeout;
12
+ #ui: TUI | null = null;
13
+
14
+ constructor(
15
+ ui: TUI,
16
+ private spinnerColorFn: (str: string) => string,
17
+ private messageColorFn: (str: string) => string,
18
+ private message: string = "Loading...",
19
+ spinnerFrames?: string[],
20
+ ) {
21
+ super("", 1, 0);
22
+ this.#ui = ui;
23
+ if (spinnerFrames && spinnerFrames.length > 0) {
24
+ this.#frames = spinnerFrames;
25
+ }
26
+ this.start();
27
+ }
28
+
29
+ render(width: number): string[] {
30
+ const lines = ["", ...super.render(width)];
31
+ for (let i = 0; i < lines.length; i++) {
32
+ const line = lines[i];
33
+ if (visibleWidth(line) > width) {
34
+ lines[i] = sliceByColumn(line, 0, width, true);
35
+ }
36
+ }
37
+ return lines;
38
+ }
39
+
40
+ start() {
41
+ this.#updateDisplay();
42
+ this.#intervalId = setInterval(() => {
43
+ this.#currentFrame = (this.#currentFrame + 1) % this.#frames.length;
44
+ this.#updateDisplay();
45
+ }, 80);
46
+ }
47
+
48
+ stop() {
49
+ if (this.#intervalId) {
50
+ clearInterval(this.#intervalId);
51
+ this.#intervalId = undefined;
52
+ }
53
+ }
54
+
55
+ setMessage(message: string) {
56
+ this.message = message;
57
+ this.#updateDisplay();
58
+ }
59
+
60
+ #updateDisplay() {
61
+ const frame = this.#frames[this.#currentFrame];
62
+ this.setText(`${this.spinnerColorFn(frame)} ${this.messageColorFn(this.message)}`);
63
+ if (this.#ui) {
64
+ this.#ui.requestRender();
65
+ }
66
+ }
67
+ }