From 360afeb030c47768301d7b066a96bf1d9bcfa135 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Wed, 4 Oct 2023 09:02:39 -0300 Subject: [PATCH 1/2] feat: adaptative max items --- .changeset/light-laws-judge.md | 5 +++++ packages/prompts/src/index.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .changeset/light-laws-judge.md diff --git a/.changeset/light-laws-judge.md b/.changeset/light-laws-judge.md new file mode 100644 index 00000000..145c5a66 --- /dev/null +++ b/.changeset/light-laws-judge.md @@ -0,0 +1,5 @@ +--- +'@clack/prompts': patch +--- + +feat: adaptative max items diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 02e29d77..8d35bf00 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -8,7 +8,7 @@ import { SelectKeyPrompt, SelectPrompt, State, - TextPrompt + TextPrompt, } from '@clack/core'; import isUnicodeSupported from 'is-unicode-supported'; import color from 'picocolors'; @@ -69,7 +69,8 @@ const limitOptions = (params: LimitOptionsParams): string[] => const { cursor, options, style } = params; // We clamp to minimum 5 because anything less doesn't make sense UX wise - const maxItems = params.maxItems === undefined ? Infinity : Math.max(params.maxItems, 5); + const maxItems = + params.maxItems === undefined ? process.stdout.rows - 4 : Math.max(params.maxItems, 5); let slidingWindowLocation = 0; if (cursor >= slidingWindowLocation + maxItems - 3) { From f33a56f2b50291e36fb27daa8258d131ba3183f1 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Fri, 10 Nov 2023 19:09:27 -0300 Subject: [PATCH 2/2] refactor: prevent edge cases --- packages/prompts/src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 8d35bf00..097ab509 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -68,9 +68,10 @@ interface LimitOptionsParams { const limitOptions = (params: LimitOptionsParams): string[] => { const { cursor, options, style } = params; + const paramMaxItems = params.maxItems ?? Infinity; + const outputMaxItems = Math.max(process.stdout.rows - 4, 0); // We clamp to minimum 5 because anything less doesn't make sense UX wise - const maxItems = - params.maxItems === undefined ? process.stdout.rows - 4 : Math.max(params.maxItems, 5); + const maxItems = Math.min(outputMaxItems, Math.max(paramMaxItems, 5)); let slidingWindowLocation = 0; if (cursor >= slidingWindowLocation + maxItems - 3) {