Skip to content

Commit e21e4b9

Browse files
committed
fix: handle cjk in note component
1 parent 9536010 commit e21e4b9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

packages/prompts/src/note.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import process from 'node:process';
22
import type { Writable } from 'node:stream';
3-
import { stripVTControlCharacters as strip } from 'node:util';
43
import { getColumns } from '@clack/core';
54
import { type Options as WrapAnsiOptions, wrapAnsi } from 'fast-wrap-ansi';
65
import color from 'picocolors';
@@ -13,6 +12,7 @@ import {
1312
S_CORNER_TOP_RIGHT,
1413
S_STEP_SUBMIT,
1514
} from './common.js';
15+
import stringWidth from "fast-string-width";
1616

1717
type FormatFn = (line: string) => string;
1818
export interface NoteOptions extends CommonOptions {
@@ -27,10 +27,10 @@ const wrapWithFormat = (message: string, width: number, format: FormatFn): strin
2727
trim: false,
2828
};
2929
const wrapMsg = wrapAnsi(message, width, opts).split('\n');
30-
const maxWidthNormal = wrapMsg.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
30+
const maxWidthNormal = wrapMsg.reduce((sum, ln) => Math.max(stringWidth(ln), sum), 0);
3131
const maxWidthFormat = wrapMsg
3232
.map(format)
33-
.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
33+
.reduce((sum, ln) => Math.max(stringWidth(ln), sum), 0);
3434
const wrapWidth = width - (maxWidthFormat - maxWidthNormal);
3535
return wrapAnsi(message, wrapWidth, opts);
3636
};
@@ -40,18 +40,18 @@ export const note = (message = '', title = '', opts?: NoteOptions) => {
4040
const format = opts?.format ?? defaultNoteFormatter;
4141
const wrapMsg = wrapWithFormat(message, getColumns(output) - 6, format);
4242
const lines = ['', ...wrapMsg.split('\n').map(format), ''];
43-
const titleLen = strip(title).length;
43+
const titleLen = stringWidth(title);
4444
const len =
4545
Math.max(
4646
lines.reduce((sum, ln) => {
47-
const line = strip(ln);
48-
return line.length > sum ? line.length : sum;
47+
const width = stringWidth(ln);
48+
return width > sum ? width : sum;
4949
}, 0),
5050
titleLen
5151
) + 2;
5252
const msg = lines
5353
.map(
54-
(ln) => `${color.gray(S_BAR)} ${ln}${' '.repeat(len - strip(ln).length)}${color.gray(S_BAR)}`
54+
(ln) => `${color.gray(S_BAR)} ${ln}${' '.repeat(len - stringWidth(ln))}${color.gray(S_BAR)}`
5555
)
5656
.join('\n');
5757
output.write(

0 commit comments

Comments
 (0)