From bf2af4f13f75e4d0544532bb147b238878fbe445 Mon Sep 17 00:00:00 2001 From: zouhang Date: Tue, 11 Apr 2023 16:06:26 +0800 Subject: [PATCH] fix: fix note box width with Chinese word #115 --- packages/prompts/src/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 3b342dd5..f813f536 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -532,6 +532,14 @@ export const groupMultiselect = [], Value>( }; const strip = (str: string) => str.replace(ansiRegex(), ''); +const strLength = (str: string) => { + let len = 0; + const arr = [...str]; + for (const char of arr) { + len += char.charCodeAt(0) > 127 || char.charCodeAt(0) === 94 ? 2 : 1; + } + return len; +}; export const note = (message = '', title = '') => { const lines = `\n${message}\n`.split('\n'); const len = @@ -552,7 +560,7 @@ export const note = (message = '', title = '') => { .join('\n'); process.stdout.write( `${color.gray(S_BAR)}\n${color.green(S_STEP_SUBMIT)} ${color.reset(title)} ${color.gray( - S_BAR_H.repeat(Math.max(len - title.length - 1, 1)) + S_CORNER_TOP_RIGHT + S_BAR_H.repeat(Math.max(len - strLength(title) - 1, 1)) + S_CORNER_TOP_RIGHT )}\n${msg}\n${color.gray(S_CONNECT_LEFT + S_BAR_H.repeat(len + 2) + S_CORNER_BOTTOM_RIGHT)}\n` ); };