@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.
- package/README.md +654 -0
- package/package.json +45 -0
- package/src/autocomplete.ts +575 -0
- package/src/components/box.ts +134 -0
- package/src/components/cancellable-loader.ts +39 -0
- package/src/components/editor.ts +1342 -0
- package/src/components/image.ts +87 -0
- package/src/components/input.ts +344 -0
- package/src/components/loader.ts +55 -0
- package/src/components/markdown.ts +646 -0
- package/src/components/select-list.ts +184 -0
- package/src/components/settings-list.ts +188 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +140 -0
- package/src/components/text.ts +106 -0
- package/src/components/truncated-text.ts +65 -0
- package/src/index.ts +91 -0
- package/src/keys.ts +560 -0
- package/src/terminal-image.ts +340 -0
- package/src/terminal.ts +163 -0
- package/src/tui.ts +353 -0
- package/src/utils.ts +712 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { isArrowDown, isArrowUp, isCtrlC, isEnter, isEscape } from "../keys.js";
|
|
2
|
+
import type { Component } from "../tui.js";
|
|
3
|
+
import { truncateToWidth } from "../utils.js";
|
|
4
|
+
|
|
5
|
+
export interface SelectItem {
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SelectListTheme {
|
|
12
|
+
selectedPrefix: (text: string) => string;
|
|
13
|
+
selectedText: (text: string) => string;
|
|
14
|
+
description: (text: string) => string;
|
|
15
|
+
scrollInfo: (text: string) => string;
|
|
16
|
+
noMatch: (text: string) => string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class SelectList implements Component {
|
|
20
|
+
private items: SelectItem[] = [];
|
|
21
|
+
private filteredItems: SelectItem[] = [];
|
|
22
|
+
private selectedIndex: number = 0;
|
|
23
|
+
private maxVisible: number = 5;
|
|
24
|
+
private theme: SelectListTheme;
|
|
25
|
+
|
|
26
|
+
public onSelect?: (item: SelectItem) => void;
|
|
27
|
+
public onCancel?: () => void;
|
|
28
|
+
public onSelectionChange?: (item: SelectItem) => void;
|
|
29
|
+
|
|
30
|
+
constructor(items: SelectItem[], maxVisible: number, theme: SelectListTheme) {
|
|
31
|
+
this.items = items;
|
|
32
|
+
this.filteredItems = items;
|
|
33
|
+
this.maxVisible = maxVisible;
|
|
34
|
+
this.theme = theme;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setFilter(filter: string): void {
|
|
38
|
+
this.filteredItems = this.items.filter((item) => item.value.toLowerCase().startsWith(filter.toLowerCase()));
|
|
39
|
+
// Reset selection when filter changes
|
|
40
|
+
this.selectedIndex = 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setSelectedIndex(index: number): void {
|
|
44
|
+
this.selectedIndex = Math.max(0, Math.min(index, this.filteredItems.length - 1));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
invalidate(): void {
|
|
48
|
+
// No cached state to invalidate currently
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
render(width: number): string[] {
|
|
52
|
+
const lines: string[] = [];
|
|
53
|
+
|
|
54
|
+
// If no items match filter, show message
|
|
55
|
+
if (this.filteredItems.length === 0) {
|
|
56
|
+
lines.push(this.theme.noMatch(" No matching commands"));
|
|
57
|
+
return lines;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Calculate visible range with scrolling
|
|
61
|
+
const startIndex = Math.max(
|
|
62
|
+
0,
|
|
63
|
+
Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible),
|
|
64
|
+
);
|
|
65
|
+
const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length);
|
|
66
|
+
|
|
67
|
+
// Render visible items
|
|
68
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
69
|
+
const item = this.filteredItems[i];
|
|
70
|
+
if (!item) continue;
|
|
71
|
+
|
|
72
|
+
const isSelected = i === this.selectedIndex;
|
|
73
|
+
|
|
74
|
+
let line = "";
|
|
75
|
+
if (isSelected) {
|
|
76
|
+
// Use arrow indicator for selection - entire line uses selectedText color
|
|
77
|
+
const prefixWidth = 2; // "→ " is 2 characters visually
|
|
78
|
+
const displayValue = item.label || item.value;
|
|
79
|
+
|
|
80
|
+
if (item.description && width > 40) {
|
|
81
|
+
// Calculate how much space we have for value + description
|
|
82
|
+
const maxValueWidth = Math.min(30, width - prefixWidth - 4);
|
|
83
|
+
const truncatedValue = truncateToWidth(displayValue, maxValueWidth, "");
|
|
84
|
+
const spacing = " ".repeat(Math.max(1, 32 - truncatedValue.length));
|
|
85
|
+
|
|
86
|
+
// Calculate remaining space for description using visible widths
|
|
87
|
+
const descriptionStart = prefixWidth + truncatedValue.length + spacing.length;
|
|
88
|
+
const remainingWidth = width - descriptionStart - 2; // -2 for safety
|
|
89
|
+
|
|
90
|
+
if (remainingWidth > 10) {
|
|
91
|
+
const truncatedDesc = truncateToWidth(item.description, remainingWidth, "");
|
|
92
|
+
// Apply selectedText to entire line content
|
|
93
|
+
line = this.theme.selectedText(`→ ${truncatedValue}${spacing}${truncatedDesc}`);
|
|
94
|
+
} else {
|
|
95
|
+
// Not enough space for description
|
|
96
|
+
const maxWidth = width - prefixWidth - 2;
|
|
97
|
+
line = this.theme.selectedText(`→ ${truncateToWidth(displayValue, maxWidth, "")}`);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
// No description or not enough width
|
|
101
|
+
const maxWidth = width - prefixWidth - 2;
|
|
102
|
+
line = this.theme.selectedText(`→ ${truncateToWidth(displayValue, maxWidth, "")}`);
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
const displayValue = item.label || item.value;
|
|
106
|
+
const prefix = " ";
|
|
107
|
+
|
|
108
|
+
if (item.description && width > 40) {
|
|
109
|
+
// Calculate how much space we have for value + description
|
|
110
|
+
const maxValueWidth = Math.min(30, width - prefix.length - 4);
|
|
111
|
+
const truncatedValue = truncateToWidth(displayValue, maxValueWidth, "");
|
|
112
|
+
const spacing = " ".repeat(Math.max(1, 32 - truncatedValue.length));
|
|
113
|
+
|
|
114
|
+
// Calculate remaining space for description
|
|
115
|
+
const descriptionStart = prefix.length + truncatedValue.length + spacing.length;
|
|
116
|
+
const remainingWidth = width - descriptionStart - 2; // -2 for safety
|
|
117
|
+
|
|
118
|
+
if (remainingWidth > 10) {
|
|
119
|
+
const truncatedDesc = truncateToWidth(item.description, remainingWidth, "");
|
|
120
|
+
const descText = this.theme.description(spacing + truncatedDesc);
|
|
121
|
+
line = prefix + truncatedValue + descText;
|
|
122
|
+
} else {
|
|
123
|
+
// Not enough space for description
|
|
124
|
+
const maxWidth = width - prefix.length - 2;
|
|
125
|
+
line = prefix + truncateToWidth(displayValue, maxWidth, "");
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
// No description or not enough width
|
|
129
|
+
const maxWidth = width - prefix.length - 2;
|
|
130
|
+
line = prefix + truncateToWidth(displayValue, maxWidth, "");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
lines.push(line);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Add scroll indicators if needed
|
|
138
|
+
if (startIndex > 0 || endIndex < this.filteredItems.length) {
|
|
139
|
+
const scrollText = ` (${this.selectedIndex + 1}/${this.filteredItems.length})`;
|
|
140
|
+
// Truncate if too long for terminal
|
|
141
|
+
lines.push(this.theme.scrollInfo(truncateToWidth(scrollText, width - 2, "")));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return lines;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
handleInput(keyData: string): void {
|
|
148
|
+
// Up arrow - wrap to bottom when at top
|
|
149
|
+
if (isArrowUp(keyData)) {
|
|
150
|
+
this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1;
|
|
151
|
+
this.notifySelectionChange();
|
|
152
|
+
}
|
|
153
|
+
// Down arrow - wrap to top when at bottom
|
|
154
|
+
else if (isArrowDown(keyData)) {
|
|
155
|
+
this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1;
|
|
156
|
+
this.notifySelectionChange();
|
|
157
|
+
}
|
|
158
|
+
// Enter
|
|
159
|
+
else if (isEnter(keyData)) {
|
|
160
|
+
const selectedItem = this.filteredItems[this.selectedIndex];
|
|
161
|
+
if (selectedItem && this.onSelect) {
|
|
162
|
+
this.onSelect(selectedItem);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Escape or Ctrl+C
|
|
166
|
+
else if (isEscape(keyData) || isCtrlC(keyData)) {
|
|
167
|
+
if (this.onCancel) {
|
|
168
|
+
this.onCancel();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private notifySelectionChange(): void {
|
|
174
|
+
const selectedItem = this.filteredItems[this.selectedIndex];
|
|
175
|
+
if (selectedItem && this.onSelectionChange) {
|
|
176
|
+
this.onSelectionChange(selectedItem);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
getSelectedItem(): SelectItem | null {
|
|
181
|
+
const item = this.filteredItems[this.selectedIndex];
|
|
182
|
+
return item || null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { isArrowDown, isArrowUp, isCtrlC, isEnter, isEscape } from "../keys.js";
|
|
2
|
+
import type { Component } from "../tui.js";
|
|
3
|
+
import { truncateToWidth, visibleWidth } from "../utils.js";
|
|
4
|
+
|
|
5
|
+
export interface SettingItem {
|
|
6
|
+
/** Unique identifier for this setting */
|
|
7
|
+
id: string;
|
|
8
|
+
/** Display label (left side) */
|
|
9
|
+
label: string;
|
|
10
|
+
/** Optional description shown when selected */
|
|
11
|
+
description?: string;
|
|
12
|
+
/** Current value to display (right side) */
|
|
13
|
+
currentValue: string;
|
|
14
|
+
/** If provided, Enter/Space cycles through these values */
|
|
15
|
+
values?: string[];
|
|
16
|
+
/** If provided, Enter opens this submenu. Receives current value and done callback. */
|
|
17
|
+
submenu?: (currentValue: string, done: (selectedValue?: string) => void) => Component;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SettingsListTheme {
|
|
21
|
+
label: (text: string, selected: boolean) => string;
|
|
22
|
+
value: (text: string, selected: boolean) => string;
|
|
23
|
+
description: (text: string) => string;
|
|
24
|
+
cursor: string;
|
|
25
|
+
hint: (text: string) => string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class SettingsList implements Component {
|
|
29
|
+
private items: SettingItem[];
|
|
30
|
+
private theme: SettingsListTheme;
|
|
31
|
+
private selectedIndex = 0;
|
|
32
|
+
private maxVisible: number;
|
|
33
|
+
private onChange: (id: string, newValue: string) => void;
|
|
34
|
+
private onCancel: () => void;
|
|
35
|
+
|
|
36
|
+
// Submenu state
|
|
37
|
+
private submenuComponent: Component | null = null;
|
|
38
|
+
private submenuItemIndex: number | null = null;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
items: SettingItem[],
|
|
42
|
+
maxVisible: number,
|
|
43
|
+
theme: SettingsListTheme,
|
|
44
|
+
onChange: (id: string, newValue: string) => void,
|
|
45
|
+
onCancel: () => void,
|
|
46
|
+
) {
|
|
47
|
+
this.items = items;
|
|
48
|
+
this.maxVisible = maxVisible;
|
|
49
|
+
this.theme = theme;
|
|
50
|
+
this.onChange = onChange;
|
|
51
|
+
this.onCancel = onCancel;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Update an item's currentValue */
|
|
55
|
+
updateValue(id: string, newValue: string): void {
|
|
56
|
+
const item = this.items.find((i) => i.id === id);
|
|
57
|
+
if (item) {
|
|
58
|
+
item.currentValue = newValue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
invalidate(): void {
|
|
63
|
+
this.submenuComponent?.invalidate?.();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
render(width: number): string[] {
|
|
67
|
+
// If submenu is active, render it instead
|
|
68
|
+
if (this.submenuComponent) {
|
|
69
|
+
return this.submenuComponent.render(width);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return this.renderMainList(width);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private renderMainList(width: number): string[] {
|
|
76
|
+
const lines: string[] = [];
|
|
77
|
+
|
|
78
|
+
if (this.items.length === 0) {
|
|
79
|
+
lines.push(this.theme.hint(" No settings available"));
|
|
80
|
+
return lines;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Calculate visible range with scrolling
|
|
84
|
+
const startIndex = Math.max(
|
|
85
|
+
0,
|
|
86
|
+
Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.items.length - this.maxVisible),
|
|
87
|
+
);
|
|
88
|
+
const endIndex = Math.min(startIndex + this.maxVisible, this.items.length);
|
|
89
|
+
|
|
90
|
+
// Calculate max label width for alignment
|
|
91
|
+
const maxLabelWidth = Math.min(30, Math.max(...this.items.map((item) => visibleWidth(item.label))));
|
|
92
|
+
|
|
93
|
+
// Render visible items
|
|
94
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
95
|
+
const item = this.items[i];
|
|
96
|
+
if (!item) continue;
|
|
97
|
+
|
|
98
|
+
const isSelected = i === this.selectedIndex;
|
|
99
|
+
const prefix = isSelected ? this.theme.cursor : " ";
|
|
100
|
+
const prefixWidth = visibleWidth(prefix);
|
|
101
|
+
|
|
102
|
+
// Pad label to align values
|
|
103
|
+
const labelPadded = item.label + " ".repeat(Math.max(0, maxLabelWidth - visibleWidth(item.label)));
|
|
104
|
+
const labelText = this.theme.label(labelPadded, isSelected);
|
|
105
|
+
|
|
106
|
+
// Calculate space for value
|
|
107
|
+
const separator = " ";
|
|
108
|
+
const usedWidth = prefixWidth + maxLabelWidth + visibleWidth(separator);
|
|
109
|
+
const valueMaxWidth = width - usedWidth - 2;
|
|
110
|
+
|
|
111
|
+
const valueText = this.theme.value(truncateToWidth(item.currentValue, valueMaxWidth, ""), isSelected);
|
|
112
|
+
|
|
113
|
+
lines.push(prefix + labelText + separator + valueText);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Add scroll indicator if needed
|
|
117
|
+
if (startIndex > 0 || endIndex < this.items.length) {
|
|
118
|
+
const scrollText = ` (${this.selectedIndex + 1}/${this.items.length})`;
|
|
119
|
+
lines.push(this.theme.hint(truncateToWidth(scrollText, width - 2, "")));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Add description for selected item
|
|
123
|
+
const selectedItem = this.items[this.selectedIndex];
|
|
124
|
+
if (selectedItem?.description) {
|
|
125
|
+
lines.push("");
|
|
126
|
+
lines.push(this.theme.description(` ${truncateToWidth(selectedItem.description, width - 4, "")}`));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Add hint
|
|
130
|
+
lines.push("");
|
|
131
|
+
lines.push(this.theme.hint(" Enter/Space to change · Esc to cancel"));
|
|
132
|
+
|
|
133
|
+
return lines;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
handleInput(data: string): void {
|
|
137
|
+
// If submenu is active, delegate all input to it
|
|
138
|
+
// The submenu's onCancel (triggered by escape) will call done() which closes it
|
|
139
|
+
if (this.submenuComponent) {
|
|
140
|
+
this.submenuComponent.handleInput?.(data);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Main list input handling
|
|
145
|
+
if (isArrowUp(data)) {
|
|
146
|
+
this.selectedIndex = this.selectedIndex === 0 ? this.items.length - 1 : this.selectedIndex - 1;
|
|
147
|
+
} else if (isArrowDown(data)) {
|
|
148
|
+
this.selectedIndex = this.selectedIndex === this.items.length - 1 ? 0 : this.selectedIndex + 1;
|
|
149
|
+
} else if (isEnter(data) || data === " ") {
|
|
150
|
+
this.activateItem();
|
|
151
|
+
} else if (isEscape(data) || isCtrlC(data)) {
|
|
152
|
+
this.onCancel();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private activateItem(): void {
|
|
157
|
+
const item = this.items[this.selectedIndex];
|
|
158
|
+
if (!item) return;
|
|
159
|
+
|
|
160
|
+
if (item.submenu) {
|
|
161
|
+
// Open submenu, passing current value so it can pre-select correctly
|
|
162
|
+
this.submenuItemIndex = this.selectedIndex;
|
|
163
|
+
this.submenuComponent = item.submenu(item.currentValue, (selectedValue?: string) => {
|
|
164
|
+
if (selectedValue !== undefined) {
|
|
165
|
+
item.currentValue = selectedValue;
|
|
166
|
+
this.onChange(item.id, selectedValue);
|
|
167
|
+
}
|
|
168
|
+
this.closeSubmenu();
|
|
169
|
+
});
|
|
170
|
+
} else if (item.values && item.values.length > 0) {
|
|
171
|
+
// Cycle through values
|
|
172
|
+
const currentIndex = item.values.indexOf(item.currentValue);
|
|
173
|
+
const nextIndex = (currentIndex + 1) % item.values.length;
|
|
174
|
+
const newValue = item.values[nextIndex];
|
|
175
|
+
item.currentValue = newValue;
|
|
176
|
+
this.onChange(item.id, newValue);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private closeSubmenu(): void {
|
|
181
|
+
this.submenuComponent = null;
|
|
182
|
+
// Restore selection to the item that opened the submenu
|
|
183
|
+
if (this.submenuItemIndex !== null) {
|
|
184
|
+
this.selectedIndex = this.submenuItemIndex;
|
|
185
|
+
this.submenuItemIndex = null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -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,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tab Bar Component
|
|
3
|
+
*
|
|
4
|
+
* A horizontal tab bar for switching between views/panels.
|
|
5
|
+
* Renders as: "Label: Tab1 Tab2 Tab3 (tab to cycle)"
|
|
6
|
+
*
|
|
7
|
+
* Navigation:
|
|
8
|
+
* - Tab / Arrow Right: Next tab (wraps around)
|
|
9
|
+
* - Shift+Tab / Arrow Left: Previous tab (wraps around)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { isArrowLeft, isArrowRight, isShiftTab, isTab } from "../keys.js";
|
|
13
|
+
import type { Component } from "../tui.js";
|
|
14
|
+
|
|
15
|
+
/** Tab definition */
|
|
16
|
+
export interface Tab {
|
|
17
|
+
/** Unique identifier for the tab */
|
|
18
|
+
id: string;
|
|
19
|
+
/** Display label shown in the tab bar */
|
|
20
|
+
label: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Theme for styling the tab bar */
|
|
24
|
+
export interface TabBarTheme {
|
|
25
|
+
/** Style for the label prefix (e.g., "Settings:") */
|
|
26
|
+
label: (text: string) => string;
|
|
27
|
+
/** Style for the currently active tab */
|
|
28
|
+
activeTab: (text: string) => string;
|
|
29
|
+
/** Style for inactive tabs */
|
|
30
|
+
inactiveTab: (text: string) => string;
|
|
31
|
+
/** Style for the hint text (e.g., "(tab to cycle)") */
|
|
32
|
+
hint: (text: string) => string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Horizontal tab bar component.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const tabs = [
|
|
41
|
+
* { id: "config", label: "Config" },
|
|
42
|
+
* { id: "tools", label: "Tools" },
|
|
43
|
+
* ];
|
|
44
|
+
* const tabBar = new TabBar("Settings", tabs, theme);
|
|
45
|
+
* tabBar.onTabChange = (tab) => console.log(`Switched to ${tab.id}`);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export class TabBar implements Component {
|
|
49
|
+
private tabs: Tab[];
|
|
50
|
+
private activeIndex: number = 0;
|
|
51
|
+
private theme: TabBarTheme;
|
|
52
|
+
private label: string;
|
|
53
|
+
|
|
54
|
+
/** Callback fired when the active tab changes */
|
|
55
|
+
public onTabChange?: (tab: Tab, index: number) => void;
|
|
56
|
+
|
|
57
|
+
constructor(label: string, tabs: Tab[], theme: TabBarTheme, initialIndex: number = 0) {
|
|
58
|
+
this.label = label;
|
|
59
|
+
this.tabs = tabs;
|
|
60
|
+
this.theme = theme;
|
|
61
|
+
this.activeIndex = initialIndex;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Get the currently active tab */
|
|
65
|
+
getActiveTab(): Tab {
|
|
66
|
+
return this.tabs[this.activeIndex];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Get the index of the currently active tab */
|
|
70
|
+
getActiveIndex(): number {
|
|
71
|
+
return this.activeIndex;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Set the active tab by index (clamped to valid range) */
|
|
75
|
+
setActiveIndex(index: number): void {
|
|
76
|
+
const newIndex = Math.max(0, Math.min(index, this.tabs.length - 1));
|
|
77
|
+
if (newIndex !== this.activeIndex) {
|
|
78
|
+
this.activeIndex = newIndex;
|
|
79
|
+
this.onTabChange?.(this.tabs[this.activeIndex], this.activeIndex);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Move to the next tab (wraps to first tab after last) */
|
|
84
|
+
nextTab(): void {
|
|
85
|
+
this.setActiveIndex((this.activeIndex + 1) % this.tabs.length);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Move to the previous tab (wraps to last tab before first) */
|
|
89
|
+
prevTab(): void {
|
|
90
|
+
this.setActiveIndex((this.activeIndex - 1 + this.tabs.length) % this.tabs.length);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
invalidate(): void {
|
|
94
|
+
// No cached state to invalidate
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Handle keyboard input for tab navigation.
|
|
99
|
+
* @returns true if the input was handled, false otherwise
|
|
100
|
+
*/
|
|
101
|
+
handleInput(data: string): boolean {
|
|
102
|
+
if (isTab(data) || isArrowRight(data)) {
|
|
103
|
+
this.nextTab();
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
if (isShiftTab(data) || isArrowLeft(data)) {
|
|
107
|
+
this.prevTab();
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Render the tab bar as a single line */
|
|
114
|
+
render(_width: number): string[] {
|
|
115
|
+
const parts: string[] = [];
|
|
116
|
+
|
|
117
|
+
// Label prefix
|
|
118
|
+
parts.push(this.theme.label(`${this.label}:`));
|
|
119
|
+
parts.push(" ");
|
|
120
|
+
|
|
121
|
+
// Tab buttons
|
|
122
|
+
for (let i = 0; i < this.tabs.length; i++) {
|
|
123
|
+
const tab = this.tabs[i];
|
|
124
|
+
if (i === this.activeIndex) {
|
|
125
|
+
parts.push(this.theme.activeTab(` ${tab.label} `));
|
|
126
|
+
} else {
|
|
127
|
+
parts.push(this.theme.inactiveTab(` ${tab.label} `));
|
|
128
|
+
}
|
|
129
|
+
if (i < this.tabs.length - 1) {
|
|
130
|
+
parts.push(" ");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Navigation hint
|
|
135
|
+
parts.push(" ");
|
|
136
|
+
parts.push(this.theme.hint("(tab to cycle)"));
|
|
137
|
+
|
|
138
|
+
return [parts.join("")];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -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
|
+
}
|