Skip to content

Commit c3666e2

Browse files
Simek43081j
andauthored
chore(prompts): destruct limitOption param for better code readability (#457)
Co-authored-by: James Garbutt <43081j@users.noreply.github.com>
1 parent 667572b commit c3666e2

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

.changeset/cool-parrots-throw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": patch
3+
---
4+
5+
destruct `limitOption` param for better code readability, tweak types definitions

packages/prompts/src/limit-options.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import type { Writable } from 'node:stream';
21
import { getColumns, getRows } from '@clack/core';
32
import { wrapAnsi } from 'fast-wrap-ansi';
43
import color from 'picocolors';
54
import type { CommonOptions } from './common.js';
65

76
export 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

Comments
 (0)