@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,229 @@
|
|
|
1
|
+
import { getKeybindings } from "../keybindings.js";
|
|
2
|
+
import type { Component } from "../tui.js";
|
|
3
|
+
import { truncateToWidth, visibleWidth } from "../utils.js";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_PRIMARY_COLUMN_WIDTH = 32;
|
|
6
|
+
const PRIMARY_COLUMN_GAP = 2;
|
|
7
|
+
const MIN_DESCRIPTION_WIDTH = 10;
|
|
8
|
+
|
|
9
|
+
const normalizeToSingleLine = (text: string): string => text.replace(/[\r\n]+/g, " ").trim();
|
|
10
|
+
const clamp = (value: number, min: number, max: number): number => Math.max(min, Math.min(value, max));
|
|
11
|
+
|
|
12
|
+
export interface SelectItem {
|
|
13
|
+
value: string;
|
|
14
|
+
label: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SelectListTheme {
|
|
19
|
+
selectedPrefix: (text: string) => string;
|
|
20
|
+
selectedText: (text: string) => string;
|
|
21
|
+
description: (text: string) => string;
|
|
22
|
+
scrollInfo: (text: string) => string;
|
|
23
|
+
noMatch: (text: string) => string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SelectListTruncatePrimaryContext {
|
|
27
|
+
text: string;
|
|
28
|
+
maxWidth: number;
|
|
29
|
+
columnWidth: number;
|
|
30
|
+
item: SelectItem;
|
|
31
|
+
isSelected: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SelectListLayoutOptions {
|
|
35
|
+
minPrimaryColumnWidth?: number;
|
|
36
|
+
maxPrimaryColumnWidth?: number;
|
|
37
|
+
truncatePrimary?: (context: SelectListTruncatePrimaryContext) => string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class SelectList implements Component {
|
|
41
|
+
private items: SelectItem[] = [];
|
|
42
|
+
private filteredItems: SelectItem[] = [];
|
|
43
|
+
private selectedIndex: number = 0;
|
|
44
|
+
private maxVisible: number = 5;
|
|
45
|
+
private theme: SelectListTheme;
|
|
46
|
+
private layout: SelectListLayoutOptions;
|
|
47
|
+
|
|
48
|
+
public onSelect?: (item: SelectItem) => void;
|
|
49
|
+
public onCancel?: () => void;
|
|
50
|
+
public onSelectionChange?: (item: SelectItem) => void;
|
|
51
|
+
|
|
52
|
+
constructor(items: SelectItem[], maxVisible: number, theme: SelectListTheme, layout: SelectListLayoutOptions = {}) {
|
|
53
|
+
this.items = items;
|
|
54
|
+
this.filteredItems = items;
|
|
55
|
+
this.maxVisible = maxVisible;
|
|
56
|
+
this.theme = theme;
|
|
57
|
+
this.layout = layout;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setFilter(filter: string): void {
|
|
61
|
+
this.filteredItems = this.items.filter((item) => item.value.toLowerCase().startsWith(filter.toLowerCase()));
|
|
62
|
+
// Reset selection when filter changes
|
|
63
|
+
this.selectedIndex = 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setSelectedIndex(index: number): void {
|
|
67
|
+
this.selectedIndex = Math.max(0, Math.min(index, this.filteredItems.length - 1));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
invalidate(): void {
|
|
71
|
+
// No cached state to invalidate currently
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
render(width: number): string[] {
|
|
75
|
+
const lines: string[] = [];
|
|
76
|
+
|
|
77
|
+
// If no items match filter, show message
|
|
78
|
+
if (this.filteredItems.length === 0) {
|
|
79
|
+
lines.push(this.theme.noMatch(" No matching commands"));
|
|
80
|
+
return lines;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const primaryColumnWidth = this.getPrimaryColumnWidth();
|
|
84
|
+
|
|
85
|
+
// Calculate visible range with scrolling
|
|
86
|
+
const startIndex = Math.max(
|
|
87
|
+
0,
|
|
88
|
+
Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible),
|
|
89
|
+
);
|
|
90
|
+
const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length);
|
|
91
|
+
|
|
92
|
+
// Render visible items
|
|
93
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
94
|
+
const item = this.filteredItems[i];
|
|
95
|
+
if (!item) continue;
|
|
96
|
+
|
|
97
|
+
const isSelected = i === this.selectedIndex;
|
|
98
|
+
const descriptionSingleLine = item.description ? normalizeToSingleLine(item.description) : undefined;
|
|
99
|
+
lines.push(this.renderItem(item, isSelected, width, descriptionSingleLine, primaryColumnWidth));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Add scroll indicators if needed
|
|
103
|
+
if (startIndex > 0 || endIndex < this.filteredItems.length) {
|
|
104
|
+
const scrollText = ` (${this.selectedIndex + 1}/${this.filteredItems.length})`;
|
|
105
|
+
// Truncate if too long for terminal
|
|
106
|
+
lines.push(this.theme.scrollInfo(truncateToWidth(scrollText, width - 2, "")));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return lines;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
handleInput(keyData: string): void {
|
|
113
|
+
const kb = getKeybindings();
|
|
114
|
+
// Up arrow - wrap to bottom when at top
|
|
115
|
+
if (kb.matches(keyData, "tui.select.up")) {
|
|
116
|
+
this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1;
|
|
117
|
+
this.notifySelectionChange();
|
|
118
|
+
}
|
|
119
|
+
// Down arrow - wrap to top when at bottom
|
|
120
|
+
else if (kb.matches(keyData, "tui.select.down")) {
|
|
121
|
+
this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1;
|
|
122
|
+
this.notifySelectionChange();
|
|
123
|
+
}
|
|
124
|
+
// Enter
|
|
125
|
+
else if (kb.matches(keyData, "tui.select.confirm")) {
|
|
126
|
+
const selectedItem = this.filteredItems[this.selectedIndex];
|
|
127
|
+
if (selectedItem && this.onSelect) {
|
|
128
|
+
this.onSelect(selectedItem);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Escape or Ctrl+C
|
|
132
|
+
else if (kb.matches(keyData, "tui.select.cancel")) {
|
|
133
|
+
if (this.onCancel) {
|
|
134
|
+
this.onCancel();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private renderItem(
|
|
140
|
+
item: SelectItem,
|
|
141
|
+
isSelected: boolean,
|
|
142
|
+
width: number,
|
|
143
|
+
descriptionSingleLine: string | undefined,
|
|
144
|
+
primaryColumnWidth: number,
|
|
145
|
+
): string {
|
|
146
|
+
const prefix = isSelected ? "→ " : " ";
|
|
147
|
+
const prefixWidth = visibleWidth(prefix);
|
|
148
|
+
|
|
149
|
+
if (descriptionSingleLine && width > 40) {
|
|
150
|
+
const effectivePrimaryColumnWidth = Math.max(1, Math.min(primaryColumnWidth, width - prefixWidth - 4));
|
|
151
|
+
const maxPrimaryWidth = Math.max(1, effectivePrimaryColumnWidth - PRIMARY_COLUMN_GAP);
|
|
152
|
+
const truncatedValue = this.truncatePrimary(item, isSelected, maxPrimaryWidth, effectivePrimaryColumnWidth);
|
|
153
|
+
const truncatedValueWidth = visibleWidth(truncatedValue);
|
|
154
|
+
const spacing = " ".repeat(Math.max(1, effectivePrimaryColumnWidth - truncatedValueWidth));
|
|
155
|
+
const descriptionStart = prefixWidth + truncatedValueWidth + spacing.length;
|
|
156
|
+
const remainingWidth = width - descriptionStart - 2; // -2 for safety
|
|
157
|
+
|
|
158
|
+
if (remainingWidth > MIN_DESCRIPTION_WIDTH) {
|
|
159
|
+
const truncatedDesc = truncateToWidth(descriptionSingleLine, remainingWidth, "");
|
|
160
|
+
if (isSelected) {
|
|
161
|
+
return this.theme.selectedText(`${prefix}${truncatedValue}${spacing}${truncatedDesc}`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const descText = this.theme.description(spacing + truncatedDesc);
|
|
165
|
+
return prefix + truncatedValue + descText;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const maxWidth = width - prefixWidth - 2;
|
|
170
|
+
const truncatedValue = this.truncatePrimary(item, isSelected, maxWidth, maxWidth);
|
|
171
|
+
if (isSelected) {
|
|
172
|
+
return this.theme.selectedText(`${prefix}${truncatedValue}`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return prefix + truncatedValue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private getPrimaryColumnWidth(): number {
|
|
179
|
+
const { min, max } = this.getPrimaryColumnBounds();
|
|
180
|
+
const widestPrimary = this.filteredItems.reduce((widest, item) => {
|
|
181
|
+
return Math.max(widest, visibleWidth(this.getDisplayValue(item)) + PRIMARY_COLUMN_GAP);
|
|
182
|
+
}, 0);
|
|
183
|
+
|
|
184
|
+
return clamp(widestPrimary, min, max);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private getPrimaryColumnBounds(): { min: number; max: number } {
|
|
188
|
+
const rawMin =
|
|
189
|
+
this.layout.minPrimaryColumnWidth ?? this.layout.maxPrimaryColumnWidth ?? DEFAULT_PRIMARY_COLUMN_WIDTH;
|
|
190
|
+
const rawMax =
|
|
191
|
+
this.layout.maxPrimaryColumnWidth ?? this.layout.minPrimaryColumnWidth ?? DEFAULT_PRIMARY_COLUMN_WIDTH;
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
min: Math.max(1, Math.min(rawMin, rawMax)),
|
|
195
|
+
max: Math.max(1, Math.max(rawMin, rawMax)),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private truncatePrimary(item: SelectItem, isSelected: boolean, maxWidth: number, columnWidth: number): string {
|
|
200
|
+
const displayValue = this.getDisplayValue(item);
|
|
201
|
+
const truncatedValue = this.layout.truncatePrimary
|
|
202
|
+
? this.layout.truncatePrimary({
|
|
203
|
+
text: displayValue,
|
|
204
|
+
maxWidth,
|
|
205
|
+
columnWidth,
|
|
206
|
+
item,
|
|
207
|
+
isSelected,
|
|
208
|
+
})
|
|
209
|
+
: truncateToWidth(displayValue, maxWidth, "");
|
|
210
|
+
|
|
211
|
+
return truncateToWidth(truncatedValue, maxWidth, "");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private getDisplayValue(item: SelectItem): string {
|
|
215
|
+
return item.label || item.value;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private notifySelectionChange(): void {
|
|
219
|
+
const selectedItem = this.filteredItems[this.selectedIndex];
|
|
220
|
+
if (selectedItem && this.onSelectionChange) {
|
|
221
|
+
this.onSelectionChange(selectedItem);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
getSelectedItem(): SelectItem | null {
|
|
226
|
+
const item = this.filteredItems[this.selectedIndex];
|
|
227
|
+
return item || null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { fuzzyFilter } from "../fuzzy.js";
|
|
2
|
+
import { getKeybindings } from "../keybindings.js";
|
|
3
|
+
import type { Component } from "../tui.js";
|
|
4
|
+
import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "../utils.js";
|
|
5
|
+
import { Input } from "./input.js";
|
|
6
|
+
|
|
7
|
+
export interface SettingItem {
|
|
8
|
+
/** Unique identifier for this setting */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Display label (left side) */
|
|
11
|
+
label: string;
|
|
12
|
+
/** Optional description shown when selected */
|
|
13
|
+
description?: string;
|
|
14
|
+
/** Current value to display (right side) */
|
|
15
|
+
currentValue: string;
|
|
16
|
+
/** If provided, Enter/Space cycles through these values */
|
|
17
|
+
values?: string[];
|
|
18
|
+
/** If provided, Enter opens this submenu. Receives current value and done callback. */
|
|
19
|
+
submenu?: (currentValue: string, done: (selectedValue?: string) => void) => Component;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SettingsListTheme {
|
|
23
|
+
label: (text: string, selected: boolean) => string;
|
|
24
|
+
value: (text: string, selected: boolean) => string;
|
|
25
|
+
description: (text: string) => string;
|
|
26
|
+
cursor: string;
|
|
27
|
+
hint: (text: string) => string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SettingsListOptions {
|
|
31
|
+
enableSearch?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class SettingsList implements Component {
|
|
35
|
+
private items: SettingItem[];
|
|
36
|
+
private filteredItems: SettingItem[];
|
|
37
|
+
private theme: SettingsListTheme;
|
|
38
|
+
private selectedIndex = 0;
|
|
39
|
+
private maxVisible: number;
|
|
40
|
+
private onChange: (id: string, newValue: string) => void;
|
|
41
|
+
private onCancel: () => void;
|
|
42
|
+
private searchInput?: Input;
|
|
43
|
+
private searchEnabled: boolean;
|
|
44
|
+
|
|
45
|
+
// Submenu state
|
|
46
|
+
private submenuComponent: Component | null = null;
|
|
47
|
+
private submenuItemIndex: number | null = null;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
items: SettingItem[],
|
|
51
|
+
maxVisible: number,
|
|
52
|
+
theme: SettingsListTheme,
|
|
53
|
+
onChange: (id: string, newValue: string) => void,
|
|
54
|
+
onCancel: () => void,
|
|
55
|
+
options: SettingsListOptions = {},
|
|
56
|
+
) {
|
|
57
|
+
this.items = items;
|
|
58
|
+
this.filteredItems = items;
|
|
59
|
+
this.maxVisible = maxVisible;
|
|
60
|
+
this.theme = theme;
|
|
61
|
+
this.onChange = onChange;
|
|
62
|
+
this.onCancel = onCancel;
|
|
63
|
+
this.searchEnabled = options.enableSearch ?? false;
|
|
64
|
+
if (this.searchEnabled) {
|
|
65
|
+
this.searchInput = new Input();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Update an item's currentValue */
|
|
70
|
+
updateValue(id: string, newValue: string): void {
|
|
71
|
+
const item = this.items.find((i) => i.id === id);
|
|
72
|
+
if (item) {
|
|
73
|
+
item.currentValue = newValue;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
invalidate(): void {
|
|
78
|
+
this.submenuComponent?.invalidate?.();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
render(width: number): string[] {
|
|
82
|
+
// If submenu is active, render it instead
|
|
83
|
+
if (this.submenuComponent) {
|
|
84
|
+
return this.submenuComponent.render(width);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return this.renderMainList(width);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private renderMainList(width: number): string[] {
|
|
91
|
+
const lines: string[] = [];
|
|
92
|
+
|
|
93
|
+
if (this.searchEnabled && this.searchInput) {
|
|
94
|
+
lines.push(...this.searchInput.render(width));
|
|
95
|
+
lines.push("");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (this.items.length === 0) {
|
|
99
|
+
lines.push(this.theme.hint(" No settings available"));
|
|
100
|
+
if (this.searchEnabled) {
|
|
101
|
+
this.addHintLine(lines, width);
|
|
102
|
+
}
|
|
103
|
+
return lines;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const displayItems = this.searchEnabled ? this.filteredItems : this.items;
|
|
107
|
+
if (displayItems.length === 0) {
|
|
108
|
+
lines.push(truncateToWidth(this.theme.hint(" No matching settings"), width));
|
|
109
|
+
this.addHintLine(lines, width);
|
|
110
|
+
return lines;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Calculate visible range with scrolling
|
|
114
|
+
const startIndex = Math.max(
|
|
115
|
+
0,
|
|
116
|
+
Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), displayItems.length - this.maxVisible),
|
|
117
|
+
);
|
|
118
|
+
const endIndex = Math.min(startIndex + this.maxVisible, displayItems.length);
|
|
119
|
+
|
|
120
|
+
// Calculate max label width for alignment
|
|
121
|
+
const maxLabelWidth = Math.min(30, Math.max(...this.items.map((item) => visibleWidth(item.label))));
|
|
122
|
+
|
|
123
|
+
// Render visible items
|
|
124
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
125
|
+
const item = displayItems[i];
|
|
126
|
+
if (!item) continue;
|
|
127
|
+
|
|
128
|
+
const isSelected = i === this.selectedIndex;
|
|
129
|
+
const prefix = isSelected ? this.theme.cursor : " ";
|
|
130
|
+
const prefixWidth = visibleWidth(prefix);
|
|
131
|
+
|
|
132
|
+
// Pad label to align values
|
|
133
|
+
const labelPadded = item.label + " ".repeat(Math.max(0, maxLabelWidth - visibleWidth(item.label)));
|
|
134
|
+
const labelText = this.theme.label(labelPadded, isSelected);
|
|
135
|
+
|
|
136
|
+
// Calculate space for value
|
|
137
|
+
const separator = " ";
|
|
138
|
+
const usedWidth = prefixWidth + maxLabelWidth + visibleWidth(separator);
|
|
139
|
+
const valueMaxWidth = width - usedWidth - 2;
|
|
140
|
+
|
|
141
|
+
const valueText = this.theme.value(truncateToWidth(item.currentValue, valueMaxWidth, ""), isSelected);
|
|
142
|
+
|
|
143
|
+
lines.push(truncateToWidth(prefix + labelText + separator + valueText, width));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Add scroll indicator if needed
|
|
147
|
+
if (startIndex > 0 || endIndex < displayItems.length) {
|
|
148
|
+
const scrollText = ` (${this.selectedIndex + 1}/${displayItems.length})`;
|
|
149
|
+
lines.push(this.theme.hint(truncateToWidth(scrollText, width - 2, "")));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Add description for selected item
|
|
153
|
+
const selectedItem = displayItems[this.selectedIndex];
|
|
154
|
+
if (selectedItem?.description) {
|
|
155
|
+
lines.push("");
|
|
156
|
+
const wrappedDesc = wrapTextWithAnsi(selectedItem.description, width - 4);
|
|
157
|
+
for (const line of wrappedDesc) {
|
|
158
|
+
lines.push(this.theme.description(` ${line}`));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Add hint
|
|
163
|
+
this.addHintLine(lines, width);
|
|
164
|
+
|
|
165
|
+
return lines;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
handleInput(data: string): void {
|
|
169
|
+
// If submenu is active, delegate all input to it
|
|
170
|
+
// The submenu's onCancel (triggered by escape) will call done() which closes it
|
|
171
|
+
if (this.submenuComponent) {
|
|
172
|
+
this.submenuComponent.handleInput?.(data);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Main list input handling
|
|
177
|
+
const kb = getKeybindings();
|
|
178
|
+
const displayItems = this.searchEnabled ? this.filteredItems : this.items;
|
|
179
|
+
if (kb.matches(data, "tui.select.up")) {
|
|
180
|
+
if (displayItems.length === 0) return;
|
|
181
|
+
this.selectedIndex = this.selectedIndex === 0 ? displayItems.length - 1 : this.selectedIndex - 1;
|
|
182
|
+
} else if (kb.matches(data, "tui.select.down")) {
|
|
183
|
+
if (displayItems.length === 0) return;
|
|
184
|
+
this.selectedIndex = this.selectedIndex === displayItems.length - 1 ? 0 : this.selectedIndex + 1;
|
|
185
|
+
} else if (kb.matches(data, "tui.select.confirm") || data === " ") {
|
|
186
|
+
this.activateItem();
|
|
187
|
+
} else if (kb.matches(data, "tui.select.cancel")) {
|
|
188
|
+
this.onCancel();
|
|
189
|
+
} else if (this.searchEnabled && this.searchInput) {
|
|
190
|
+
const sanitized = data.replace(/ /g, "");
|
|
191
|
+
if (!sanitized) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.searchInput.handleInput(sanitized);
|
|
195
|
+
this.applyFilter(this.searchInput.getValue());
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private activateItem(): void {
|
|
200
|
+
const item = this.searchEnabled ? this.filteredItems[this.selectedIndex] : this.items[this.selectedIndex];
|
|
201
|
+
if (!item) return;
|
|
202
|
+
|
|
203
|
+
if (item.submenu) {
|
|
204
|
+
// Open submenu, passing current value so it can pre-select correctly
|
|
205
|
+
this.submenuItemIndex = this.selectedIndex;
|
|
206
|
+
this.submenuComponent = item.submenu(item.currentValue, (selectedValue?: string) => {
|
|
207
|
+
if (selectedValue !== undefined) {
|
|
208
|
+
item.currentValue = selectedValue;
|
|
209
|
+
this.onChange(item.id, selectedValue);
|
|
210
|
+
}
|
|
211
|
+
this.closeSubmenu();
|
|
212
|
+
});
|
|
213
|
+
} else if (item.values && item.values.length > 0) {
|
|
214
|
+
// Cycle through values
|
|
215
|
+
const currentIndex = item.values.indexOf(item.currentValue);
|
|
216
|
+
const nextIndex = (currentIndex + 1) % item.values.length;
|
|
217
|
+
const newValue = item.values[nextIndex];
|
|
218
|
+
item.currentValue = newValue;
|
|
219
|
+
this.onChange(item.id, newValue);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private closeSubmenu(): void {
|
|
224
|
+
this.submenuComponent = null;
|
|
225
|
+
// Restore selection to the item that opened the submenu
|
|
226
|
+
if (this.submenuItemIndex !== null) {
|
|
227
|
+
this.selectedIndex = this.submenuItemIndex;
|
|
228
|
+
this.submenuItemIndex = null;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private applyFilter(query: string): void {
|
|
233
|
+
this.filteredItems = fuzzyFilter(this.items, query, (item) => item.label);
|
|
234
|
+
this.selectedIndex = 0;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private addHintLine(lines: string[], width: number): void {
|
|
238
|
+
lines.push("");
|
|
239
|
+
lines.push(
|
|
240
|
+
truncateToWidth(
|
|
241
|
+
this.theme.hint(
|
|
242
|
+
this.searchEnabled
|
|
243
|
+
? " Type to search · Enter/Space to change · Esc to cancel"
|
|
244
|
+
: " Enter/Space to change · Esc to cancel",
|
|
245
|
+
),
|
|
246
|
+
width,
|
|
247
|
+
),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Component } from "../tui.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Spacer component that renders empty lines
|
|
5
|
+
*/
|
|
6
|
+
export class Spacer implements Component {
|
|
7
|
+
private lines: number;
|
|
8
|
+
|
|
9
|
+
constructor(lines: number = 1) {
|
|
10
|
+
this.lines = lines;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
setLines(lines: number): void {
|
|
14
|
+
this.lines = lines;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
invalidate(): void {
|
|
18
|
+
// No cached state to invalidate currently
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
render(_width: number): string[] {
|
|
22
|
+
const result: string[] = [];
|
|
23
|
+
for (let i = 0; i < this.lines; i++) {
|
|
24
|
+
result.push("");
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { Component } from "../tui.js";
|
|
2
|
+
import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Text component - displays multi-line text with word wrapping
|
|
6
|
+
*/
|
|
7
|
+
export class Text implements Component {
|
|
8
|
+
private text: string;
|
|
9
|
+
private paddingX: number; // Left/right padding
|
|
10
|
+
private paddingY: number; // Top/bottom padding
|
|
11
|
+
private customBgFn?: (text: string) => string;
|
|
12
|
+
|
|
13
|
+
// Cache for rendered output
|
|
14
|
+
private cachedText?: string;
|
|
15
|
+
private cachedWidth?: number;
|
|
16
|
+
private cachedLines?: string[];
|
|
17
|
+
|
|
18
|
+
constructor(text: string = "", paddingX: number = 1, paddingY: number = 1, customBgFn?: (text: string) => string) {
|
|
19
|
+
this.text = text;
|
|
20
|
+
this.paddingX = paddingX;
|
|
21
|
+
this.paddingY = paddingY;
|
|
22
|
+
this.customBgFn = customBgFn;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setText(text: string): void {
|
|
26
|
+
this.text = text;
|
|
27
|
+
this.cachedText = undefined;
|
|
28
|
+
this.cachedWidth = undefined;
|
|
29
|
+
this.cachedLines = undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setCustomBgFn(customBgFn?: (text: string) => string): void {
|
|
33
|
+
this.customBgFn = customBgFn;
|
|
34
|
+
this.cachedText = undefined;
|
|
35
|
+
this.cachedWidth = undefined;
|
|
36
|
+
this.cachedLines = undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
invalidate(): void {
|
|
40
|
+
this.cachedText = undefined;
|
|
41
|
+
this.cachedWidth = undefined;
|
|
42
|
+
this.cachedLines = undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
render(width: number): string[] {
|
|
46
|
+
// Check cache
|
|
47
|
+
if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
|
|
48
|
+
return this.cachedLines;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Don't render anything if there's no actual text
|
|
52
|
+
if (!this.text || this.text.trim() === "") {
|
|
53
|
+
const result: string[] = [];
|
|
54
|
+
this.cachedText = this.text;
|
|
55
|
+
this.cachedWidth = width;
|
|
56
|
+
this.cachedLines = result;
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Replace tabs with 3 spaces
|
|
61
|
+
const normalizedText = this.text.replace(/\t/g, " ");
|
|
62
|
+
|
|
63
|
+
// Calculate content width (subtract left/right margins)
|
|
64
|
+
const contentWidth = Math.max(1, width - this.paddingX * 2);
|
|
65
|
+
|
|
66
|
+
// Wrap text (this preserves ANSI codes but does NOT pad)
|
|
67
|
+
const wrappedLines = wrapTextWithAnsi(normalizedText, contentWidth);
|
|
68
|
+
|
|
69
|
+
// Add margins and background to each line
|
|
70
|
+
const leftMargin = " ".repeat(this.paddingX);
|
|
71
|
+
const rightMargin = " ".repeat(this.paddingX);
|
|
72
|
+
const contentLines: string[] = [];
|
|
73
|
+
|
|
74
|
+
for (const line of wrappedLines) {
|
|
75
|
+
// Add margins
|
|
76
|
+
const lineWithMargins = leftMargin + line + rightMargin;
|
|
77
|
+
|
|
78
|
+
// Apply background if specified (this also pads to full width)
|
|
79
|
+
if (this.customBgFn) {
|
|
80
|
+
contentLines.push(applyBackgroundToLine(lineWithMargins, width, this.customBgFn));
|
|
81
|
+
} else {
|
|
82
|
+
// No background - just pad to width with spaces
|
|
83
|
+
const visibleLen = visibleWidth(lineWithMargins);
|
|
84
|
+
const paddingNeeded = Math.max(0, width - visibleLen);
|
|
85
|
+
contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Add top/bottom padding (empty lines)
|
|
90
|
+
const emptyLine = " ".repeat(width);
|
|
91
|
+
const emptyLines: string[] = [];
|
|
92
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
93
|
+
const line = this.customBgFn ? applyBackgroundToLine(emptyLine, width, this.customBgFn) : emptyLine;
|
|
94
|
+
emptyLines.push(line);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const result = [...emptyLines, ...contentLines, ...emptyLines];
|
|
98
|
+
|
|
99
|
+
// Update cache
|
|
100
|
+
this.cachedText = this.text;
|
|
101
|
+
this.cachedWidth = width;
|
|
102
|
+
this.cachedLines = result;
|
|
103
|
+
|
|
104
|
+
return result.length > 0 ? result : [""];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Component } from "../tui.js";
|
|
2
|
+
import { truncateToWidth, visibleWidth } from "../utils.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Text component that truncates to fit viewport width
|
|
6
|
+
*/
|
|
7
|
+
export class TruncatedText implements Component {
|
|
8
|
+
private text: string;
|
|
9
|
+
private paddingX: number;
|
|
10
|
+
private paddingY: number;
|
|
11
|
+
|
|
12
|
+
constructor(text: string, paddingX: number = 0, paddingY: number = 0) {
|
|
13
|
+
this.text = text;
|
|
14
|
+
this.paddingX = paddingX;
|
|
15
|
+
this.paddingY = paddingY;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
invalidate(): void {
|
|
19
|
+
// No cached state to invalidate currently
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
render(width: number): string[] {
|
|
23
|
+
const result: string[] = [];
|
|
24
|
+
|
|
25
|
+
// Empty line padded to width
|
|
26
|
+
const emptyLine = " ".repeat(width);
|
|
27
|
+
|
|
28
|
+
// Add vertical padding above
|
|
29
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
30
|
+
result.push(emptyLine);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Calculate available width after horizontal padding
|
|
34
|
+
const availableWidth = Math.max(1, width - this.paddingX * 2);
|
|
35
|
+
|
|
36
|
+
// Take only the first line (stop at newline)
|
|
37
|
+
let singleLineText = this.text;
|
|
38
|
+
const newlineIndex = this.text.indexOf("\n");
|
|
39
|
+
if (newlineIndex !== -1) {
|
|
40
|
+
singleLineText = this.text.substring(0, newlineIndex);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Truncate text if needed (accounting for ANSI codes)
|
|
44
|
+
const displayText = truncateToWidth(singleLineText, availableWidth);
|
|
45
|
+
|
|
46
|
+
// Add horizontal padding
|
|
47
|
+
const leftPadding = " ".repeat(this.paddingX);
|
|
48
|
+
const rightPadding = " ".repeat(this.paddingX);
|
|
49
|
+
const lineWithPadding = leftPadding + displayText + rightPadding;
|
|
50
|
+
|
|
51
|
+
// Pad line to exactly width characters
|
|
52
|
+
const lineVisibleWidth = visibleWidth(lineWithPadding);
|
|
53
|
+
const paddingNeeded = Math.max(0, width - lineVisibleWidth);
|
|
54
|
+
const finalLine = lineWithPadding + " ".repeat(paddingNeeded);
|
|
55
|
+
|
|
56
|
+
result.push(finalLine);
|
|
57
|
+
|
|
58
|
+
// Add vertical padding below
|
|
59
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
60
|
+
result.push(emptyLine);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
}
|