Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/box-wide-title-truncation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/prompts": patch
---

Fix `box()` so a title wider than the box is truncated by display width instead of UTF-16 code units. A wide-character (CJK) title that overflowed the title budget previously produced ragged borders, and could throw `RangeError: Invalid count value` once the remainder passed to `'─'.repeat(...)` went negative.
18 changes: 17 additions & 1 deletion packages/prompts/src/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ function getPaddingForLine(
return [leftPadding, rightPadding];
}

function truncateToWidth(str: string, maxWidth: number): string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should really be in common.ts since it will be useful in other places too

const ellipsis = '...';
const target = maxWidth - stringWidth(ellipsis);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ellipsis is constant so should be moved out of the function, and the length doesn't need to use stringWidth since we know it has no wide characters

let result = '';
let width = 0;
for (const char of str) {
const charWidth = stringWidth(char);
if (width + charWidth > target) {
break;
}
result += char;
width += charWidth;
Comment on lines +106 to +110

@43081j 43081j Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (width + charWidth > target) {
break;
}
result += char;
width += charWidth;
width += charWidth;
if (width > target) {
break;
}
result += char;

to avoid computing it twice

}
return result + ellipsis;
}

const defaultFormatBorder = (text: string) => text;

/**
Expand Down Expand Up @@ -162,7 +178,7 @@ export const box = (message = '', title = '', opts?: BoxOptions) => {
const innerWidth = boxWidth - borderTotalWidth;
const maxTitleLength = innerWidth - titlePadding * 2;
const truncatedTitle =
titleWidth > maxTitleLength ? `${title.slice(0, maxTitleLength - 3)}...` : title;
titleWidth > maxTitleLength ? truncateToWidth(title, maxTitleLength) : title;
const [titlePaddingLeft, titlePaddingRight] = getPaddingForLine(
stringWidth(truncatedTitle),
innerWidth,
Expand Down
27 changes: 27 additions & 0 deletions packages/prompts/test/box.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { styleText } from 'node:util';
import { updateSettings } from '@clack/core';
import stringWidth from 'fast-string-width';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import * as prompts from '../src/index.js';
import { MockReadable, MockWritable } from './test-utils.js';
Expand Down Expand Up @@ -270,4 +271,30 @@ describe.each(['true', 'false'])('box (isCI = %s)', (isCI) => {

expect(output.buffer).toMatchSnapshot();
});

test('truncates a wide-character title that overflows the box width', () => {
// '这是一个非常长的标题' is 10 wide chars = 20 display columns, which
// exceeds the title budget at width: 0.2. The title must be truncated by
// display width, not by UTF-16 code units, otherwise the trailing
// `'─'.repeat(...)` count goes negative and box() throws RangeError.
const title = '这是一个非常长的标题';
expect(() => {
prompts.box('message', title, {
input,
output,
width: 0.2,
});
}).not.toThrow();

const rendered = output.buffer.join('');
expect(rendered).toContain('...');

// Every rendered border line must occupy the same number of display
// columns; otherwise the box corners are ragged.
const lineWidths = rendered
.split('\n')
.filter((line) => line.length > 0)
.map((line) => stringWidth(line));
expect(new Set(lineWidths).size).toBe(1);
});
});
Loading