Skip to content

Commit ca0fc6f

Browse files
fix: fix note box width with Chinese word #115
1 parent 593f93d commit ca0fc6f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

packages/prompts/src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ export const groupMultiselect = <Options extends Option<Value>[], Value>(
532532
};
533533

534534
const strip = (str: string) => str.replace(ansiRegex(), '');
535+
const strLength = (str: string) => {
536+
if (!str) return 0;
537+
let len = 0;
538+
const arr = [...str];
539+
for (const char of arr) {
540+
len += char.charCodeAt(0) > 127 || char.charCodeAt(0) === 94 ? 2 : 1;
541+
}
542+
return len;
543+
};
535544
export const note = (message = '', title = '') => {
536545
const lines = `\n${message}\n`.split('\n');
537546
const len =
@@ -540,19 +549,20 @@ export const note = (message = '', title = '') => {
540549
ln = strip(ln);
541550
return ln.length > sum ? ln.length : sum;
542551
}, 0),
543-
strip(title).length
552+
strLength(strip(title)),
553+
strLength(strip(message))
544554
) + 2;
545555
const msg = lines
546556
.map(
547557
(ln) =>
548-
`${color.gray(S_BAR)} ${color.dim(ln)}${' '.repeat(len - strip(ln).length)}${color.gray(
549-
S_BAR
550-
)}`
558+
`${color.gray(S_BAR)} ${color.dim(ln)}${' '.repeat(
559+
len - strLength(strip(ln))
560+
)}${color.gray(S_BAR)}`
551561
)
552562
.join('\n');
553563
process.stdout.write(
554564
`${color.gray(S_BAR)}\n${color.green(S_STEP_SUBMIT)} ${color.reset(title)} ${color.gray(
555-
S_BAR_H.repeat(Math.max(len - title.length - 1, 1)) + S_CORNER_TOP_RIGHT
565+
S_BAR_H.repeat(Math.max(len - strLength(title) - 1, 1)) + S_CORNER_TOP_RIGHT
556566
)}\n${msg}\n${color.gray(S_CONNECT_LEFT + S_BAR_H.repeat(len + 2) + S_CORNER_BOTTOM_RIGHT)}\n`
557567
);
558568
};

0 commit comments

Comments
 (0)