Skip to content

Commit

Permalink
feat(@clack/prompts): dynamic max items (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 authored Mar 20, 2024
2 parents 0cb17d3 + f33a56f commit e74d448
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-laws-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/prompts': patch
---

feat: adaptative max items
6 changes: 4 additions & 2 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SelectKeyPrompt,
SelectPrompt,
State,
TextPrompt
TextPrompt,
} from '@clack/core';
import isUnicodeSupported from 'is-unicode-supported';
import color from 'picocolors';
Expand Down Expand Up @@ -68,8 +68,10 @@ interface LimitOptionsParams<TOption> {
const limitOptions = <TOption>(params: LimitOptionsParams<TOption>): 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 ? Infinity : Math.max(params.maxItems, 5);
const maxItems = Math.min(outputMaxItems, Math.max(paramMaxItems, 5));
let slidingWindowLocation = 0;

if (cursor >= slidingWindowLocation + maxItems - 3) {
Expand Down

0 comments on commit e74d448

Please sign in to comment.