Skip to content

Commit c718cc6

Browse files
committed
some refactor
1 parent 09608c1 commit c718cc6

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

packages/prompts/src/note.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,35 @@ export interface NoteOptions extends CommonOptions {
2020
const defaultNoteFormatter = (line: string): string => color.dim(line);
2121

2222
const wrapWithFormat = (message: string, width: number, format: NoteOptions["format"]): string => {
23-
const wrapMsg = wrapAnsi(message, width).split("\n");
24-
const maxWidthNormal = wrapMsg.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
25-
const maxWidthFormat = wrapMsg.map(format).reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
26-
const wrapWidth = width - (maxWidthFormat - maxWidthNormal);
27-
return wrapAnsi(message, wrapWidth);
23+
const wrapMsg = wrapAnsi(message, width).split("\n");
24+
const maxWidthNormal = wrapMsg.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
25+
const maxWidthFormat = wrapMsg.map(format).reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
26+
const wrapWidth = width - (maxWidthFormat - maxWidthNormal);
27+
return wrapAnsi(message, wrapWidth);
2828
}
2929

3030
export const note = (message = '', title = '', opts?: NoteOptions) => {
31-
const output: Writable = opts?.output ?? process.stdout;
32-
const format = opts?.format ?? defaultNoteFormatter;
33-
const wrapMsg = wrapWithFormat(message, output.columns - 6, format);
34-
const lines = ['', ...wrapMsg.split('\n').map(format), ''];
35-
const titleLen = strip(title).length;
36-
const len = Math.max(
37-
lines.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0),
38-
titleLen
39-
);
40-
const header = `${color.green(S_STEP_SUBMIT)} ${color.reset(title)} ${color.gray(S_BAR_H.repeat(len - titleLen + 1) + S_CORNER_TOP_RIGHT)}`;
41-
const noteLines = lines.map(
42-
(line) =>
43-
`${color.gray(S_BAR)} ${line}${' '.repeat(len - strip(line).length + 2)}${color.gray(S_BAR)}`
44-
);
45-
const footer = `${color.gray(S_CONNECT_LEFT + S_BAR_H.repeat(len + 4) + S_CORNER_BOTTOM_RIGHT)}`;
46-
output.write(`${color.gray(S_BAR)}\n${header}\n${noteLines.join('\n')}\n${footer}\n`);
31+
const output: Writable = opts?.output ?? process.stdout;
32+
const format = opts?.format ?? defaultNoteFormatter;
33+
const wrapMsg = wrapWithFormat(message, output.columns - 6, format);
34+
const lines = ['', ...wrapMsg.split('\n').map(format), ''];
35+
const titleLen = strip(title).length;
36+
const len =
37+
Math.max(
38+
lines.reduce((sum, ln) => {
39+
const line = strip(ln);
40+
return line.length > sum ? line.length : sum;
41+
}, 0),
42+
titleLen
43+
) + 2;
44+
const msg = lines
45+
.map(
46+
(ln) => `${color.gray(S_BAR)} ${ln}${' '.repeat(len - strip(ln).length)}${color.gray(S_BAR)}`
47+
)
48+
.join('\n');
49+
output.write(
50+
`${color.gray(S_BAR)}\n${color.green(S_STEP_SUBMIT)} ${color.reset(title)} ${color.gray(
51+
S_BAR_H.repeat(len - titleLen + 3) + S_CORNER_TOP_RIGHT
52+
)}\n${msg}\n${color.gray(S_CONNECT_LEFT + S_BAR_H.repeat(len + 2) + S_CORNER_BOTTOM_RIGHT)}\n`
53+
);
4754
};

0 commit comments

Comments
 (0)