-
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathrenderer-ansi.test.ts
More file actions
236 lines (205 loc) · 7.96 KB
/
Copy pathrenderer-ansi.test.ts
File metadata and controls
236 lines (205 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import {
describe,
expect,
it
} from 'vitest';
import type { RenderContext } from '../../types/RenderContext';
import {
DEFAULT_SETTINGS,
type Settings
} from '../../types/Settings';
import type { WidgetItem } from '../../types/Widget';
import {
getVisibleText,
getVisibleWidth,
stripOscCodes,
truncateStyledText
} from '../ansi';
import {
calculateMaxWidthsFromPreRendered,
preRenderAllWidgets,
renderStatusLine
} from '../renderer';
const OSC8_OPEN = '\x1b]8;;https://example.com/docs\x1b\\';
const OSC8_CLOSE = '\x1b]8;;\x1b\\';
const OSC8_OPEN_WITH_PARAMS = '\x1b]8;id=abc;https://example.com/docs\x1b\\';
const OSC8_CLOSE_WITH_PARAMS = '\x1b]8;id=abc;\x1b\\';
const BEL_OSC8_OPEN = '\x1b]8;;https://example.com/docs\x07';
const BEL_OSC8_CLOSE = '\x1b]8;;\x07';
function createSettings(overrides: Partial<Settings> = {}): Settings {
return {
...DEFAULT_SETTINGS,
flexMode: 'full',
...overrides,
powerline: {
...DEFAULT_SETTINGS.powerline,
...(overrides.powerline ?? {})
}
};
}
function renderLine(
widgets: WidgetItem[],
options: { settings?: Partial<Settings>; terminalWidth?: number } = {}
): string {
const settings = createSettings(options.settings);
const context: RenderContext = {
isPreview: false,
terminalWidth: options.terminalWidth
};
const preRenderedLines = preRenderAllWidgets([widgets], settings, context);
const preCalculatedMaxWidths = calculateMaxWidthsFromPreRendered(preRenderedLines, settings);
const preRenderedWidgets = preRenderedLines[0] ?? [];
return renderStatusLine(widgets, settings, context, preRenderedWidgets, preCalculatedMaxWidths[0] ?? []);
}
describe('renderer ANSI/OSC handling', () => {
it('treats OSC 8 wrappers as non-visible text for width calculations', () => {
const text = `A ${OSC8_OPEN}click${OSC8_CLOSE} B`;
expect(getVisibleText(text)).toBe('A click B');
expect(getVisibleWidth(text)).toBe(getVisibleWidth('A click B'));
});
it('strips OSC controls while preserving SGR styling', () => {
const text = `\x1b[31m${OSC8_OPEN}click${OSC8_CLOSE}\x1b[39m`;
expect(stripOscCodes(text)).toBe('\x1b[31mclick\x1b[39m');
});
it('treats text-presentation pictographs as narrow unless explicitly emoji-style', () => {
expect(getVisibleWidth('⚠ 2')).toBe(3);
expect(getVisibleWidth('⚠️ 2')).toBe(4);
expect(getVisibleWidth('😀 2')).toBe(4);
});
it('closes open OSC 8 hyperlinks when truncating styled text', () => {
const text = `${OSC8_OPEN}very-long-link-text${OSC8_CLOSE}`;
const truncated = truncateStyledText(text, 10, { ellipsis: true });
expect(truncated.endsWith('...')).toBe(true);
expect(truncated).toContain(OSC8_CLOSE);
expect(getVisibleWidth(truncated)).toBeLessThanOrEqual(10);
});
it('keeps OSC 8 links well-formed in normal renderer truncation', () => {
const widgets: WidgetItem[] = [
{
id: 'w1',
type: 'custom-text',
customText: `${OSC8_OPEN}very-long-link-text${OSC8_CLOSE}`
}
];
const line = renderLine(widgets, { terminalWidth: 12 });
expect(line.endsWith('...')).toBe(true);
expect(line).toContain(OSC8_CLOSE);
expect(getVisibleWidth(line)).toBeLessThanOrEqual(12);
});
it('keeps OSC 8 links well-formed in powerline truncation', () => {
const widgets: WidgetItem[] = [
{
id: 'w1',
type: 'custom-text',
customText: `${OSC8_OPEN}very-long-link-text${OSC8_CLOSE}`,
color: 'white',
backgroundColor: 'bgBlue'
},
{
id: 'w2',
type: 'custom-text',
customText: 'tail',
color: 'white',
backgroundColor: 'bgGreen'
}
];
const line = renderLine(widgets, {
terminalWidth: 16,
settings: {
powerline: {
...DEFAULT_SETTINGS.powerline,
enabled: true,
separators: ['\uE0B0'],
separatorInvertBackground: [false]
}
}
});
expect(line).toContain(OSC8_CLOSE);
expect(getVisibleWidth(line)).toBeLessThanOrEqual(16);
});
it('truncates custom-command preserveColors output without breaking OSC 8', () => {
const widget: WidgetItem = {
id: 'cmd1',
type: 'custom-command',
preserveColors: true,
maxWidth: 6
};
const settings = createSettings();
const context: RenderContext = {
isPreview: false,
terminalWidth: 200
};
const content = `${OSC8_OPEN}abcdefghij${OSC8_CLOSE}`;
const preRenderedWidgets = [{
content,
plainLength: getVisibleWidth(content),
widget
}];
const line = renderStatusLine([widget], settings, context, preRenderedWidgets, []);
expect(line).toContain(OSC8_CLOSE);
expect(getVisibleWidth(line)).toBe(6);
});
it('uses visible width for flex separator alignment when OSC 8 text is present', () => {
const widgets: WidgetItem[] = [
{
id: 'left',
type: 'custom-text',
customText: `${OSC8_OPEN}A${OSC8_CLOSE}`
},
{
id: 'flex',
type: 'flex-separator'
},
{
id: 'right',
type: 'custom-text',
customText: 'B'
}
];
const line = renderLine(widgets, { terminalWidth: 26 });
const visible = getVisibleText(line);
expect(visible.startsWith('A')).toBe(true);
expect(visible.endsWith('B')).toBe(true);
expect(visible.includes('...')).toBe(false);
expect(getVisibleWidth(line)).toBe(20);
});
it('handles BEL-terminated OSC 8 sequences during truncation', () => {
const line = `${BEL_OSC8_OPEN}bel-link-text${BEL_OSC8_CLOSE}`;
const truncated = truncateStyledText(line, 8, { ellipsis: true });
expect(truncated.endsWith('...')).toBe(true);
expect(truncated).toContain(BEL_OSC8_CLOSE);
expect(getVisibleWidth(truncated)).toBeLessThanOrEqual(8);
});
it('handles OSC 8 sequences with params during truncation', () => {
const line = `${OSC8_OPEN_WITH_PARAMS}param-link-text${OSC8_CLOSE_WITH_PARAMS}`;
const truncated = truncateStyledText(line, 8, { ellipsis: true });
expect(truncated.endsWith('...')).toBe(true);
expect(truncated).toContain(OSC8_CLOSE);
expect(getVisibleWidth(truncated)).toBeLessThanOrEqual(8);
});
});
describe('renderer minimalist mode', () => {
it('renders widget as raw value when minimalist mode is enabled', () => {
const widgets: WidgetItem[] = [{ id: 'model1', type: 'model' }];
const settings = createSettings();
const context: RenderContext = {
isPreview: true,
minimalist: true
};
const preRenderedLines = preRenderAllWidgets([widgets], settings, context);
const content = preRenderedLines[0]?.[0]?.content;
// With minimalist mode, model widget should render raw value ('Claude') not 'Model: Claude'
expect(content).toBe('Claude');
});
it('renders widget with label when minimalist mode is disabled', () => {
const widgets: WidgetItem[] = [{ id: 'model1', type: 'model' }];
const settings = createSettings();
const context: RenderContext = {
isPreview: true,
minimalist: false
};
const preRenderedLines = preRenderAllWidgets([widgets], settings, context);
const content = preRenderedLines[0]?.[0]?.content;
expect(content).toBe('Model: Claude');
});
});