@gajae-code/tui 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +818 -0
- package/README.md +704 -0
- package/dist/types/autocomplete.d.ts +76 -0
- package/dist/types/bracketed-paste.d.ts +26 -0
- package/dist/types/components/box.d.ts +15 -0
- package/dist/types/components/cancellable-loader.d.ts +21 -0
- package/dist/types/components/editor.d.ts +101 -0
- package/dist/types/components/image.d.ts +16 -0
- package/dist/types/components/input.d.ts +16 -0
- package/dist/types/components/loader.d.ts +13 -0
- package/dist/types/components/markdown.d.ts +61 -0
- package/dist/types/components/select-list.d.ts +46 -0
- package/dist/types/components/settings-list.d.ts +39 -0
- package/dist/types/components/spacer.d.ts +11 -0
- package/dist/types/components/tab-bar.d.ts +56 -0
- package/dist/types/components/text.d.ts +13 -0
- package/dist/types/components/truncated-text.d.ts +10 -0
- package/dist/types/editor-component.d.ts +36 -0
- package/dist/types/fuzzy.d.ts +15 -0
- package/dist/types/index.d.ts +25 -0
- package/dist/types/keybindings.d.ts +189 -0
- package/dist/types/keys.d.ts +208 -0
- package/dist/types/kill-ring.d.ts +27 -0
- package/dist/types/stdin-buffer.d.ts +43 -0
- package/dist/types/symbols.d.ts +23 -0
- package/dist/types/terminal-capabilities.d.ts +75 -0
- package/dist/types/terminal.d.ts +61 -0
- package/dist/types/ttyid.d.ts +9 -0
- package/dist/types/tui.d.ts +161 -0
- package/dist/types/utils.d.ts +74 -0
- package/package.json +73 -0
- package/src/autocomplete.ts +836 -0
- package/src/bracketed-paste.ts +47 -0
- package/src/components/box.ts +144 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +2664 -0
- package/src/components/image.ts +90 -0
- package/src/components/input.ts +465 -0
- package/src/components/loader.ts +86 -0
- package/src/components/markdown.ts +1009 -0
- package/src/components/select-list.ts +249 -0
- package/src/components/settings-list.ts +211 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +175 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +61 -0
- package/src/editor-component.ts +71 -0
- package/src/fuzzy.ts +143 -0
- package/src/index.ts +39 -0
- package/src/keybindings.ts +279 -0
- package/src/keys.ts +537 -0
- package/src/kill-ring.ts +46 -0
- package/src/stdin-buffer.ts +410 -0
- package/src/symbols.ts +24 -0
- package/src/terminal-capabilities.ts +537 -0
- package/src/terminal.ts +716 -0
- package/src/ttyid.ts +66 -0
- package/src/tui.ts +1481 -0
- package/src/utils.ts +359 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Ellipsis,
|
|
3
|
+
type ExtractSegmentsResult,
|
|
4
|
+
extractSegments as nativeExtractSegments,
|
|
5
|
+
sliceWithWidth as nativeSliceWithWidth,
|
|
6
|
+
truncateToWidth as nativeTruncateToWidth,
|
|
7
|
+
wrapTextWithAnsi as nativeWrapTextWithAnsi,
|
|
8
|
+
type SliceResult,
|
|
9
|
+
} from "@gajae-code/natives";
|
|
10
|
+
import { getDefaultTabWidth, getIndentation } from "@gajae-code/utils";
|
|
11
|
+
|
|
12
|
+
export { Ellipsis } from "@gajae-code/natives";
|
|
13
|
+
|
|
14
|
+
export { getDefaultTabWidth, getIndentation } from "@gajae-code/utils";
|
|
15
|
+
|
|
16
|
+
export function sliceWithWidth(line: string, startCol: number, length: number, strict?: boolean | null): SliceResult {
|
|
17
|
+
return nativeSliceWithWidth(line, startCol, length, strict ?? null, getDefaultTabWidth());
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function truncateToWidth(
|
|
21
|
+
text: string,
|
|
22
|
+
maxWidth: number,
|
|
23
|
+
ellipsisKind?: Ellipsis | null,
|
|
24
|
+
pad?: boolean | null,
|
|
25
|
+
): string {
|
|
26
|
+
// Guard nullish napi inputs: napi-rs 3 on the Windows prebuilt rejects
|
|
27
|
+
// `null` for `Option<u8>` (Ellipsis) / `Option<bool>` (pad) (issue #848),
|
|
28
|
+
// and `maxWidth` is a required `u32` that throws on `null`/`undefined`
|
|
29
|
+
// everywhere. Pass concrete defaults that mirror the Rust `unwrap_or`s.
|
|
30
|
+
const safeWidth = Number.isFinite(maxWidth) ? Math.max(0, Math.trunc(maxWidth)) : 0;
|
|
31
|
+
let resolvedEllipsis: Ellipsis | null | undefined | string = ellipsisKind;
|
|
32
|
+
if (typeof resolvedEllipsis === "string") {
|
|
33
|
+
resolvedEllipsis = resolvedEllipsis === "" ? Ellipsis.Omit : Ellipsis.Unicode;
|
|
34
|
+
}
|
|
35
|
+
return nativeTruncateToWidth(
|
|
36
|
+
text,
|
|
37
|
+
safeWidth,
|
|
38
|
+
resolvedEllipsis ?? Ellipsis.Unicode,
|
|
39
|
+
pad ?? false,
|
|
40
|
+
getDefaultTabWidth(),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function wrapTextWithAnsi(text: string, width: number): string[] {
|
|
45
|
+
return nativeWrapTextWithAnsi(text, width, getDefaultTabWidth());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function extractSegments(
|
|
49
|
+
line: string,
|
|
50
|
+
beforeEnd: number,
|
|
51
|
+
afterStart: number,
|
|
52
|
+
afterLen: number,
|
|
53
|
+
strictAfter: boolean,
|
|
54
|
+
): ExtractSegmentsResult {
|
|
55
|
+
return nativeExtractSegments(line, beforeEnd, afterStart, afterLen, strictAfter, getDefaultTabWidth());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Pre-allocated space buffer for padding
|
|
59
|
+
const SPACE_BUFFER = " ".repeat(512);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Tab width in columns for `file`, using `process.cwd()` as the project root for relative paths.
|
|
63
|
+
*/
|
|
64
|
+
export function getIndentationNoescape(file?: string): number {
|
|
65
|
+
return getIndentation(file, process.cwd());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
* Replace tabs with configured spacing for consistent rendering.
|
|
70
|
+
*/
|
|
71
|
+
export function replaceTabs(text: string, file?: string): string {
|
|
72
|
+
return text.replaceAll("\t", " ".repeat(getIndentation(file)));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns a string of n spaces. Uses a pre-allocated buffer for efficiency.
|
|
77
|
+
*/
|
|
78
|
+
export function padding(n: number): string {
|
|
79
|
+
if (n <= 0) return "";
|
|
80
|
+
if (n <= 512) return SPACE_BUFFER.slice(0, n);
|
|
81
|
+
return " ".repeat(n);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Grapheme segmenter (shared instance)
|
|
85
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the shared grapheme segmenter instance.
|
|
89
|
+
*/
|
|
90
|
+
export function getSegmenter(): Intl.Segmenter {
|
|
91
|
+
return segmenter;
|
|
92
|
+
}
|
|
93
|
+
function normalizeForWidth(str: string): string {
|
|
94
|
+
const normalized = str.normalize("NFC");
|
|
95
|
+
return normalized === str ? str : normalized;
|
|
96
|
+
}
|
|
97
|
+
export function visibleWidthRaw(str: string): number {
|
|
98
|
+
if (!str) {
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Fast path: pure ASCII printable
|
|
103
|
+
let tabLength = 0;
|
|
104
|
+
const tabWidth = getDefaultTabWidth();
|
|
105
|
+
let isPureAscii = true;
|
|
106
|
+
for (let i = 0; i < str.length; i++) {
|
|
107
|
+
const code = str.charCodeAt(i);
|
|
108
|
+
if (code === 9) {
|
|
109
|
+
tabLength += tabWidth;
|
|
110
|
+
} else if (code < 0x20 || code > 0x7e) {
|
|
111
|
+
isPureAscii = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (isPureAscii) {
|
|
115
|
+
return str.length + tabLength;
|
|
116
|
+
}
|
|
117
|
+
return Bun.stringWidth(normalizeForWidth(str)) + tabLength;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Calculate the visible width of a string in terminal columns.
|
|
122
|
+
*/
|
|
123
|
+
export function visibleWidth(str: string): number {
|
|
124
|
+
if (!str) return 0;
|
|
125
|
+
return visibleWidthRaw(str);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const THAI_LAO_AM_REGEX = /[\u0e33\u0eb3]/;
|
|
129
|
+
const THAI_LAO_AM_GLOBAL_REGEX = /[\u0e33\u0eb3]/g;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Normalize text for terminal output without changing logical editor content.
|
|
133
|
+
* Some terminals render precomposed Thai/Lao AM vowels inconsistently during
|
|
134
|
+
* differential repaint. Their compatibility decompositions have the same cell
|
|
135
|
+
* width but avoid stale-cell artifacts in terminal renderers.
|
|
136
|
+
*/
|
|
137
|
+
export function normalizeTerminalOutput(str: string): string {
|
|
138
|
+
if (!THAI_LAO_AM_REGEX.test(str)) return str;
|
|
139
|
+
return str.replace(THAI_LAO_AM_GLOBAL_REGEX, char => (char === "\u0e33" ? "\u0e4d\u0e32" : "\u0ecd\u0eb2"));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const makeBoolArray = (chars: string): Uint8Array => {
|
|
143
|
+
const table = new Uint8Array(128);
|
|
144
|
+
for (let i = 0; i < chars.length; i++) {
|
|
145
|
+
const code = chars.charCodeAt(i);
|
|
146
|
+
if (code < table.length) {
|
|
147
|
+
table[code] = 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return table;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const ASCII_WHITESPACE = makeBoolArray("\x09\x0a\x0b\x0c\x0d\x20");
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Check if a character is whitespace.
|
|
157
|
+
*/
|
|
158
|
+
export function isWhitespaceChar(char: string): boolean {
|
|
159
|
+
const code = char.codePointAt(0) ?? 0;
|
|
160
|
+
return code < 128 && ASCII_WHITESPACE[code] === 1;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const ASCII_PUNCTUATION = makeBoolArray("(){}[]<>.,;:'\"!?+-=*/\\|&%^$#@~`");
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Check if a character is punctuation.
|
|
167
|
+
*/
|
|
168
|
+
export function isPunctuationChar(char: string): boolean {
|
|
169
|
+
const code = char.codePointAt(0) ?? 0;
|
|
170
|
+
return code < 128 && ASCII_PUNCTUATION[code] === 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export type WordNavKind = "whitespace" | "delimiter" | "cjk" | "word" | "other";
|
|
174
|
+
|
|
175
|
+
const WORD_NAV_RE_WHITESPACE = /^\p{White_Space}$/u;
|
|
176
|
+
const WORD_NAV_RE_PUNCT = /^\p{P}$/u;
|
|
177
|
+
const WORD_NAV_RE_SYMBOL = /^\p{S}$/u;
|
|
178
|
+
const WORD_NAV_RE_LETTER = /^\p{L}$/u;
|
|
179
|
+
const WORD_NAV_RE_NUMBER = /^\p{N}$/u;
|
|
180
|
+
const WORD_NAV_RE_HAN = /^\p{Script=Han}$/u;
|
|
181
|
+
const WORD_NAV_RE_HIRAGANA = /^\p{Script=Hiragana}$/u;
|
|
182
|
+
const WORD_NAV_RE_KATAKANA = /^\p{Script=Katakana}$/u;
|
|
183
|
+
const WORD_NAV_RE_HANGUL = /^\p{Script=Hangul}$/u;
|
|
184
|
+
|
|
185
|
+
function firstCodePointChar(str: string): string {
|
|
186
|
+
const cp = str.codePointAt(0);
|
|
187
|
+
if (cp === undefined) return "";
|
|
188
|
+
return String.fromCodePoint(cp);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Coarse Unicode-aware character classification for word navigation (Option/Alt + Left/Right).
|
|
193
|
+
* This intentionally avoids language-specific word segmentation for predictability across scripts.
|
|
194
|
+
*/
|
|
195
|
+
export function getWordNavKind(grapheme: string): WordNavKind {
|
|
196
|
+
if (!grapheme) return "other";
|
|
197
|
+
const ch = firstCodePointChar(grapheme);
|
|
198
|
+
if (!ch) return "other";
|
|
199
|
+
if (WORD_NAV_RE_WHITESPACE.test(ch)) return "whitespace";
|
|
200
|
+
if (WORD_NAV_RE_PUNCT.test(ch) || WORD_NAV_RE_SYMBOL.test(ch)) return "delimiter";
|
|
201
|
+
if (
|
|
202
|
+
WORD_NAV_RE_HAN.test(ch) ||
|
|
203
|
+
WORD_NAV_RE_HIRAGANA.test(ch) ||
|
|
204
|
+
WORD_NAV_RE_KATAKANA.test(ch) ||
|
|
205
|
+
WORD_NAV_RE_HANGUL.test(ch)
|
|
206
|
+
) {
|
|
207
|
+
return "cjk";
|
|
208
|
+
}
|
|
209
|
+
if (ch === "_" || WORD_NAV_RE_LETTER.test(ch) || WORD_NAV_RE_NUMBER.test(ch)) return "word";
|
|
210
|
+
return "other";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const WORD_NAV_JOINERS = new Set(["'", "’", "-", "‐", "‑"]);
|
|
214
|
+
|
|
215
|
+
export function isWordNavJoiner(grapheme: string): boolean {
|
|
216
|
+
const ch = firstCodePointChar(grapheme);
|
|
217
|
+
return WORD_NAV_JOINERS.has(ch);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Move the cursor one "word" to the left using Unicode-aware coarse navigation.
|
|
222
|
+
*
|
|
223
|
+
* Returns a new cursor index in the range [0, text.length].
|
|
224
|
+
*/
|
|
225
|
+
export function moveWordLeft(text: string, cursor: number): number {
|
|
226
|
+
const len = text.length;
|
|
227
|
+
if (len === 0) return 0;
|
|
228
|
+
let i = Math.min(Math.max(cursor, 0), len);
|
|
229
|
+
if (i === 0) return 0;
|
|
230
|
+
|
|
231
|
+
const graphemes = [...segmenter.segment(text.slice(0, i))];
|
|
232
|
+
if (graphemes.length === 0) return 0;
|
|
233
|
+
|
|
234
|
+
// Skip trailing whitespace.
|
|
235
|
+
while (graphemes.length > 0 && getWordNavKind(graphemes[graphemes.length - 1]?.segment || "") === "whitespace") {
|
|
236
|
+
i -= graphemes.pop()?.segment.length || 0;
|
|
237
|
+
}
|
|
238
|
+
if (i === 0 || graphemes.length === 0) return i;
|
|
239
|
+
|
|
240
|
+
const kind = getWordNavKind(graphemes[graphemes.length - 1]?.segment || "");
|
|
241
|
+
if (kind === "delimiter" || kind === "cjk") {
|
|
242
|
+
while (graphemes.length > 0 && getWordNavKind(graphemes[graphemes.length - 1]?.segment || "") === kind) {
|
|
243
|
+
i -= graphemes.pop()?.segment.length || 0;
|
|
244
|
+
}
|
|
245
|
+
return i;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (kind === "word") {
|
|
249
|
+
// Skip word run (letters/numbers/underscore), keeping common joiners inside words.
|
|
250
|
+
let hasRightWord = false;
|
|
251
|
+
while (graphemes.length > 0) {
|
|
252
|
+
const g = graphemes[graphemes.length - 1]?.segment || "";
|
|
253
|
+
const k = getWordNavKind(g);
|
|
254
|
+
if (k === "word") {
|
|
255
|
+
hasRightWord = true;
|
|
256
|
+
i -= graphemes.pop()?.segment.length || 0;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (hasRightWord && k === "delimiter" && isWordNavJoiner(g)) {
|
|
260
|
+
const left = graphemes[graphemes.length - 2]?.segment || "";
|
|
261
|
+
if (getWordNavKind(left) === "word") {
|
|
262
|
+
i -= graphemes.pop()?.segment.length || 0;
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
return i;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Fallback: move by one grapheme.
|
|
272
|
+
i -= graphemes.pop()?.segment.length || 0;
|
|
273
|
+
return Math.max(0, i);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Move the cursor one "word" to the right using Unicode-aware coarse navigation.
|
|
278
|
+
*
|
|
279
|
+
* Returns a new cursor index in the range [0, text.length].
|
|
280
|
+
*/
|
|
281
|
+
export function moveWordRight(text: string, cursor: number): number {
|
|
282
|
+
const len = text.length;
|
|
283
|
+
if (len === 0) return 0;
|
|
284
|
+
let i = Math.min(Math.max(cursor, 0), len);
|
|
285
|
+
if (i === len) return len;
|
|
286
|
+
|
|
287
|
+
const iterator = segmenter.segment(text.slice(i))[Symbol.iterator]();
|
|
288
|
+
let next = iterator.next();
|
|
289
|
+
|
|
290
|
+
// Skip leading whitespace.
|
|
291
|
+
while (!next.done && getWordNavKind(next.value.segment) === "whitespace") {
|
|
292
|
+
i += next.value.segment.length;
|
|
293
|
+
next = iterator.next();
|
|
294
|
+
}
|
|
295
|
+
if (next.done) return i;
|
|
296
|
+
|
|
297
|
+
const firstKind = getWordNavKind(next.value.segment);
|
|
298
|
+
if (firstKind === "delimiter" || firstKind === "cjk") {
|
|
299
|
+
while (!next.done && getWordNavKind(next.value.segment) === firstKind) {
|
|
300
|
+
i += next.value.segment.length;
|
|
301
|
+
next = iterator.next();
|
|
302
|
+
}
|
|
303
|
+
return i;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (firstKind === "word") {
|
|
307
|
+
let hasLeftWord = false;
|
|
308
|
+
while (!next.done) {
|
|
309
|
+
const segment = next.value.segment;
|
|
310
|
+
const k = getWordNavKind(segment);
|
|
311
|
+
if (k === "word") {
|
|
312
|
+
hasLeftWord = true;
|
|
313
|
+
i += segment.length;
|
|
314
|
+
next = iterator.next();
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (hasLeftWord && k === "delimiter" && isWordNavJoiner(segment)) {
|
|
318
|
+
const lookahead = iterator.next();
|
|
319
|
+
if (!lookahead.done && getWordNavKind(lookahead.value.segment) === "word") {
|
|
320
|
+
i += segment.length;
|
|
321
|
+
next = lookahead;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
return i;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Fallback: move by one grapheme.
|
|
331
|
+
return i + next.value.segment.length;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Apply background color to a line, padding to full width.
|
|
336
|
+
*
|
|
337
|
+
* @param line - Line of text (may contain ANSI codes)
|
|
338
|
+
* @param width - Total width to pad to
|
|
339
|
+
* @param bgFn - Background color function
|
|
340
|
+
* @returns Line with background applied and padded to width
|
|
341
|
+
*/
|
|
342
|
+
export function applyBackgroundToLine(line: string, width: number, bgFn: (text: string) => string): string {
|
|
343
|
+
// Calculate padding needed
|
|
344
|
+
const visibleLen = visibleWidth(line);
|
|
345
|
+
const paddingNeeded = Math.max(0, width - visibleLen);
|
|
346
|
+
|
|
347
|
+
// Apply background to content + padding
|
|
348
|
+
const withPadding = line + padding(paddingNeeded);
|
|
349
|
+
return bgFn(withPadding);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Extract a range of visible columns from a line. Handles ANSI codes and wide chars.
|
|
354
|
+
*
|
|
355
|
+
* @param strict - If true, exclude wide chars at boundary that would extend past the range
|
|
356
|
+
*/
|
|
357
|
+
export function sliceByColumn(line: string, startCol: number, length: number, strict = false): string {
|
|
358
|
+
return sliceWithWidth(line, startCol, length, strict).text;
|
|
359
|
+
}
|