1- import type { Writable } from 'node:stream' ;
21import { getColumns , getRows } from '@clack/core' ;
32import { wrapAnsi } from 'fast-wrap-ansi' ;
43import color from 'picocolors' ;
54import type { CommonOptions } from './common.js' ;
65
76export interface LimitOptionsParams < TOption > extends CommonOptions {
87 options : TOption [ ] ;
9- maxItems : number | undefined ;
108 cursor : number ;
119 style : ( option : TOption , active : boolean ) => string ;
10+ maxItems ?: number ;
1211 columnPadding ?: number ;
1312 rowPadding ?: number ;
1413}
@@ -33,31 +32,34 @@ const trimLines = (
3332 return { lineCount, removals } ;
3433} ;
3534
36- export const limitOptions = < TOption > ( params : LimitOptionsParams < TOption > ) : string [ ] => {
37- const { cursor, options, style } = params ;
38- const output : Writable = params . output ?? process . stdout ;
35+ export const limitOptions = < TOption > ( {
36+ cursor,
37+ options,
38+ style,
39+ output = process . stdout ,
40+ maxItems = Number . POSITIVE_INFINITY ,
41+ columnPadding = 0 ,
42+ rowPadding = 4
43+ } : LimitOptionsParams < TOption > ) : string [ ] => {
3944 const columns = getColumns ( output ) ;
40- const columnPadding = params . columnPadding ?? 0 ;
41- const rowPadding = params . rowPadding ?? 4 ;
4245 const maxWidth = columns - columnPadding ;
4346 const rows = getRows ( output ) ;
4447 const overflowFormat = color . dim ( '...' ) ;
4548
46- const paramMaxItems = params . maxItems ?? Number . POSITIVE_INFINITY ;
4749 const outputMaxItems = Math . max ( rows - rowPadding , 0 ) ;
4850 // We clamp to minimum 5 because anything less doesn't make sense UX wise
49- const maxItems = Math . max ( Math . min ( paramMaxItems , outputMaxItems ) , 5 ) ;
51+ const computedMaxItems = Math . max ( Math . min ( maxItems , outputMaxItems ) , 5 ) ;
5052 let slidingWindowLocation = 0 ;
5153
52- if ( cursor >= maxItems - 3 ) {
53- slidingWindowLocation = Math . max ( Math . min ( cursor - maxItems + 3 , options . length - maxItems ) , 0 ) ;
54+ if ( cursor >= computedMaxItems - 3 ) {
55+ slidingWindowLocation = Math . max ( Math . min ( cursor - computedMaxItems + 3 , options . length - computedMaxItems ) , 0 ) ;
5456 }
5557
56- let shouldRenderTopEllipsis = maxItems < options . length && slidingWindowLocation > 0 ;
58+ let shouldRenderTopEllipsis = computedMaxItems < options . length && slidingWindowLocation > 0 ;
5759 let shouldRenderBottomEllipsis =
58- maxItems < options . length && slidingWindowLocation + maxItems < options . length ;
60+ computedMaxItems < options . length && slidingWindowLocation + computedMaxItems < options . length ;
5961
60- const slidingWindowLocationEnd = Math . min ( slidingWindowLocation + maxItems , options . length ) ;
62+ const slidingWindowLocationEnd = Math . min ( slidingWindowLocation + computedMaxItems , options . length ) ;
6163 const lineGroups : Array < string [ ] > = [ ] ;
6264 let lineCount = 0 ;
6365 if ( shouldRenderTopEllipsis ) {
0 commit comments