From 0f1e525c536e8972f2116df016b76bc32df9d8f2 Mon Sep 17 00:00:00 2001 From: Bartosz Kaszubowski Date: Wed, 4 Feb 2026 12:17:24 +0100 Subject: [PATCH] chore(prompts): destruct `limitOption` param for better code readability --- .changeset/cool-parrots-throw.md | 5 ++++ packages/prompts/src/limit-options.ts | 34 ++++++++++++++------------- 2 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 .changeset/cool-parrots-throw.md diff --git a/.changeset/cool-parrots-throw.md b/.changeset/cool-parrots-throw.md new file mode 100644 index 00000000..e76a9a79 --- /dev/null +++ b/.changeset/cool-parrots-throw.md @@ -0,0 +1,5 @@ +--- +"@clack/prompts": patch +--- + +destruct `limitOption` param for better code readability, tweak types definitions diff --git a/packages/prompts/src/limit-options.ts b/packages/prompts/src/limit-options.ts index 11d8f6d1..8b602c57 100644 --- a/packages/prompts/src/limit-options.ts +++ b/packages/prompts/src/limit-options.ts @@ -1,4 +1,3 @@ -import type { Writable } from 'node:stream'; import { getColumns, getRows } from '@clack/core'; import { wrapAnsi } from 'fast-wrap-ansi'; import color from 'picocolors'; @@ -6,15 +5,15 @@ import type { CommonOptions } from './common.js'; export interface LimitOptionsParams extends CommonOptions { options: TOption[]; - maxItems: number | undefined; cursor: number; style: (option: TOption, active: boolean) => string; + maxItems?: number; columnPadding?: number; rowPadding?: number; } const trimLines = ( - groups: Array, + groups: string[][], initialLineCount: number, startIndex: number, endIndex: number, @@ -33,32 +32,35 @@ const trimLines = ( return { lineCount, removals }; }; -export const limitOptions = (params: LimitOptionsParams): string[] => { - const { cursor, options, style } = params; - const output: Writable = params.output ?? process.stdout; +export const limitOptions = ({ + cursor, + options, + style, + output = process.stdout, + maxItems = Number.POSITIVE_INFINITY, + columnPadding = 0, + rowPadding = 4 +}: LimitOptionsParams): string[] => { const columns = getColumns(output); - const columnPadding = params.columnPadding ?? 0; - const rowPadding = params.rowPadding ?? 4; const maxWidth = columns - columnPadding; const rows = getRows(output); const overflowFormat = color.dim('...'); - const paramMaxItems = params.maxItems ?? Number.POSITIVE_INFINITY; const outputMaxItems = Math.max(rows - rowPadding, 0); // We clamp to minimum 5 because anything less doesn't make sense UX wise - const maxItems = Math.max(Math.min(paramMaxItems, outputMaxItems), 5); + const computedMaxItems = Math.max(Math.min(maxItems, outputMaxItems), 5); let slidingWindowLocation = 0; - if (cursor >= maxItems - 3) { - slidingWindowLocation = Math.max(Math.min(cursor - maxItems + 3, options.length - maxItems), 0); + if (cursor >= computedMaxItems - 3) { + slidingWindowLocation = Math.max(Math.min(cursor - computedMaxItems + 3, options.length - computedMaxItems), 0); } - let shouldRenderTopEllipsis = maxItems < options.length && slidingWindowLocation > 0; + let shouldRenderTopEllipsis = computedMaxItems < options.length && slidingWindowLocation > 0; let shouldRenderBottomEllipsis = - maxItems < options.length && slidingWindowLocation + maxItems < options.length; + computedMaxItems < options.length && slidingWindowLocation + computedMaxItems < options.length; - const slidingWindowLocationEnd = Math.min(slidingWindowLocation + maxItems, options.length); - const lineGroups: Array = []; + const slidingWindowLocationEnd = Math.min(slidingWindowLocation + computedMaxItems, options.length); + const lineGroups: string[][] = []; let lineCount = 0; if (shouldRenderTopEllipsis) { lineCount++;