···
1
1
+
import process from 'node:process';
1
2
import type { Writable } from 'node:stream';
2
3
import { stripVTControlCharacters as strip } from 'node:util';
4
4
+
import { wrapAnsi } from 'fast-wrap-ansi';
3
5
import color from 'picocolors';
4
6
import {
5
7
type CommonOptions,
···
10
12
S_CORNER_TOP_RIGHT,
11
13
S_STEP_SUBMIT,
12
14
} from './common.js';
13
13
-
import { wrapAnsi } from "fast-wrap-ansi";
14
14
-
import process from "node:process";
15
15
16
16
export interface NoteOptions extends CommonOptions {
17
17
format?: (line: string) => string;
···
19
19
20
20
const defaultNoteFormatter = (line: string): string => color.dim(line);
21
21
22
22
-
const wrapWithFormat = (message: string, width: number, format: NoteOptions["format"]): string => {
23
23
-
const wrapMsg = wrapAnsi(message, width).split("\n");
22
22
+
const wrapWithFormat = (message: string, width: number, format: NoteOptions['format']): string => {
23
23
+
const wrapMsg = wrapAnsi(message, width).split('\n');
24
24
const maxWidthNormal = wrapMsg.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
25
25
-
const maxWidthFormat = wrapMsg.map(format).reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
25
25
+
const maxWidthFormat = wrapMsg
26
26
+
.map(format)
27
27
+
.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
26
28
const wrapWidth = width - (maxWidthFormat - maxWidthNormal);
27
29
return wrapAnsi(message, wrapWidth);
28
28
-
}
30
30
+
};
29
31
30
32
export const note = (message = '', title = '', opts?: NoteOptions) => {
31
33
const output: Writable = opts?.output ?? process.stdout;
···
64
64
expect(output.buffer).toMatchSnapshot();
65
65
});
66
66
67
67
-
test('don\'t overflow', () => {
68
68
-
const input = `${'test string '.repeat(32)}\n`.repeat(4).trim();
67
67
+
test("don't overflow", () => {
68
68
+
const input = `${'test string '.repeat(32)}\n`.repeat(4).trim();
69
69
prompts.note(input, 'title', {
70
70
input,
71
71
output: Object.assign(output, { columns: 75 }),
···
74
74
expect(output.buffer).toMatchSnapshot();
75
75
});
76
76
77
77
-
test('don\'t overflow with formatter', () => {
78
78
-
const input = `${'test string '.repeat(32)}\n`.repeat(4).trim();
77
77
+
test("don't overflow with formatter", () => {
78
78
+
const input = `${'test string '.repeat(32)}\n`.repeat(4).trim();
79
79
prompts.note(input, 'title', {
80
80
-
format: (line) => colors.red(`* ${colors.cyan(line)} *`),
80
80
+
format: (line) => colors.red(`* ${colors.cyan(line)} *`),
81
81
input,
82
82
output: Object.assign(output, { columns: 75 }),
83
83
});