@pellux/goodvibes-tui 0.20.0 → 0.20.2

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,199 @@
1
+ import type { Line } from '../types/grid.ts';
2
+ import { GLYPHS } from './ui-primitives.ts';
3
+ import {
4
+ borderLine,
5
+ clamp,
6
+ contentLine,
7
+ drawHorizontalRule,
8
+ drawVerticalRule,
9
+ fillRange,
10
+ FULLSCREEN_PALETTE,
11
+ makeLine,
12
+ writeText,
13
+ } from './fullscreen-primitives.ts';
14
+
15
+ export {
16
+ borderLine,
17
+ clamp,
18
+ contentLine,
19
+ fillRange,
20
+ makeLine,
21
+ padDisplay,
22
+ stableWindow,
23
+ writeText,
24
+ } from './fullscreen-primitives.ts';
25
+ export { FULLSCREEN_PALETTE as WORKSPACE_PALETTE } from './fullscreen-primitives.ts';
26
+
27
+ const WORKSPACE_PALETTE = FULLSCREEN_PALETTE;
28
+
29
+ export interface WorkspaceRow {
30
+ readonly text: string;
31
+ readonly selected?: boolean;
32
+ readonly bold?: boolean;
33
+ readonly dim?: boolean;
34
+ readonly fg?: string;
35
+ readonly bg?: string;
36
+ readonly kind?: 'group' | 'item' | 'more' | 'empty';
37
+ }
38
+
39
+ export interface FullscreenWorkspaceRenderOptions {
40
+ readonly width: number;
41
+ readonly height: number;
42
+ readonly title: string;
43
+ readonly stateLabel?: string;
44
+ readonly leftHeader: string;
45
+ readonly mainHeader: string;
46
+ readonly leftRows: readonly WorkspaceRow[];
47
+ readonly contextRows: readonly WorkspaceRow[];
48
+ readonly controlRows: readonly WorkspaceRow[];
49
+ readonly footer: string;
50
+ readonly leftWidth?: number;
51
+ readonly contextRatio?: number;
52
+ readonly minContextRows?: number;
53
+ }
54
+
55
+ export interface FullscreenWorkspaceMetrics {
56
+ readonly safeWidth: number;
57
+ readonly safeHeight: number;
58
+ readonly leftWidth: number;
59
+ readonly centerWidth: number;
60
+ readonly bodyRows: number;
61
+ readonly contextWidth: number;
62
+ readonly contextRows: number;
63
+ readonly controlRows: number;
64
+ }
65
+
66
+ export function drawVertical(line: Line, x: number, bg = ''): void {
67
+ if (x <= 0 || x >= line.length - 1) return;
68
+ drawVerticalRule(line, x, WORKSPACE_PALETTE.border, bg);
69
+ }
70
+
71
+ export function drawHorizontalRange(line: Line, startX: number, endX: number, bg = ''): void {
72
+ drawHorizontalRule(line, Math.max(1, startX), Math.min(line.length - 2, endX), WORKSPACE_PALETTE.border, bg);
73
+ }
74
+
75
+ function leftWidthFor(width: number, explicit?: number): number {
76
+ if (explicit !== undefined) return clamp(explicit, 14, Math.max(14, width - 24));
77
+ return width < 80
78
+ ? clamp(Math.round(width * 0.32), 14, Math.max(14, width - 24))
79
+ : clamp(Math.round(width * 0.22), 24, 34);
80
+ }
81
+
82
+ export function getFullscreenWorkspaceMetrics(options: Pick<
83
+ FullscreenWorkspaceRenderOptions,
84
+ 'width' | 'height' | 'leftWidth' | 'contextRatio' | 'minContextRows'
85
+ >): FullscreenWorkspaceMetrics {
86
+ const safeWidth = Math.max(1, options.width);
87
+ const safeHeight = Math.max(12, options.height);
88
+ const leftWidth = leftWidthFor(safeWidth, options.leftWidth);
89
+ const centerWidth = Math.max(20, safeWidth - leftWidth - 3);
90
+ const bodyTop = 3;
91
+ const footerY = safeHeight - 2;
92
+ const bodyRows = Math.max(4, footerY - bodyTop);
93
+ const contextWidth = Math.max(10, centerWidth - 2);
94
+ const maxContextRows = Math.max(3, bodyRows - 4);
95
+ const minContextRows = clamp(options.minContextRows ?? 10, 3, maxContextRows);
96
+ const contextRows = clamp(
97
+ Math.round(bodyRows * (options.contextRatio ?? 0.4)),
98
+ Math.min(minContextRows, maxContextRows),
99
+ maxContextRows,
100
+ );
101
+ const controlRows = Math.max(3, bodyRows - contextRows - 1);
102
+ return { safeWidth, safeHeight, leftWidth, centerWidth, bodyRows, contextWidth, contextRows, controlRows };
103
+ }
104
+
105
+ function rowFg(row: WorkspaceRow, fallback: string): string {
106
+ if (row.fg) return row.fg;
107
+ if (row.kind === 'group') return WORKSPACE_PALETTE.subtitle;
108
+ if (row.kind === 'more') return WORKSPACE_PALETTE.dim;
109
+ return fallback;
110
+ }
111
+
112
+ export function renderFullscreenWorkspace(options: FullscreenWorkspaceRenderOptions): Line[] {
113
+ const { safeWidth, safeHeight, leftWidth, centerWidth, bodyRows, contextWidth, contextRows } = getFullscreenWorkspaceMetrics(options);
114
+ const leftStart = 1;
115
+ const dividerX = leftWidth + 1;
116
+ const centerStart = dividerX + 1;
117
+ const centerEnd = safeWidth - 2;
118
+ const bodyTop = 3;
119
+ const separatorY = bodyTop + contextRows;
120
+ const lines: Line[] = [];
121
+
122
+ const top = borderLine(safeWidth, GLYPHS.frame.topLeft, GLYPHS.frame.horizontal, GLYPHS.frame.topRight);
123
+ writeText(top, 2, safeWidth - 4, ` ${options.title} `, { fg: WORKSPACE_PALETTE.title, bold: true });
124
+ if (options.stateLabel) {
125
+ writeText(top, Math.max(2, safeWidth - options.stateLabel.length - 4), options.stateLabel.length, options.stateLabel, {
126
+ fg: WORKSPACE_PALETTE.subtitle,
127
+ });
128
+ }
129
+ lines.push(top);
130
+
131
+ const header = contentLine(safeWidth, WORKSPACE_PALETTE.footerBg);
132
+ drawVertical(header, dividerX, WORKSPACE_PALETTE.footerBg);
133
+ writeText(header, leftStart + 1, leftWidth - 2, options.leftHeader, {
134
+ fg: WORKSPACE_PALETTE.subtitle,
135
+ bold: true,
136
+ bg: WORKSPACE_PALETTE.footerBg,
137
+ });
138
+ writeText(header, centerStart + 1, centerWidth - 2, options.mainHeader, {
139
+ fg: WORKSPACE_PALETTE.subtitle,
140
+ bold: true,
141
+ bg: WORKSPACE_PALETTE.footerBg,
142
+ });
143
+ lines.push(header);
144
+
145
+ const headerSep = contentLine(safeWidth, '');
146
+ drawVertical(headerSep, dividerX);
147
+ drawHorizontalRange(headerSep, 1, safeWidth - 2);
148
+ lines.push(headerSep);
149
+
150
+ for (let row = 0; row < bodyRows; row += 1) {
151
+ const y = bodyTop + row;
152
+ const inContext = y < separatorY;
153
+ const inSeparator = y === separatorY;
154
+ const bg = inSeparator ? '' : inContext ? WORKSPACE_PALETTE.contextBg : WORKSPACE_PALETTE.controlsBg;
155
+ const line = contentLine(safeWidth, bg);
156
+ fillRange(line, 1, dividerX - 1, WORKSPACE_PALETTE.categoryBg);
157
+ drawVertical(line, dividerX, bg);
158
+
159
+ const leftRow = options.leftRows[row] ?? { text: '', kind: 'empty' as const };
160
+ if (leftRow.selected) fillRange(line, leftStart, dividerX - 1, WORKSPACE_PALETTE.selectedBg);
161
+ writeText(line, leftStart + 1, leftWidth - 3, leftRow.text, {
162
+ fg: leftRow.selected ? WORKSPACE_PALETTE.text : rowFg(leftRow, WORKSPACE_PALETTE.muted),
163
+ bg: leftRow.selected ? WORKSPACE_PALETTE.selectedBg : WORKSPACE_PALETTE.categoryBg,
164
+ bold: leftRow.bold ?? (leftRow.selected || leftRow.kind === 'group'),
165
+ dim: leftRow.dim,
166
+ });
167
+
168
+ if (inSeparator) {
169
+ drawHorizontalRange(line, centerStart, centerEnd);
170
+ } else if (inContext) {
171
+ const contextRow = options.contextRows[row] ?? { text: '', kind: 'empty' as const };
172
+ writeText(line, centerStart + 1, contextWidth, contextRow.text, {
173
+ fg: rowFg(contextRow, WORKSPACE_PALETTE.text),
174
+ bg,
175
+ bold: contextRow.bold,
176
+ dim: contextRow.dim ?? contextRow.text.length === 0,
177
+ });
178
+ } else {
179
+ const controlRow = options.controlRows[row - contextRows - 1] ?? { text: '', kind: 'empty' as const };
180
+ if (controlRow.selected) fillRange(line, centerStart, centerEnd, WORKSPACE_PALETTE.selectedBg);
181
+ writeText(line, centerStart + 1, contextWidth, controlRow.text, {
182
+ fg: controlRow.selected ? WORKSPACE_PALETTE.text : rowFg(controlRow, WORKSPACE_PALETTE.text),
183
+ bg: controlRow.selected ? WORKSPACE_PALETTE.selectedBg : bg,
184
+ bold: controlRow.bold ?? controlRow.selected,
185
+ dim: controlRow.dim ?? controlRow.text.length === 0,
186
+ });
187
+ }
188
+
189
+ lines.push(line);
190
+ }
191
+
192
+ const footer = contentLine(safeWidth, WORKSPACE_PALETTE.footerBg);
193
+ writeText(footer, 2, safeWidth - 4, options.footer, { fg: WORKSPACE_PALETTE.muted, bg: WORKSPACE_PALETTE.footerBg });
194
+ lines.push(footer);
195
+ lines.push(borderLine(safeWidth, GLYPHS.frame.bottomLeft, GLYPHS.frame.horizontal, GLYPHS.frame.bottomRight));
196
+
197
+ while (lines.length < safeHeight) lines.unshift(makeLine(safeWidth));
198
+ return lines.slice(-safeHeight);
199
+ }