@oh-my-pi/pi-tui 1.337.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.
@@ -0,0 +1,87 @@
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
+ }
19
+
20
+ export class Image implements Component {
21
+ private base64Data: string;
22
+ private mimeType: string;
23
+ private dimensions: ImageDimensions;
24
+ private theme: ImageTheme;
25
+ private options: ImageOptions;
26
+
27
+ private cachedLines?: string[];
28
+ private 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
+ const caps = getCapabilities();
57
+ let lines: string[];
58
+
59
+ if (caps.images) {
60
+ const result = renderImage(this.base64Data, this.dimensions, { maxWidthCells: maxWidth });
61
+
62
+ if (result) {
63
+ // Return `rows` lines so TUI accounts for image height
64
+ // First (rows-1) lines are empty (TUI clears them)
65
+ // Last line: move cursor back up, then output image sequence
66
+ lines = [];
67
+ for (let i = 0; i < result.rows - 1; i++) {
68
+ lines.push("");
69
+ }
70
+ // Move cursor up to first row, then output image
71
+ const moveUp = result.rows > 1 ? `\x1b[${result.rows - 1}A` : "";
72
+ lines.push(moveUp + result.sequence);
73
+ } else {
74
+ const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
75
+ lines = [this.theme.fallbackColor(fallback)];
76
+ }
77
+ } else {
78
+ const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
79
+ lines = [this.theme.fallbackColor(fallback)];
80
+ }
81
+
82
+ this.cachedLines = lines;
83
+ this.cachedWidth = width;
84
+
85
+ return lines;
86
+ }
87
+ }
@@ -0,0 +1,344 @@
1
+ import {
2
+ isAltBackspace,
3
+ isAltLeft,
4
+ isAltRight,
5
+ isArrowLeft,
6
+ isArrowRight,
7
+ isBackspace,
8
+ isCtrlA,
9
+ isCtrlE,
10
+ isCtrlK,
11
+ isCtrlLeft,
12
+ isCtrlRight,
13
+ isCtrlU,
14
+ isCtrlW,
15
+ isDelete,
16
+ isEnter,
17
+ } from "../keys.js";
18
+ import type { Component } from "../tui.js";
19
+ import { getSegmenter, isPunctuationChar, isWhitespaceChar, visibleWidth } from "../utils.js";
20
+
21
+ const segmenter = getSegmenter();
22
+
23
+ /**
24
+ * Input component - single-line text input with horizontal scrolling
25
+ */
26
+ export class Input implements Component {
27
+ private value: string = "";
28
+ private cursor: number = 0; // Cursor position in the value
29
+ public onSubmit?: (value: string) => void;
30
+
31
+ // Bracketed paste mode buffering
32
+ private pasteBuffer: string = "";
33
+ private isInPaste: boolean = false;
34
+
35
+ getValue(): string {
36
+ return this.value;
37
+ }
38
+
39
+ setValue(value: string): void {
40
+ this.value = value;
41
+ this.cursor = Math.min(this.cursor, value.length);
42
+ }
43
+
44
+ handleInput(data: string): void {
45
+ // Handle bracketed paste mode
46
+ // Start of paste: \x1b[200~
47
+ // End of paste: \x1b[201~
48
+
49
+ // Check if we're starting a bracketed paste
50
+ if (data.includes("\x1b[200~")) {
51
+ this.isInPaste = true;
52
+ this.pasteBuffer = "";
53
+ data = data.replace("\x1b[200~", "");
54
+ }
55
+
56
+ // If we're in a paste, buffer the data
57
+ if (this.isInPaste) {
58
+ // Check if this chunk contains the end marker
59
+ this.pasteBuffer += data;
60
+
61
+ const endIndex = this.pasteBuffer.indexOf("\x1b[201~");
62
+ if (endIndex !== -1) {
63
+ // Extract the pasted content
64
+ const pasteContent = this.pasteBuffer.substring(0, endIndex);
65
+
66
+ // Process the complete paste
67
+ this.handlePaste(pasteContent);
68
+
69
+ // Reset paste state
70
+ this.isInPaste = false;
71
+
72
+ // Handle any remaining input after the paste marker
73
+ const remaining = this.pasteBuffer.substring(endIndex + 6); // 6 = length of \x1b[201~
74
+ this.pasteBuffer = "";
75
+ if (remaining) {
76
+ this.handleInput(remaining);
77
+ }
78
+ }
79
+ return;
80
+ }
81
+ // Handle special keys
82
+ if (isEnter(data) || data === "\n") {
83
+ // Enter - submit
84
+ if (this.onSubmit) {
85
+ this.onSubmit(this.value);
86
+ }
87
+ return;
88
+ }
89
+
90
+ if (isBackspace(data)) {
91
+ // Backspace - delete grapheme before cursor (handles emojis, etc.)
92
+ if (this.cursor > 0) {
93
+ const beforeCursor = this.value.slice(0, this.cursor);
94
+ const graphemes = [...segmenter.segment(beforeCursor)];
95
+ const lastGrapheme = graphemes[graphemes.length - 1];
96
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
97
+ this.value = this.value.slice(0, this.cursor - graphemeLength) + this.value.slice(this.cursor);
98
+ this.cursor -= graphemeLength;
99
+ }
100
+ return;
101
+ }
102
+
103
+ if (isArrowLeft(data)) {
104
+ // Left arrow - move by one grapheme (handles emojis, etc.)
105
+ if (this.cursor > 0) {
106
+ const beforeCursor = this.value.slice(0, this.cursor);
107
+ const graphemes = [...segmenter.segment(beforeCursor)];
108
+ const lastGrapheme = graphemes[graphemes.length - 1];
109
+ this.cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
110
+ }
111
+ return;
112
+ }
113
+
114
+ if (isArrowRight(data)) {
115
+ // Right arrow - move by one grapheme (handles emojis, etc.)
116
+ if (this.cursor < this.value.length) {
117
+ const afterCursor = this.value.slice(this.cursor);
118
+ const graphemes = [...segmenter.segment(afterCursor)];
119
+ const firstGrapheme = graphemes[0];
120
+ this.cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
121
+ }
122
+ return;
123
+ }
124
+
125
+ if (isDelete(data)) {
126
+ // Delete - delete grapheme at cursor (handles emojis, etc.)
127
+ if (this.cursor < this.value.length) {
128
+ const afterCursor = this.value.slice(this.cursor);
129
+ const graphemes = [...segmenter.segment(afterCursor)];
130
+ const firstGrapheme = graphemes[0];
131
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
132
+ this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + graphemeLength);
133
+ }
134
+ return;
135
+ }
136
+
137
+ if (isCtrlA(data)) {
138
+ // Ctrl+A - beginning of line
139
+ this.cursor = 0;
140
+ return;
141
+ }
142
+
143
+ if (isCtrlE(data)) {
144
+ // Ctrl+E - end of line
145
+ this.cursor = this.value.length;
146
+ return;
147
+ }
148
+
149
+ if (isCtrlW(data)) {
150
+ // Ctrl+W - delete word backwards
151
+ this.deleteWordBackwards();
152
+ return;
153
+ }
154
+
155
+ if (isAltBackspace(data)) {
156
+ // Option/Alt+Backspace - delete word backwards
157
+ this.deleteWordBackwards();
158
+ return;
159
+ }
160
+
161
+ if (isCtrlU(data)) {
162
+ // Ctrl+U - delete from cursor to start of line
163
+ this.value = this.value.slice(this.cursor);
164
+ this.cursor = 0;
165
+ return;
166
+ }
167
+
168
+ if (isCtrlK(data)) {
169
+ // Ctrl+K - delete from cursor to end of line
170
+ this.value = this.value.slice(0, this.cursor);
171
+ return;
172
+ }
173
+
174
+ if (isCtrlLeft(data) || isAltLeft(data)) {
175
+ this.moveWordBackwards();
176
+ return;
177
+ }
178
+
179
+ if (isCtrlRight(data) || isAltRight(data)) {
180
+ this.moveWordForwards();
181
+ return;
182
+ }
183
+
184
+ // Regular character input - accept printable characters including Unicode,
185
+ // but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F)
186
+ const hasControlChars = [...data].some((ch) => {
187
+ const code = ch.charCodeAt(0);
188
+ return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
189
+ });
190
+ if (!hasControlChars) {
191
+ this.value = this.value.slice(0, this.cursor) + data + this.value.slice(this.cursor);
192
+ this.cursor += data.length;
193
+ }
194
+ }
195
+
196
+ private deleteWordBackwards(): void {
197
+ if (this.cursor === 0) {
198
+ return;
199
+ }
200
+
201
+ const oldCursor = this.cursor;
202
+ this.moveWordBackwards();
203
+ const deleteFrom = this.cursor;
204
+ this.cursor = oldCursor;
205
+
206
+ this.value = this.value.slice(0, deleteFrom) + this.value.slice(this.cursor);
207
+ this.cursor = deleteFrom;
208
+ }
209
+
210
+ private moveWordBackwards(): void {
211
+ if (this.cursor === 0) {
212
+ return;
213
+ }
214
+
215
+ const textBeforeCursor = this.value.slice(0, this.cursor);
216
+ const graphemes = [...segmenter.segment(textBeforeCursor)];
217
+
218
+ // Skip trailing whitespace
219
+ while (graphemes.length > 0 && isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "")) {
220
+ this.cursor -= graphemes.pop()?.segment.length || 0;
221
+ }
222
+
223
+ if (graphemes.length > 0) {
224
+ const lastGrapheme = graphemes[graphemes.length - 1]?.segment || "";
225
+ if (isPunctuationChar(lastGrapheme)) {
226
+ // Skip punctuation run
227
+ while (graphemes.length > 0 && isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")) {
228
+ this.cursor -= graphemes.pop()?.segment.length || 0;
229
+ }
230
+ } else {
231
+ // Skip word run
232
+ while (
233
+ graphemes.length > 0 &&
234
+ !isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "") &&
235
+ !isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")
236
+ ) {
237
+ this.cursor -= graphemes.pop()?.segment.length || 0;
238
+ }
239
+ }
240
+ }
241
+ }
242
+
243
+ private moveWordForwards(): void {
244
+ if (this.cursor >= this.value.length) {
245
+ return;
246
+ }
247
+
248
+ const textAfterCursor = this.value.slice(this.cursor);
249
+ const segments = segmenter.segment(textAfterCursor);
250
+ const iterator = segments[Symbol.iterator]();
251
+ let next = iterator.next();
252
+
253
+ // Skip leading whitespace
254
+ while (!next.done && isWhitespaceChar(next.value.segment)) {
255
+ this.cursor += next.value.segment.length;
256
+ next = iterator.next();
257
+ }
258
+
259
+ if (!next.done) {
260
+ const firstGrapheme = next.value.segment;
261
+ if (isPunctuationChar(firstGrapheme)) {
262
+ // Skip punctuation run
263
+ while (!next.done && isPunctuationChar(next.value.segment)) {
264
+ this.cursor += next.value.segment.length;
265
+ next = iterator.next();
266
+ }
267
+ } else {
268
+ // Skip word run
269
+ while (!next.done && !isWhitespaceChar(next.value.segment) && !isPunctuationChar(next.value.segment)) {
270
+ this.cursor += next.value.segment.length;
271
+ next = iterator.next();
272
+ }
273
+ }
274
+ }
275
+ }
276
+
277
+ private handlePaste(pastedText: string): void {
278
+ // Clean the pasted text - remove newlines and carriage returns
279
+ const cleanText = pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, "");
280
+
281
+ // Insert at cursor position
282
+ this.value = this.value.slice(0, this.cursor) + cleanText + this.value.slice(this.cursor);
283
+ this.cursor += cleanText.length;
284
+ }
285
+
286
+ invalidate(): void {
287
+ // No cached state to invalidate currently
288
+ }
289
+
290
+ render(width: number): string[] {
291
+ // Calculate visible window
292
+ const prompt = "> ";
293
+ const availableWidth = width - prompt.length;
294
+
295
+ if (availableWidth <= 0) {
296
+ return [prompt];
297
+ }
298
+
299
+ let visibleText = "";
300
+ let cursorDisplay = this.cursor;
301
+
302
+ if (this.value.length < availableWidth) {
303
+ // Everything fits (leave room for cursor at end)
304
+ visibleText = this.value;
305
+ } else {
306
+ // Need horizontal scrolling
307
+ // Reserve one character for cursor if it's at the end
308
+ const scrollWidth = this.cursor === this.value.length ? availableWidth - 1 : availableWidth;
309
+ const halfWidth = Math.floor(scrollWidth / 2);
310
+
311
+ if (this.cursor < halfWidth) {
312
+ // Cursor near start
313
+ visibleText = this.value.slice(0, scrollWidth);
314
+ cursorDisplay = this.cursor;
315
+ } else if (this.cursor > this.value.length - halfWidth) {
316
+ // Cursor near end
317
+ visibleText = this.value.slice(this.value.length - scrollWidth);
318
+ cursorDisplay = scrollWidth - (this.value.length - this.cursor);
319
+ } else {
320
+ // Cursor in middle
321
+ const start = this.cursor - halfWidth;
322
+ visibleText = this.value.slice(start, start + scrollWidth);
323
+ cursorDisplay = halfWidth;
324
+ }
325
+ }
326
+
327
+ // Build line with fake cursor
328
+ // Insert cursor character at cursor position
329
+ const beforeCursor = visibleText.slice(0, cursorDisplay);
330
+ const atCursor = visibleText[cursorDisplay] || " "; // Character at cursor, or space if at end
331
+ const afterCursor = visibleText.slice(cursorDisplay + 1);
332
+
333
+ // Use inverse video to show cursor
334
+ const cursorChar = `\x1b[7m${atCursor}\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal
335
+ const textWithCursor = beforeCursor + cursorChar + afterCursor;
336
+
337
+ // Calculate visual width
338
+ const visualLength = visibleWidth(textWithCursor);
339
+ const padding = " ".repeat(Math.max(0, availableWidth - visualLength));
340
+ const line = prompt + textWithCursor + padding;
341
+
342
+ return [line];
343
+ }
344
+ }
@@ -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
+ }