Skip to content

Commit 0db777b

Browse files
committed
fix: hard wrap & strict format function
1 parent 4255673 commit 0db777b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/prompts/src/note.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process';
22
import type { Writable } from 'node:stream';
33
import { stripVTControlCharacters as strip } from 'node:util';
4-
import { wrapAnsi } from 'fast-wrap-ansi';
4+
import { wrapAnsi, type Options as WrapAnsiOptions } from 'fast-wrap-ansi';
55
import color from 'picocolors';
66
import {
77
type CommonOptions,
@@ -20,18 +20,22 @@ 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');
23+
const opts: WrapAnsiOptions = {
24+
hard: true,
25+
trim: false,
26+
};
27+
const wrapMsg = wrapAnsi(message, width, opts).split('\n');
2428
const maxWidthNormal = wrapMsg.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
2529
const maxWidthFormat = wrapMsg
2630
.map(format)
2731
.reduce((sum, ln) => Math.max(strip(ln).length, sum), 0);
2832
const wrapWidth = width - (maxWidthFormat - maxWidthNormal);
29-
return wrapAnsi(message, wrapWidth);
33+
return wrapAnsi(message, wrapWidth, opts);
3034
};
3135

3236
export const note = (message = '', title = '', opts?: NoteOptions) => {
3337
const output: Writable = opts?.output ?? process.stdout;
34-
const format = opts?.format ?? defaultNoteFormatter;
38+
const format = (ln) => opts?.format?.(ln) ?? defaultNoteFormatter(ln);
3539
const wrapMsg = wrapWithFormat(message, output.columns - 6, format);
3640
const lines = ['', ...wrapMsg.split('\n').map(format), ''];
3741
const titleLen = strip(title).length;

0 commit comments

Comments
 (0)