Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-outputs-wrap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/core': patch
---

Use the configured output stream's width when wrapping and clearing prompt frames, including prompts written to stderr while stdout is redirected.
5 changes: 3 additions & 2 deletions packages/core/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Action } from '../utils/index.js';
import {
CANCEL_SYMBOL,
diffLines,
getColumns,
getRows,
isAccessible,
isActionKey,
Expand Down Expand Up @@ -303,13 +304,13 @@ export default class Prompt<TValue> {

private restoreCursor() {
const lines =
wrapAnsi(this._prevFrame, process.stdout.columns, { hard: true, trim: false }).split('\n')
wrapAnsi(this._prevFrame, getColumns(this.output), { hard: true, trim: false }).split('\n')
.length - 1;
this.output.write(cursor.move(-999, lines * -1));
}

private render() {
const frame = wrapAnsi(this._render(this) ?? '', process.stdout.columns, {
const frame = wrapAnsi(this._render(this) ?? '', getColumns(this.output), {
hard: true,
trim: false,
});
Expand Down
29 changes: 28 additions & 1 deletion packages/core/test/prompts/prompt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type } from 'arktype';
import { cursor } from 'sisteransi';
import { cursor, erase } from 'sisteransi';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { default as Prompt } from '../../src/prompts/prompt.js';
import { isCancel, settings } from '../../src/utils/index.js';
Expand Down Expand Up @@ -91,6 +91,33 @@ describe('Prompt', () => {
expect(renderFn).toHaveBeenCalledTimes(2);
});

test.each([
{ columns: 4, text: 'abcdefgh', wrapped: 'abcd\nefgh' },
{ columns: 7, text: 'abcdefghijklmnop', wrapped: 'abcdefg\nhijklmn\nop' },
])(
'wraps and clears frames using the output width ($columns columns)',
async ({ columns, text, wrapped }) => {
Object.assign(output, { columns });
const render = vi.fn(() => text);
const instance = new Prompt({ input, output, render });
const result = instance.prompt();

expect(output.buffer).to.deep.equal([cursor.hide, wrapped]);

output.buffer = [];
render.mockReturnValue('x');
output.emit('resize');

expect(output.buffer).to.deep.equal([
cursor.move(-999, -(wrapped.split('\n').length - 1)),
erase.down(),
'x',
]);
input.emit('keypress', '\x03', { name: 'c' });
await result;
}
);

test('state is active after first render', async () => {
const instance = new Prompt({
input,
Expand Down
54 changes: 30 additions & 24 deletions packages/prompts/test/__snapshots__/multi-select.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -871,15 +871,16 @@ exports[`multiselect (isCI = false) > wraps cancelled state with long options 1`
│ 1 Option 1 Option 1 Option 
│ 1 Option 1 Option 1 Option 
│ 1 Option 1
│ ↑/↓ to navigate • Space: select • Enter: confirm
│ ↑/↓ to navigate • Space: select •
Enter: confirm
└
",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=2>",
"<erase.line><cursor.left count=1>",
"│ ◼ Option 0 Option 0 Option ",
"<cursor.down count=10>",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.down count=11>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=1>",
"<erase.down>",
"■ foo
Expand All @@ -903,15 +904,16 @@ exports[`multiselect (isCI = false) > wraps long messages 1`] = `
│ foo foo foo foo foo foo foo
│ ◻ opt0
│ ◻ opt1
│ ↑/↓ to navigate • Space: select • Enter: confirm
│ ↑/↓ to navigate • Space: select •
Enter: confirm
└
",
"<cursor.backward count=999><cursor.up count=8>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=4>",
"<erase.line><cursor.left count=1>",
"│ ◼ opt0",
"<cursor.down count=4>",
"<cursor.backward count=999><cursor.up count=8>",
"<cursor.down count=5>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo foo foo foo foo foo foo
Expand All @@ -937,15 +939,16 @@ exports[`multiselect (isCI = false) > wraps success state with long options 1`]
│ 1 Option 1 Option 1 Option 
│ 1 Option 1 Option 1 Option 
│ 1 Option 1
│ ↑/↓ to navigate • Space: select • Enter: confirm
│ ↑/↓ to navigate • Space: select •
Enter: confirm
└
",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=2>",
"<erase.line><cursor.left count=1>",
"│ ◼ Option 0 Option 0 Option ",
"<cursor.down count=10>",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.down count=11>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
Expand Down Expand Up @@ -1830,15 +1833,16 @@ exports[`multiselect (isCI = true) > wraps cancelled state with long options 1`]
│ 1 Option 1 Option 1 Option 
│ 1 Option 1 Option 1 Option 
│ 1 Option 1
│ ↑/↓ to navigate • Space: select • Enter: confirm
│ ↑/↓ to navigate • Space: select •
Enter: confirm
└
",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=2>",
"<erase.line><cursor.left count=1>",
"│ ◼ Option 0 Option 0 Option ",
"<cursor.down count=10>",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.down count=11>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=1>",
"<erase.down>",
"■ foo
Expand All @@ -1862,15 +1866,16 @@ exports[`multiselect (isCI = true) > wraps long messages 1`] = `
│ foo foo foo foo foo foo foo
│ ◻ opt0
│ ◻ opt1
│ ↑/↓ to navigate • Space: select • Enter: confirm
│ ↑/↓ to navigate • Space: select •
Enter: confirm
└
",
"<cursor.backward count=999><cursor.up count=8>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=4>",
"<erase.line><cursor.left count=1>",
"│ ◼ opt0",
"<cursor.down count=4>",
"<cursor.backward count=999><cursor.up count=8>",
"<cursor.down count=5>",
"<cursor.backward count=999><cursor.up count=9>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo foo foo foo foo foo foo
Expand All @@ -1896,15 +1901,16 @@ exports[`multiselect (isCI = true) > wraps success state with long options 1`] =
│ 1 Option 1 Option 1 Option 
│ 1 Option 1 Option 1 Option 
│ 1 Option 1
│ ↑/↓ to navigate • Space: select • Enter: confirm
│ ↑/↓ to navigate • Space: select •
Enter: confirm
└
",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=2>",
"<erase.line><cursor.left count=1>",
"│ ◼ Option 0 Option 0 Option ",
"<cursor.down count=10>",
"<cursor.backward count=999><cursor.up count=12>",
"<cursor.down count=11>",
"<cursor.backward count=999><cursor.up count=13>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
Expand Down
46 changes: 26 additions & 20 deletions packages/prompts/test/__snapshots__/select.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,41 @@ exports[`select (isCI = false) > correctly limits options when message wraps to
│ ● Option 0
│ ○ Option 1
│ ...
│ ↑/↓ to navigate • Enter: confirm
│ ↑/↓ to navigate • Enter:
confirm
└
",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=5>",
"<erase.down>",
"│ ○ Option 0
│ ● Option 1
│ ...
│ ↑/↓ to navigate • Enter: confirm
│ ↑/↓ to navigate • Enter:
confirm
└
",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=5>",
"<erase.down>",
"│ ...
│ ● Option 2
│ ...
│ ↑/↓ to navigate • Enter: confirm
│ ↑/↓ to navigate • Enter:
confirm
└
",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=6>",
"<erase.line><cursor.left count=1>",
"│ ● Option 3",
"<cursor.down count=4>",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=5>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=6>",
"<erase.line><cursor.left count=1>",
"│ ● Option 4",
"<cursor.down count=4>",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=5>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=1>",
"<erase.down>",
"◇ This is a very
Expand Down Expand Up @@ -669,38 +672,41 @@ exports[`select (isCI = true) > correctly limits options when message wraps to m
│ ● Option 0
│ ○ Option 1
│ ...
│ ↑/↓ to navigate • Enter: confirm
│ ↑/↓ to navigate • Enter:
confirm
└
",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=5>",
"<erase.down>",
"│ ○ Option 0
│ ● Option 1
│ ...
│ ↑/↓ to navigate • Enter: confirm
│ ↑/↓ to navigate • Enter:
confirm
└
",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=5>",
"<erase.down>",
"│ ...
│ ● Option 2
│ ...
│ ↑/↓ to navigate • Enter: confirm
│ ↑/↓ to navigate • Enter:
confirm
└
",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=6>",
"<erase.line><cursor.left count=1>",
"│ ● Option 3",
"<cursor.down count=4>",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=5>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=6>",
"<erase.line><cursor.left count=1>",
"│ ● Option 4",
"<cursor.down count=4>",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=5>",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=1>",
"<erase.down>",
"◇ This is a very
Expand Down
Loading