@prometheus-ai/tui 0.5.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +704 -0
  3. package/dist/types/autocomplete.d.ts +76 -0
  4. package/dist/types/bracketed-paste.d.ts +26 -0
  5. package/dist/types/components/box.d.ts +17 -0
  6. package/dist/types/components/cancellable-loader.d.ts +21 -0
  7. package/dist/types/components/editor.d.ts +105 -0
  8. package/dist/types/components/image.d.ts +84 -0
  9. package/dist/types/components/input.d.ts +18 -0
  10. package/dist/types/components/loader.d.ts +13 -0
  11. package/dist/types/components/markdown.d.ts +61 -0
  12. package/dist/types/components/scroll-view.d.ts +40 -0
  13. package/dist/types/components/select-list.d.ts +48 -0
  14. package/dist/types/components/settings-list.d.ts +41 -0
  15. package/dist/types/components/spacer.d.ts +11 -0
  16. package/dist/types/components/tab-bar.d.ts +56 -0
  17. package/dist/types/components/text.d.ts +13 -0
  18. package/dist/types/components/truncated-text.d.ts +10 -0
  19. package/dist/types/deccara.d.ts +49 -0
  20. package/dist/types/editor-component.d.ts +36 -0
  21. package/dist/types/fuzzy.d.ts +15 -0
  22. package/dist/types/index.d.ts +28 -0
  23. package/dist/types/keybindings.d.ts +189 -0
  24. package/dist/types/keys.d.ts +208 -0
  25. package/dist/types/kill-ring.d.ts +27 -0
  26. package/dist/types/kitty-graphics.d.ts +94 -0
  27. package/dist/types/stdin-buffer.d.ts +43 -0
  28. package/dist/types/symbols.d.ts +25 -0
  29. package/dist/types/terminal-capabilities.d.ts +196 -0
  30. package/dist/types/terminal.d.ts +103 -0
  31. package/dist/types/ttyid.d.ts +9 -0
  32. package/dist/types/tui.d.ts +275 -0
  33. package/dist/types/utils.d.ts +89 -0
  34. package/package.json +73 -0
  35. package/src/autocomplete.ts +871 -0
  36. package/src/bracketed-paste.ts +47 -0
  37. package/src/components/box.ts +156 -0
  38. package/src/components/cancellable-loader.ts +40 -0
  39. package/src/components/editor.ts +2695 -0
  40. package/src/components/image.ts +318 -0
  41. package/src/components/input.ts +459 -0
  42. package/src/components/loader.ts +86 -0
  43. package/src/components/markdown.ts +1189 -0
  44. package/src/components/scroll-view.ts +166 -0
  45. package/src/components/select-list.ts +331 -0
  46. package/src/components/settings-list.ts +212 -0
  47. package/src/components/spacer.ts +28 -0
  48. package/src/components/tab-bar.ts +175 -0
  49. package/src/components/text.ts +110 -0
  50. package/src/components/truncated-text.ts +61 -0
  51. package/src/deccara.ts +314 -0
  52. package/src/editor-component.ts +71 -0
  53. package/src/fuzzy.ts +143 -0
  54. package/src/index.ts +44 -0
  55. package/src/keybindings.ts +279 -0
  56. package/src/keys.ts +537 -0
  57. package/src/kill-ring.ts +46 -0
  58. package/src/kitty-graphics.ts +270 -0
  59. package/src/stdin-buffer.ts +423 -0
  60. package/src/symbols.ts +26 -0
  61. package/src/terminal-capabilities.ts +1009 -0
  62. package/src/terminal.ts +1114 -0
  63. package/src/ttyid.ts +70 -0
  64. package/src/tui.ts +2988 -0
  65. package/src/utils.ts +452 -0
@@ -0,0 +1,71 @@
1
+ import type { AutocompleteProvider } from "./autocomplete";
2
+ import type { Component } from "./tui";
3
+
4
+ /**
5
+ * Interface for custom editor components.
6
+ *
7
+ * This allows extensions to provide their own editor implementation
8
+ * (e.g., vim mode, emacs mode, custom keybindings) while maintaining
9
+ * compatibility with the core application.
10
+ */
11
+ export interface EditorComponent extends Component {
12
+ // =========================================================================
13
+ // Core text access (required)
14
+ // =========================================================================
15
+
16
+ /** Get the current text content */
17
+ getText(): string;
18
+
19
+ /** Set the text content */
20
+ setText(text: string): void;
21
+
22
+ /** Handle raw terminal input (key presses, paste sequences, etc.) */
23
+ handleInput(data: string): void;
24
+
25
+ // =========================================================================
26
+ // Callbacks (required)
27
+ // =========================================================================
28
+
29
+ /** Called when user submits (e.g., Enter key) */
30
+ onSubmit?: (text: string) => void;
31
+
32
+ /** Called when text changes */
33
+ onChange?: (text: string) => void;
34
+
35
+ // =========================================================================
36
+ // History support (optional)
37
+ // =========================================================================
38
+
39
+ /** Add text to history for up/down navigation */
40
+ addToHistory?(text: string): void;
41
+
42
+ // =========================================================================
43
+ // Advanced text manipulation (optional)
44
+ // =========================================================================
45
+
46
+ /** Insert text at current cursor position */
47
+ insertTextAtCursor?(text: string): void;
48
+
49
+ /**
50
+ * Get text with any markers expanded (e.g., paste markers).
51
+ * Falls back to getText() if not implemented.
52
+ */
53
+ getExpandedText?(): string;
54
+
55
+ // =========================================================================
56
+ // Autocomplete support (optional)
57
+ // =========================================================================
58
+
59
+ /** Set the autocomplete provider */
60
+ setAutocompleteProvider?(provider: AutocompleteProvider): void;
61
+
62
+ // =========================================================================
63
+ // Appearance (optional)
64
+ // =========================================================================
65
+
66
+ /** Border color function */
67
+ borderColor?: (str: string) => string;
68
+
69
+ /** Set horizontal padding */
70
+ setPaddingX?(padding: number): void;
71
+ }
package/src/fuzzy.ts ADDED
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Fuzzy matching utilities.
3
+ * Matches if all query characters appear in order (not necessarily consecutive).
4
+ * Lower score = better match.
5
+ */
6
+
7
+ export interface FuzzyMatch {
8
+ matches: boolean;
9
+ score: number;
10
+ }
11
+
12
+ const ALPHANUMERIC_SWAP_PENALTY = 5;
13
+
14
+ function scoreMatch(queryLower: string, textLower: string): FuzzyMatch {
15
+ if (queryLower.length === 0) {
16
+ return { matches: true, score: 0 };
17
+ }
18
+
19
+ if (queryLower.length > textLower.length) {
20
+ return { matches: false, score: 0 };
21
+ }
22
+
23
+ let queryIndex = 0;
24
+ let score = 0;
25
+ let lastMatchIndex = -1;
26
+ let consecutiveMatches = 0;
27
+
28
+ for (let i = 0; i < textLower.length && queryIndex < queryLower.length; i++) {
29
+ if (textLower[i] === queryLower[queryIndex]) {
30
+ const isWordBoundary = i === 0 || /[\s\-_./:]/.test(textLower[i - 1]!);
31
+
32
+ // Reward consecutive matches
33
+ if (lastMatchIndex === i - 1) {
34
+ consecutiveMatches++;
35
+ score -= consecutiveMatches * 5;
36
+ } else {
37
+ consecutiveMatches = 0;
38
+ // Penalize gaps
39
+ if (lastMatchIndex >= 0) {
40
+ score += (i - lastMatchIndex - 1) * 2;
41
+ }
42
+ }
43
+
44
+ // Reward word boundary matches
45
+ if (isWordBoundary) {
46
+ score -= 10;
47
+ }
48
+
49
+ // Slight penalty for later matches
50
+ score += i * 0.1;
51
+
52
+ lastMatchIndex = i;
53
+ queryIndex++;
54
+ }
55
+ }
56
+
57
+ if (queryIndex < queryLower.length) {
58
+ return { matches: false, score: 0 };
59
+ }
60
+
61
+ return { matches: true, score };
62
+ }
63
+
64
+ function buildAlphanumericSwapQueries(queryLower: string): string[] {
65
+ const variants = new Set<string>();
66
+ for (let i = 0; i < queryLower.length - 1; i++) {
67
+ const current = queryLower[i];
68
+ const next = queryLower[i + 1];
69
+ const isAlphaNumSwap =
70
+ (current && /[a-z]/.test(current) && next && /\d/.test(next)) ||
71
+ (current && /\d/.test(current) && next && /[a-z]/.test(next));
72
+ if (!isAlphaNumSwap) continue;
73
+ const swapped = queryLower.slice(0, i) + next + current + queryLower.slice(i + 2);
74
+ variants.add(swapped);
75
+ }
76
+ return [...variants];
77
+ }
78
+
79
+ export function fuzzyMatch(query: string, text: string): FuzzyMatch {
80
+ const queryLower = query.toLowerCase();
81
+ const textLower = text.toLowerCase();
82
+
83
+ const direct = scoreMatch(queryLower, textLower);
84
+ if (direct.matches) {
85
+ return direct;
86
+ }
87
+
88
+ let bestSwap: FuzzyMatch | null = null;
89
+ for (const variant of buildAlphanumericSwapQueries(queryLower)) {
90
+ const match = scoreMatch(variant, textLower);
91
+ if (!match.matches) continue;
92
+ const score = match.score + ALPHANUMERIC_SWAP_PENALTY;
93
+ if (!bestSwap || score < bestSwap.score) {
94
+ bestSwap = { matches: true, score };
95
+ }
96
+ }
97
+
98
+ return bestSwap ?? direct;
99
+ }
100
+
101
+ /**
102
+ * Filter and sort items by fuzzy match quality (best matches first).
103
+ * Supports space-separated tokens: all tokens must match.
104
+ */
105
+ export function fuzzyFilter<T>(items: T[], query: string, getText: (item: T) => string): T[] {
106
+ if (!query.trim()) {
107
+ return items;
108
+ }
109
+
110
+ const tokens = query
111
+ .trim()
112
+ .split(/\s+/)
113
+ .filter(t => t.length > 0);
114
+
115
+ if (tokens.length === 0) {
116
+ return items;
117
+ }
118
+
119
+ const results: { item: T; totalScore: number }[] = [];
120
+
121
+ for (const item of items) {
122
+ const text = getText(item);
123
+ let totalScore = 0;
124
+ let allMatch = true;
125
+
126
+ for (const token of tokens) {
127
+ const match = fuzzyMatch(token, text);
128
+ if (match.matches) {
129
+ totalScore += match.score;
130
+ } else {
131
+ allMatch = false;
132
+ break;
133
+ }
134
+ }
135
+
136
+ if (allMatch) {
137
+ results.push({ item, totalScore });
138
+ }
139
+ }
140
+
141
+ results.sort((a, b) => a.totalScore - b.totalScore);
142
+ return results.map(r => r.item);
143
+ }
package/src/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ // Core TUI interfaces and classes
2
+
3
+ // Autocomplete support
4
+ export * from "./autocomplete";
5
+ // Components
6
+ export * from "./components/box";
7
+ export * from "./components/cancellable-loader";
8
+ export * from "./components/editor";
9
+ export * from "./components/image";
10
+ export * from "./components/input";
11
+ export * from "./components/loader";
12
+ export * from "./components/markdown";
13
+ export * from "./components/scroll-view";
14
+ export * from "./components/select-list";
15
+ export * from "./components/settings-list";
16
+ export * from "./components/spacer";
17
+ export * from "./components/tab-bar";
18
+ export * from "./components/text";
19
+ export * from "./components/truncated-text";
20
+ // DECCARA rectangular-SGR background-fill optimizer
21
+ export * from "./deccara";
22
+ // Editor component interface (for custom editors)
23
+ export type * from "./editor-component";
24
+ // Fuzzy matching
25
+ export * from "./fuzzy";
26
+ // Keybindings
27
+ export * from "./keybindings";
28
+ // Kitty keyboard protocol helpers
29
+ export * from "./keys";
30
+ // Kitty graphics: Unicode placeholders + temp-file transmission
31
+ export * from "./kitty-graphics";
32
+ // Mermaid diagram support
33
+ // Input buffering for batch splitting
34
+ export * from "./stdin-buffer";
35
+ export type * from "./symbols";
36
+ // Terminal interface and implementations
37
+ export * from "./terminal";
38
+ // Terminal image support
39
+ export * from "./terminal-capabilities";
40
+ // TTY ID
41
+ export * from "./ttyid";
42
+ export * from "./tui";
43
+ // Utilities
44
+ export * from "./utils";
@@ -0,0 +1,279 @@
1
+ import { type KeyId, matchesKey, parseKey } from "./keys";
2
+
3
+ /**
4
+ * Global keybinding registry.
5
+ * Downstream packages can add keybindings via declaration merging.
6
+ */
7
+ export interface Keybindings {
8
+ // Editor navigation and editing
9
+ "tui.editor.cursorUp": true;
10
+ "tui.editor.cursorDown": true;
11
+ "tui.editor.cursorLeft": true;
12
+ "tui.editor.cursorRight": true;
13
+ "tui.editor.cursorWordLeft": true;
14
+ "tui.editor.cursorWordRight": true;
15
+ "tui.editor.cursorLineStart": true;
16
+ "tui.editor.cursorLineEnd": true;
17
+ "tui.editor.jumpForward": true;
18
+ "tui.editor.jumpBackward": true;
19
+ "tui.editor.pageUp": true;
20
+ "tui.editor.pageDown": true;
21
+ "tui.editor.deleteCharBackward": true;
22
+ "tui.editor.deleteCharForward": true;
23
+ "tui.editor.deleteWordBackward": true;
24
+ "tui.editor.deleteWordForward": true;
25
+ "tui.editor.deleteToLineStart": true;
26
+ "tui.editor.deleteToLineEnd": true;
27
+ "tui.editor.yank": true;
28
+ "tui.editor.yankPop": true;
29
+ "tui.editor.undo": true;
30
+ // Generic input actions
31
+ "tui.input.newLine": true;
32
+ "tui.input.submit": true;
33
+ "tui.input.tab": true;
34
+ "tui.input.copy": true;
35
+ // Generic selection actions
36
+ "tui.select.up": true;
37
+ "tui.select.down": true;
38
+ "tui.select.pageUp": true;
39
+ "tui.select.pageDown": true;
40
+ "tui.select.confirm": true;
41
+ "tui.select.cancel": true;
42
+ }
43
+
44
+ export type Keybinding = keyof Keybindings;
45
+
46
+ // Re-export KeyId from keys.ts
47
+ export type { KeyId };
48
+
49
+ export interface KeybindingDefinition {
50
+ defaultKeys: KeyId | KeyId[];
51
+ description?: string;
52
+ }
53
+
54
+ export type KeybindingDefinitions = Record<string, KeybindingDefinition>;
55
+ export type KeybindingsConfig = Record<string, KeyId | KeyId[] | undefined>;
56
+
57
+ export const TUI_KEYBINDINGS = {
58
+ "tui.editor.cursorUp": { defaultKeys: "up", description: "Move cursor up" },
59
+ "tui.editor.cursorDown": { defaultKeys: "down", description: "Move cursor down" },
60
+ "tui.editor.cursorLeft": {
61
+ defaultKeys: ["left", "ctrl+b"],
62
+ description: "Move cursor left",
63
+ },
64
+ "tui.editor.cursorRight": {
65
+ defaultKeys: ["right", "ctrl+f"],
66
+ description: "Move cursor right",
67
+ },
68
+ "tui.editor.cursorWordLeft": {
69
+ defaultKeys: ["alt+left", "ctrl+left", "alt+b"],
70
+ description: "Move cursor word left",
71
+ },
72
+ "tui.editor.cursorWordRight": {
73
+ defaultKeys: ["alt+right", "ctrl+right", "alt+f"],
74
+ description: "Move cursor word right",
75
+ },
76
+ "tui.editor.cursorLineStart": {
77
+ defaultKeys: ["home", "ctrl+a"],
78
+ description: "Move to line start",
79
+ },
80
+ "tui.editor.cursorLineEnd": {
81
+ defaultKeys: ["end", "ctrl+e"],
82
+ description: "Move to line end",
83
+ },
84
+ "tui.editor.jumpForward": {
85
+ defaultKeys: "ctrl+]",
86
+ description: "Jump forward to character",
87
+ },
88
+ "tui.editor.jumpBackward": {
89
+ defaultKeys: "ctrl+alt+]",
90
+ description: "Jump backward to character",
91
+ },
92
+ "tui.editor.pageUp": { defaultKeys: "pageUp", description: "Page up" },
93
+ "tui.editor.pageDown": { defaultKeys: "pageDown", description: "Page down" },
94
+ "tui.editor.deleteCharBackward": {
95
+ defaultKeys: "backspace",
96
+ description: "Delete character backward",
97
+ },
98
+ "tui.editor.deleteCharForward": {
99
+ defaultKeys: ["delete", "ctrl+d"],
100
+ description: "Delete character forward",
101
+ },
102
+ "tui.editor.deleteWordBackward": {
103
+ defaultKeys: ["ctrl+w", "alt+backspace", "ctrl+backspace"],
104
+ description: "Delete word backward",
105
+ },
106
+ "tui.editor.deleteWordForward": {
107
+ defaultKeys: ["alt+delete", "alt+d"],
108
+ description: "Delete word forward",
109
+ },
110
+ "tui.editor.deleteToLineStart": {
111
+ defaultKeys: "ctrl+u",
112
+ description: "Delete to line start",
113
+ },
114
+ "tui.editor.deleteToLineEnd": {
115
+ defaultKeys: "ctrl+k",
116
+ description: "Delete to line end",
117
+ },
118
+ "tui.editor.yank": { defaultKeys: "ctrl+y", description: "Yank" },
119
+ "tui.editor.yankPop": { defaultKeys: "alt+y", description: "Yank pop" },
120
+ "tui.editor.undo": { defaultKeys: ["ctrl+-", "ctrl+_"], description: "Undo" },
121
+ "tui.input.newLine": { defaultKeys: "shift+enter", description: "Insert newline" },
122
+ "tui.input.submit": { defaultKeys: "enter", description: "Submit input" },
123
+ "tui.input.tab": { defaultKeys: "tab", description: "Tab / autocomplete" },
124
+ "tui.input.copy": { defaultKeys: "ctrl+c", description: "Copy selection" },
125
+ "tui.select.up": { defaultKeys: "up", description: "Move selection up" },
126
+ "tui.select.down": { defaultKeys: "down", description: "Move selection down" },
127
+ "tui.select.pageUp": { defaultKeys: "pageUp", description: "Selection page up" },
128
+ "tui.select.pageDown": {
129
+ defaultKeys: "pageDown",
130
+ description: "Selection page down",
131
+ },
132
+ "tui.select.confirm": { defaultKeys: "enter", description: "Confirm selection" },
133
+ "tui.select.cancel": {
134
+ defaultKeys: ["escape", "ctrl+c"],
135
+ description: "Cancel selection",
136
+ },
137
+ } as const satisfies KeybindingDefinitions;
138
+
139
+ export interface KeybindingConflict {
140
+ key: KeyId;
141
+ keybindings: string[];
142
+ }
143
+
144
+ const SHIFTED_SYMBOL_KEYS = new Set<string>([
145
+ "!",
146
+ "@",
147
+ "#",
148
+ "$",
149
+ "%",
150
+ "^",
151
+ "&",
152
+ "*",
153
+ "(",
154
+ ")",
155
+ "_",
156
+ "+",
157
+ "{",
158
+ "}",
159
+ "|",
160
+ ":",
161
+ "<",
162
+ ">",
163
+ "?",
164
+ "~",
165
+ ]);
166
+
167
+ const normalizeKeyId = (key: KeyId): KeyId => key.toLowerCase() as KeyId;
168
+
169
+ function normalizeKeys(keys: KeyId | KeyId[] | undefined): KeyId[] {
170
+ if (keys === undefined) return [];
171
+ const keyList = Array.isArray(keys) ? keys : [keys];
172
+ const seen = new Set<KeyId>();
173
+ const result: KeyId[] = [];
174
+ for (const key of keyList) {
175
+ const normalized = normalizeKeyId(key);
176
+ if (!seen.has(normalized)) {
177
+ seen.add(normalized);
178
+ result.push(normalized);
179
+ }
180
+ }
181
+ return result;
182
+ }
183
+
184
+ export class KeybindingsManager {
185
+ #definitions: KeybindingDefinitions;
186
+ #userBindings: KeybindingsConfig;
187
+ #keysById = new Map<Keybinding, KeyId[]>();
188
+ #conflicts: KeybindingConflict[] = [];
189
+
190
+ constructor(definitions: KeybindingDefinitions, userBindings: KeybindingsConfig = {}) {
191
+ this.#definitions = definitions;
192
+ this.#userBindings = userBindings;
193
+ this.#rebuild();
194
+ }
195
+
196
+ #rebuild(): void {
197
+ this.#keysById.clear();
198
+ this.#conflicts = [];
199
+
200
+ const userClaims = new Map<KeyId, Set<Keybinding>>();
201
+ for (const [keybinding, keys] of Object.entries(this.#userBindings)) {
202
+ if (!(keybinding in this.#definitions)) continue;
203
+ for (const key of normalizeKeys(keys)) {
204
+ const claimants = userClaims.get(key) ?? new Set<Keybinding>();
205
+ claimants.add(keybinding as Keybinding);
206
+ userClaims.set(key, claimants);
207
+ }
208
+ }
209
+
210
+ for (const [key, keybindings] of userClaims) {
211
+ if (keybindings.size > 1) {
212
+ this.#conflicts.push({ key, keybindings: [...keybindings] });
213
+ }
214
+ }
215
+
216
+ for (const [id, definition] of Object.entries(this.#definitions)) {
217
+ const userKeys = this.#userBindings[id];
218
+ const keys = userKeys === undefined ? normalizeKeys(definition.defaultKeys) : normalizeKeys(userKeys);
219
+ this.#keysById.set(id as Keybinding, keys);
220
+ }
221
+ }
222
+
223
+ matches(data: string, keybinding: Keybinding): boolean {
224
+ const keys = this.#keysById.get(keybinding) ?? [];
225
+ for (const key of keys) {
226
+ if (matchesKey(data, key)) return true;
227
+ }
228
+
229
+ // Handle shifted symbol keys (e.g., shift+- produces _ on US layout)
230
+ const parsed = parseKey(data);
231
+ if (!parsed?.startsWith("shift+")) return false;
232
+ const keyName = parsed.slice("shift+".length);
233
+ if (!SHIFTED_SYMBOL_KEYS.has(keyName)) return false;
234
+ return keys.includes(keyName as KeyId);
235
+ }
236
+
237
+ getKeys(keybinding: Keybinding): KeyId[] {
238
+ return [...(this.#keysById.get(keybinding) ?? [])];
239
+ }
240
+
241
+ getDefinition(keybinding: Keybinding): KeybindingDefinition {
242
+ return this.#definitions[keybinding];
243
+ }
244
+
245
+ getConflicts(): KeybindingConflict[] {
246
+ return this.#conflicts.map(conflict => ({ ...conflict, keybindings: [...conflict.keybindings] }));
247
+ }
248
+
249
+ setUserBindings(userBindings: KeybindingsConfig): void {
250
+ this.#userBindings = userBindings;
251
+ this.#rebuild();
252
+ }
253
+
254
+ getUserBindings(): KeybindingsConfig {
255
+ return { ...this.#userBindings };
256
+ }
257
+
258
+ getResolvedBindings(): KeybindingsConfig {
259
+ const resolved: KeybindingsConfig = {};
260
+ for (const id of Object.keys(this.#definitions)) {
261
+ const keys = this.#keysById.get(id as Keybinding) ?? [];
262
+ resolved[id] = keys.length === 1 ? keys[0]! : [...keys];
263
+ }
264
+ return resolved;
265
+ }
266
+ }
267
+
268
+ let globalKeybindings: KeybindingsManager | null = null;
269
+
270
+ export function setKeybindings(keybindings: KeybindingsManager): void {
271
+ globalKeybindings = keybindings;
272
+ }
273
+
274
+ export function getKeybindings(): KeybindingsManager {
275
+ if (!globalKeybindings) {
276
+ globalKeybindings = new KeybindingsManager(TUI_KEYBINDINGS);
277
+ }
278
+ return globalKeybindings;
279
+ }