@@ -310,25 +310,40 @@ function makeCreateTitle<Item>(columns: Columns<Item>): (item: Item) => string {
310310 } )
311311 }
312312 }
313- return ( item : Item ) : string => {
314- const separator = ' '
315- const fixedWidth = normalizedColumns . reduce (
316- ( total , c ) => total + ( c . width ?? 0 ) ,
317- separator . length * ( normalizedColumns . length - 1 )
318- )
319- const totalGrow = Math . max (
320- 1 ,
321- normalizedColumns . reduce (
322- ( total , c ) => total + ( c . grow ?? ( c . width != null ? 0 : 1 ) ) ,
323- 0
324- )
325- )
326- const remainingWidth = Math . max ( 0 , process . stdout . columns - fixedWidth - 4 )
327- const columnWidths = normalizedColumns . map (
328- ( { width, minWidth, grow = 1 } ) =>
329- width ??
330- Math . max ( minWidth ?? 0 , Math . floor ( ( remainingWidth * grow ) / totalGrow ) )
313+ const separator = ' '
314+ const availableWidth = process . stdout . columns - 4
315+ let fixedWidth = normalizedColumns . reduce (
316+ ( total , c ) =>
317+ total + Math . max ( c . minWidth ?? 0 , c . width ?? 0 ) + separator . length ,
318+ 0
319+ )
320+ let excess : number
321+ while ( ( excess = fixedWidth - availableWidth ) > 0 ) {
322+ const c = normalizedColumns [ normalizedColumns . length - 1 ]
323+ const minWidth = Math . max ( c . minWidth ?? 0 , c . width ?? 0 )
324+ if ( minWidth > excess ) {
325+ if ( c . minWidth != null ) c . minWidth = Math . max ( 0 , c . minWidth - excess )
326+ if ( c . width != null ) c . width = Math . max ( 0 , c . width - excess )
327+ break
328+ }
329+ fixedWidth -= minWidth + separator . length
330+ normalizedColumns . pop ( )
331+ }
332+
333+ const totalGrow = Math . max (
334+ 1 ,
335+ normalizedColumns . reduce (
336+ ( total , c ) => total + ( c . grow ?? ( c . width != null ? 0 : 1 ) ) ,
337+ 0
331338 )
339+ )
340+ const remainingWidth = Math . max ( 0 , availableWidth - fixedWidth )
341+ const columnWidths = normalizedColumns . map (
342+ ( { width, minWidth, grow = 1 } ) =>
343+ width ?? ( minWidth ?? 0 ) + Math . floor ( ( remainingWidth * grow ) / totalGrow )
344+ )
345+
346+ return ( item : Item ) : string => {
332347 return normalizedColumns
333348 . map ( ( c , i ) => {
334349 const width = columnWidths [ i ]
0 commit comments