Skip to content

Commit

Permalink
fix: note title correctly evaluates title length with ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC authored Apr 17, 2023
1 parent 593f93d commit ab51d29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-avocados-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/prompts': patch
---

Fixes cases where the note title length was miscalculated due to ansi characters
5 changes: 3 additions & 2 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,14 @@ export const groupMultiselect = <Options extends Option<Value>[], Value>(
const strip = (str: string) => str.replace(ansiRegex(), '');
export const note = (message = '', title = '') => {
const lines = `\n${message}\n`.split('\n');
const titleLen = strip(title).length;
const len =
Math.max(
lines.reduce((sum, ln) => {
ln = strip(ln);
return ln.length > sum ? ln.length : sum;
}, 0),
strip(title).length
titleLen
) + 2;
const msg = lines
.map(
Expand All @@ -552,7 +553,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 - titleLen - 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`
);
};
Expand Down

0 comments on commit ab51d29

Please sign in to comment.