@oh-my-pi/pi-tui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ import { isArrowDown, isArrowUp, isCtrlC, isEnter, isEscape } from "../keys";
2
+ import type { SymbolTheme } from "../symbols";
3
+ import type { Component } from "../tui";
4
+ import { truncateToWidth, visibleWidth } from "../utils";
5
+
6
+ export interface SelectItem {
7
+ value: string;
8
+ label: string;
9
+ description?: string;
10
+ }
11
+
12
+ export interface SelectListTheme {
13
+ selectedPrefix: (text: string) => string;
14
+ selectedText: (text: string) => string;
15
+ description: (text: string) => string;
16
+ scrollInfo: (text: string) => string;
17
+ noMatch: (text: string) => string;
18
+ symbols: SymbolTheme;
19
+ }
20
+
21
+ export class SelectList implements Component {
22
+ private items: SelectItem[] = [];
23
+ private filteredItems: SelectItem[] = [];
24
+ private selectedIndex: number = 0;
25
+ private maxVisible: number = 5;
26
+ private theme: SelectListTheme;
27
+
28
+ public onSelect?: (item: SelectItem) => void;
29
+ public onCancel?: () => void;
30
+ public onSelectionChange?: (item: SelectItem) => void;
31
+
32
+ constructor(items: SelectItem[], maxVisible: number, theme: SelectListTheme) {
33
+ this.items = items;
34
+ this.filteredItems = items;
35
+ this.maxVisible = maxVisible;
36
+ this.theme = theme;
37
+ }
38
+
39
+ setFilter(filter: string): void {
40
+ this.filteredItems = this.items.filter((item) => item.value.toLowerCase().startsWith(filter.toLowerCase()));
41
+ // Reset selection when filter changes
42
+ this.selectedIndex = 0;
43
+ }
44
+
45
+ setSelectedIndex(index: number): void {
46
+ this.selectedIndex = Math.max(0, Math.min(index, this.filteredItems.length - 1));
47
+ }
48
+
49
+ invalidate(): void {
50
+ // No cached state to invalidate currently
51
+ }
52
+
53
+ render(width: number): string[] {
54
+ const lines: string[] = [];
55
+
56
+ // If no items match filter, show message
57
+ if (this.filteredItems.length === 0) {
58
+ lines.push(this.theme.noMatch(" No matching commands"));
59
+ return lines;
60
+ }
61
+
62
+ // Calculate visible range with scrolling
63
+ const startIndex = Math.max(
64
+ 0,
65
+ Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible),
66
+ );
67
+ const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length);
68
+
69
+ // Render visible items
70
+ for (let i = startIndex; i < endIndex; i++) {
71
+ const item = this.filteredItems[i];
72
+ if (!item) continue;
73
+
74
+ const isSelected = i === this.selectedIndex;
75
+
76
+ let line = "";
77
+ if (isSelected) {
78
+ // Use arrow indicator for selection - entire line uses selectedText color
79
+ const prefix = `${this.theme.symbols.cursor} `;
80
+ const prefixWidth = visibleWidth(prefix);
81
+ const displayValue = item.label || item.value;
82
+
83
+ if (item.description && width > 40) {
84
+ // Calculate how much space we have for value + description
85
+ const maxValueWidth = Math.min(30, width - prefixWidth - 4);
86
+ const truncatedValue = truncateToWidth(displayValue, maxValueWidth, "");
87
+ const spacing = " ".repeat(Math.max(1, 32 - truncatedValue.length));
88
+
89
+ // Calculate remaining space for description using visible widths
90
+ const descriptionStart = prefixWidth + truncatedValue.length + spacing.length;
91
+ const remainingWidth = width - descriptionStart - 2; // -2 for safety
92
+
93
+ if (remainingWidth > 10) {
94
+ const truncatedDesc = truncateToWidth(item.description, remainingWidth, "");
95
+ // Apply selectedText to entire line content
96
+ line = this.theme.selectedText(`${prefix}${truncatedValue}${spacing}${truncatedDesc}`);
97
+ } else {
98
+ // Not enough space for description
99
+ const maxWidth = width - prefixWidth - 2;
100
+ line = this.theme.selectedText(`${prefix}${truncateToWidth(displayValue, maxWidth, "")}`);
101
+ }
102
+ } else {
103
+ // No description or not enough width
104
+ const maxWidth = width - prefixWidth - 2;
105
+ line = this.theme.selectedText(`${prefix}${truncateToWidth(displayValue, maxWidth, "")}`);
106
+ }
107
+ } else {
108
+ const displayValue = item.label || item.value;
109
+ const prefix = " ".repeat(visibleWidth(`${this.theme.symbols.cursor} `));
110
+
111
+ if (item.description && width > 40) {
112
+ // Calculate how much space we have for value + description
113
+ const maxValueWidth = Math.min(30, width - prefix.length - 4);
114
+ const truncatedValue = truncateToWidth(displayValue, maxValueWidth, "");
115
+ const spacing = " ".repeat(Math.max(1, 32 - truncatedValue.length));
116
+
117
+ // Calculate remaining space for description
118
+ const descriptionStart = prefix.length + truncatedValue.length + spacing.length;
119
+ const remainingWidth = width - descriptionStart - 2; // -2 for safety
120
+
121
+ if (remainingWidth > 10) {
122
+ const truncatedDesc = truncateToWidth(item.description, remainingWidth, "");
123
+ const descText = this.theme.description(spacing + truncatedDesc);
124
+ line = prefix + truncatedValue + descText;
125
+ } else {
126
+ // Not enough space for description
127
+ const maxWidth = width - prefix.length - 2;
128
+ line = prefix + truncateToWidth(displayValue, maxWidth, "");
129
+ }
130
+ } else {
131
+ // No description or not enough width
132
+ const maxWidth = width - prefix.length - 2;
133
+ line = prefix + truncateToWidth(displayValue, maxWidth, "");
134
+ }
135
+ }
136
+
137
+ lines.push(line);
138
+ }
139
+
140
+ // Add scroll indicators if needed
141
+ if (startIndex > 0 || endIndex < this.filteredItems.length) {
142
+ const scrollText = ` (${this.selectedIndex + 1}/${this.filteredItems.length})`;
143
+ // Truncate if too long for terminal
144
+ lines.push(this.theme.scrollInfo(truncateToWidth(scrollText, width - 2, "")));
145
+ }
146
+
147
+ return lines;
148
+ }
149
+
150
+ handleInput(keyData: string): void {
151
+ // Up arrow - wrap to bottom when at top
152
+ if (isArrowUp(keyData)) {
153
+ this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1;
154
+ this.notifySelectionChange();
155
+ }
156
+ // Down arrow - wrap to top when at bottom
157
+ else if (isArrowDown(keyData)) {
158
+ this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1;
159
+ this.notifySelectionChange();
160
+ }
161
+ // Enter
162
+ else if (isEnter(keyData)) {
163
+ const selectedItem = this.filteredItems[this.selectedIndex];
164
+ if (selectedItem && this.onSelect) {
165
+ this.onSelect(selectedItem);
166
+ }
167
+ }
168
+ // Escape or Ctrl+C
169
+ else if (isEscape(keyData) || isCtrlC(keyData)) {
170
+ if (this.onCancel) {
171
+ this.onCancel();
172
+ }
173
+ }
174
+ }
175
+
176
+ private notifySelectionChange(): void {
177
+ const selectedItem = this.filteredItems[this.selectedIndex];
178
+ if (selectedItem && this.onSelectionChange) {
179
+ this.onSelectionChange(selectedItem);
180
+ }
181
+ }
182
+
183
+ getSelectedItem(): SelectItem | null {
184
+ const item = this.filteredItems[this.selectedIndex];
185
+ return item || null;
186
+ }
187
+ }
@@ -0,0 +1,188 @@
1
+ import { isArrowDown, isArrowUp, isCtrlC, isEnter, isEscape } from "../keys";
2
+ import type { Component } from "../tui";
3
+ import { truncateToWidth, visibleWidth } from "../utils";
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";
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";
13
+ import type { Component } from "../tui";
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,110 @@
1
+ import type { Component } from "../tui";
2
+ import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils";
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
+ getText(): string {
26
+ return this.text;
27
+ }
28
+
29
+ setText(text: string): void {
30
+ this.text = text;
31
+ this.cachedText = undefined;
32
+ this.cachedWidth = undefined;
33
+ this.cachedLines = undefined;
34
+ }
35
+
36
+ setCustomBgFn(customBgFn?: (text: string) => string): void {
37
+ this.customBgFn = customBgFn;
38
+ this.cachedText = undefined;
39
+ this.cachedWidth = undefined;
40
+ this.cachedLines = undefined;
41
+ }
42
+
43
+ invalidate(): void {
44
+ this.cachedText = undefined;
45
+ this.cachedWidth = undefined;
46
+ this.cachedLines = undefined;
47
+ }
48
+
49
+ render(width: number): string[] {
50
+ // Check cache
51
+ if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
52
+ return this.cachedLines;
53
+ }
54
+
55
+ // Don't render anything if there's no actual text
56
+ if (!this.text || this.text.trim() === "") {
57
+ const result: string[] = [];
58
+ this.cachedText = this.text;
59
+ this.cachedWidth = width;
60
+ this.cachedLines = result;
61
+ return result;
62
+ }
63
+
64
+ // Replace tabs with 3 spaces
65
+ const normalizedText = this.text.replace(/\t/g, " ");
66
+
67
+ // Calculate content width (subtract left/right margins)
68
+ const contentWidth = Math.max(1, width - this.paddingX * 2);
69
+
70
+ // Wrap text (this preserves ANSI codes but does NOT pad)
71
+ const wrappedLines = wrapTextWithAnsi(normalizedText, contentWidth);
72
+
73
+ // Add margins and background to each line
74
+ const leftMargin = " ".repeat(this.paddingX);
75
+ const rightMargin = " ".repeat(this.paddingX);
76
+ const contentLines: string[] = [];
77
+
78
+ for (const line of wrappedLines) {
79
+ // Add margins
80
+ const lineWithMargins = leftMargin + line + rightMargin;
81
+
82
+ // Apply background if specified (this also pads to full width)
83
+ if (this.customBgFn) {
84
+ contentLines.push(applyBackgroundToLine(lineWithMargins, width, this.customBgFn));
85
+ } else {
86
+ // No background - just pad to width with spaces
87
+ const visibleLen = visibleWidth(lineWithMargins);
88
+ const paddingNeeded = Math.max(0, width - visibleLen);
89
+ contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
90
+ }
91
+ }
92
+
93
+ // Add top/bottom padding (empty lines)
94
+ const emptyLine = " ".repeat(width);
95
+ const emptyLines: string[] = [];
96
+ for (let i = 0; i < this.paddingY; i++) {
97
+ const line = this.customBgFn ? applyBackgroundToLine(emptyLine, width, this.customBgFn) : emptyLine;
98
+ emptyLines.push(line);
99
+ }
100
+
101
+ const result = [...emptyLines, ...contentLines, ...emptyLines];
102
+
103
+ // Update cache
104
+ this.cachedText = this.text;
105
+ this.cachedWidth = width;
106
+ this.cachedLines = result;
107
+
108
+ return result.length > 0 ? result : [""];
109
+ }
110
+ }