Skip to content

Commit b037e8b

Browse files
committed
fix(renderer): keep the strongest separator when a widget renders empty
A separator run around a hidden widget collapsed to its leftmost member, so a spacer that bound the hidden widget to its neighbour survived while the group boundary next to it was dropped. Losing the session reset timer turned "usage | weekly" into "usage weekly".
1 parent c208dd3 commit b037e8b

2 files changed

Lines changed: 103 additions & 17 deletions

File tree

‎src/utils/__tests__/renderer-separator-collapse.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ function render(
4949

5050
const T = (id: string): WidgetItem => ({ id, type: 'custom-text' });
5151
const SEP: WidgetItem = { id: 'sep', type: 'separator' };
52+
const SPACE_SEP: WidgetItem = { id: 'space-sep', type: 'separator', character: ' ' };
53+
54+
const ANSI_SGR = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, 'g');
55+
const stripAnsi = (text: string): string => text.replace(ANSI_SGR, '');
5256

5357
describe('renderer separator collapse around empty widgets', () => {
5458
it('emits exactly one separator between content widgets (regression baseline)', () => {
@@ -96,6 +100,29 @@ describe('renderer separator collapse around empty widgets', () => {
96100
expect(out).toContain('A');
97101
});
98102

103+
it('keeps the group boundary when a spacer-bound widget renders empty', () => {
104+
// Layout: [usage, ' ', reset-timer(empty), '|', weekly] — the spacer bound
105+
// the reset timer to the usage percent, so the surviving neighbours must
106+
// still be separated by the group boundary rather than by the spacer.
107+
const widgets = [T('usage'), SPACE_SEP, T('reset'), SEP, T('weekly')];
108+
const out = render(widgets, { 0: '2%', 2: '', 4: 'M 12%' });
109+
expect(stripAnsi(out)).toContain('2% | M 12%');
110+
});
111+
112+
it('keeps the group boundary when the boundary precedes the empty widget', () => {
113+
// Mirror of the case above: [usage, '|', reset-timer(empty), ' ', weekly].
114+
const widgets = [T('usage'), SEP, T('reset'), SPACE_SEP, T('weekly')];
115+
const out = render(widgets, { 0: '2%', 2: '', 4: 'M 12%' });
116+
expect(stripAnsi(out)).toContain('2% | M 12%');
117+
});
118+
119+
it('keeps a spacer when every separator around the empty widget is a spacer', () => {
120+
const widgets = [T('a'), SPACE_SEP, T('b'), SPACE_SEP, T('c')];
121+
const out = render(widgets, { 0: 'A', 2: '', 4: 'C' });
122+
expect(out).not.toMatch(/\|/);
123+
expect(stripAnsi(out)).toContain('A C');
124+
});
125+
99126
it('keeps separators between widgets that all rendered content', () => {
100127
const widgets = [T('a'), SEP, T('b'), SEP, T('c')];
101128
const out = render(widgets, { 0: 'A', 2: 'B', 4: 'C' });

‎src/utils/renderer.ts‎

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,77 @@ function formatSeparator(sep: string): string {
748748
return sep;
749749
}
750750

751+
// Decide up front which separators actually emit.
752+
//
753+
// A separator belongs to the widget on its left, so a run of separators only
754+
// emits when the last rendered widget is immediately followed by one — that is
755+
// what lets a widget merge across a neighbour that rendered empty.
756+
//
757+
// Widgets that rendered empty inside such a run collapse away and the run emits
758+
// a single separator. The survivor is the strongest one in the run (any visible
759+
// character beats a whitespace-only spacer), so losing a widget that a spacer
760+
// bound to its neighbour does not downgrade the group boundary next to it: with
761+
// `usage · spacer · reset-timer · "|" · weekly`, a hidden reset timer still
762+
// leaves `usage | weekly` rather than gluing the two groups with a space.
763+
function resolveEmittedSeparators(
764+
widgets: WidgetItem[],
765+
preRenderedWidgets: PreRenderedWidget[],
766+
defaultSeparator: string
767+
): Set<number> {
768+
const emitted = new Set<number>();
769+
const isVisible = (index: number): boolean => {
770+
const character = widgets[index]?.character ?? defaultSeparator;
771+
return /\S/.test(character);
772+
};
773+
774+
let run: number[] = [];
775+
let runAnchored = false;
776+
777+
const flushRun = () => {
778+
if (runAnchored && run.length > 0) {
779+
const survivor = run.find(isVisible) ?? run[0];
780+
if (survivor !== undefined)
781+
emitted.add(survivor);
782+
}
783+
run = [];
784+
};
785+
786+
for (let i = 0; i < widgets.length; i++) {
787+
const widget = widgets[i];
788+
if (!widget)
789+
continue;
790+
791+
// A flex-separator always emits and splits the run, but it does not
792+
// break the anchor: the segments on both sides of it keep emitting.
793+
if (widget.type === 'flex-separator') {
794+
flushRun();
795+
continue;
796+
}
797+
798+
if (widget.type === 'separator') {
799+
run.push(i);
800+
continue;
801+
}
802+
803+
if (!preRenderedWidgets[i]?.content) {
804+
// An empty widget that no separator has reached yet ends the anchor,
805+
// keeping the merge-across-empty-neighbour behavior intact.
806+
if (run.length === 0)
807+
runAnchored = false;
808+
continue;
809+
}
810+
811+
flushRun();
812+
runAnchored = true;
813+
}
814+
815+
// Trailing separators are stripped later; flushing here keeps the decision
816+
// in one place.
817+
flushRun();
818+
819+
return emitted;
820+
}
821+
751822
export interface RenderResult {
752823
line: string;
753824
wasTruncated: boolean;
@@ -1012,6 +1083,7 @@ export function renderStatusLine(
10121083

10131084
const elements: { content: string; type: string; widget?: WidgetItem }[] = [];
10141085
let hasFlexSeparator = false;
1086+
const emittedSeparators = resolveEmittedSeparators(widgets, preRenderedWidgets, settings.defaultSeparator ?? '|');
10151087

10161088
// Build elements based on configured widgets
10171089
for (let i = 0; i < widgets.length; i++) {
@@ -1021,23 +1093,10 @@ export function renderStatusLine(
10211093

10221094
// Handle separators specially (they're not widgets)
10231095
if (widget.type === 'separator') {
1024-
// Look backwards to the immediately-prior non-separator widget and
1025-
// emit this separator only if that widget actually rendered content.
1026-
// This collapses separators around hide-capable widgets that rendered
1027-
// empty (e.g., git-changes with no changes, conditional widgets with
1028-
// hide-when-zero semantics) and also suppresses a leading separator
1029-
// when no prior widget has rendered.
1030-
let hasContentBefore = false;
1031-
for (let j = i - 1; j >= 0; j--) {
1032-
const prevWidget = widgets[j];
1033-
if (!prevWidget)
1034-
continue;
1035-
if (prevWidget.type === 'separator' || prevWidget.type === 'flex-separator')
1036-
continue;
1037-
hasContentBefore = Boolean(preRenderedWidgets[j]?.content);
1038-
break;
1039-
}
1040-
if (!hasContentBefore)
1096+
// Separators around hide-capable widgets that rendered empty (e.g.
1097+
// git-changes with no changes, conditional widgets with hide-when-zero
1098+
// semantics) collapse into one; see resolveEmittedSeparators.
1099+
if (!emittedSeparators.has(i))
10411100
continue;
10421101

10431102
const sepChar = widget.character ?? (settings.defaultSeparator ?? '|');

0 commit comments

Comments
 (0)