Skip to content

Commit e3172aa

Browse files
committed
fix: handle cjk in box component
1 parent e21e4b9 commit e3172aa

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

packages/prompts/src/box.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
S_CORNER_TOP_LEFT,
1515
S_CORNER_TOP_RIGHT,
1616
} from './common.js';
17+
import stringWidth from "fast-string-width";
1718

1819
export type BoxAlignment = 'left' | 'center' | 'right';
1920

@@ -72,13 +73,13 @@ export const box = (message = '', title = '', opts?: BoxOptions) => {
7273
const symbols = (opts?.rounded ? roundedSymbols : squareSymbols).map(formatBorder);
7374
const hSymbol = formatBorder(S_BAR_H);
7475
const vSymbol = formatBorder(S_BAR);
75-
const maxBoxWidth = columns - linePrefix.length;
76-
let boxWidth = Math.floor(columns * width) - linePrefix.length;
76+
const maxBoxWidth = columns - stringWidth(linePrefix);
77+
let boxWidth = Math.floor(columns * width) - stringWidth(linePrefix);
7778
if (opts?.width === 'auto') {
7879
const lines = message.split('\n');
79-
let longestLine = title.length + titlePadding * 2;
80+
let longestLine = stringWidth(title) + titlePadding * 2;
8081
for (const line of lines) {
81-
const lineWithPadding = line.length + contentPadding * 2;
82+
const lineWithPadding = stringWidth(line) + contentPadding * 2;
8283
if (lineWithPadding > longestLine) {
8384
longestLine = lineWithPadding;
8485
}
@@ -98,9 +99,9 @@ export const box = (message = '', title = '', opts?: BoxOptions) => {
9899
const innerWidth = boxWidth - borderTotalWidth;
99100
const maxTitleLength = innerWidth - titlePadding * 2;
100101
const truncatedTitle =
101-
title.length > maxTitleLength ? `${title.slice(0, maxTitleLength - 3)}...` : title;
102+
stringWidth(title) > maxTitleLength ? `${title.slice(0, maxTitleLength - 3)}...` : title;
102103
const [titlePaddingLeft, titlePaddingRight] = getPaddingForLine(
103-
truncatedTitle.length,
104+
stringWidth(truncatedTitle),
104105
innerWidth,
105106
titlePadding,
106107
opts?.titleAlign
@@ -115,7 +116,7 @@ export const box = (message = '', title = '', opts?: BoxOptions) => {
115116
const wrappedLines = wrappedMessage.split('\n');
116117
for (const line of wrappedLines) {
117118
const [leftLinePadding, rightLinePadding] = getPaddingForLine(
118-
line.length,
119+
stringWidth(line),
119120
innerWidth,
120121
contentPadding,
121122
opts?.contentAlign

0 commit comments

Comments
 (0)