Skip to content

Commit aea4573

Browse files
authored
fix: hard wrap and clamp lines in limit-options (bombshell-dev#398)
1 parent 73b88da commit aea4573

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

.changeset/better-hotels-fall.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+
Clamp scrolling windows to 5 rows.

packages/prompts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"scripts": {
5151
"build": "unbuild",
5252
"prepack": "pnpm build",
53-
"test": "FORCE_COLOR=1 vitest run"
53+
"test": "vitest run"
5454
},
5555
"dependencies": {
5656
"@clack/core": "workspace:*",

packages/prompts/src/limit-options.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const limitOptions = <TOption>(params: LimitOptionsParams<TOption>): stri
4646
const paramMaxItems = params.maxItems ?? Number.POSITIVE_INFINITY;
4747
const outputMaxItems = Math.max(rows - rowPadding, 0);
4848
// We clamp to minimum 5 because anything less doesn't make sense UX wise
49-
const maxItems = Math.max(paramMaxItems, 5);
49+
const maxItems = Math.max(Math.min(paramMaxItems, outputMaxItems), 5);
5050
let slidingWindowLocation = 0;
5151

5252
if (cursor >= maxItems - 3) {
@@ -73,7 +73,10 @@ export const limitOptions = <TOption>(params: LimitOptionsParams<TOption>): stri
7373
slidingWindowLocationEnd - (shouldRenderBottomEllipsis ? 1 : 0);
7474

7575
for (let i = slidingWindowLocationWithEllipsis; i < slidingWindowLocationEndWithEllipsis; i++) {
76-
const wrappedLines = wrapAnsi(style(options[i], i === cursor), maxWidth).split('\n');
76+
const wrappedLines = wrapAnsi(style(options[i], i === cursor), maxWidth, {
77+
hard: true,
78+
trim: false,
79+
}).split('\n');
7780
lineGroups.push(wrappedLines);
7881
lineCount += wrappedLines.length;
7982
}

packages/prompts/test/limit-options.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('limitOptions', () => {
103103
output.rows = 7;
104104
options.maxItems = 10;
105105
const result = limitOptions(options);
106-
expect(result).toEqual(['Item 1', 'Item 2', 'Item 3', color.dim('...')]);
106+
expect(result).toEqual(['Item 1', 'Item 2', color.dim('...')]);
107107
});
108108

109109
test('handle multi-line item clamping (start)', async () => {

packages/prompts/vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { defineConfig } from 'vitest/config';
22

33
export default defineConfig({
44
test: {
5+
env: {
6+
FORCE_COLOR: '1',
7+
},
58
snapshotSerializers: ['vitest-ansi-serializer'],
69
},
710
});

0 commit comments

Comments
 (0)