@@ -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+
751822export 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