Skip to content

Commit 2310b43

Browse files
authored
fix: allow non-writestream writables to have columns (#394)
1 parent 9341346 commit 2310b43

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

.changeset/tough-crews-flow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clack/prompts": patch
3+
"@clack/core": patch
4+
---
5+
6+
Allow custom writables as output stream.

packages/core/src/utils/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { stdin, stdout } from 'node:process';
22
import type { Key } from 'node:readline';
33
import * as readline from 'node:readline';
44
import type { Readable, Writable } from 'node:stream';
5-
import { ReadStream, WriteStream } from 'node:tty';
5+
import { ReadStream } from 'node:tty';
66
import { cursor } from 'sisteransi';
77
import { isActionKey } from './settings.js';
88

@@ -84,8 +84,9 @@ export function block({
8484
}
8585

8686
export const getColumns = (output: Writable): number => {
87-
if (output instanceof WriteStream && output.columns) {
88-
return output.columns;
87+
const withColumns = output as Writable & { columns?: number };
88+
if ('columns' in withColumns && typeof withColumns.columns === 'number') {
89+
return withColumns.columns;
8990
}
9091
return 80;
9192
};

packages/prompts/test/note.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe.each(['true', 'false'])('note (isCI = %s)', (isCI) => {
6868
const message = `${'test string '.repeat(32)}\n`.repeat(4).trim();
6969
prompts.note(message, 'title', {
7070
input,
71-
output: Object.assign(output, { columns: 75 }),
71+
output,
7272
});
7373

7474
expect(output.buffer).toMatchSnapshot();
@@ -79,7 +79,7 @@ describe.each(['true', 'false'])('note (isCI = %s)', (isCI) => {
7979
prompts.note(message, 'title', {
8080
format: (line) => colors.red(`* ${colors.cyan(line)} *`),
8181
input,
82-
output: Object.assign(output, { columns: 75 }),
82+
output,
8383
});
8484

8585
expect(output.buffer).toMatchSnapshot();

packages/prompts/test/test-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Readable, Writable } from 'node:stream';
33
export class MockWritable extends Writable {
44
public buffer: string[] = [];
55
public isTTY = false;
6+
public columns = 80;
67

78
_write(
89
chunk: any,

0 commit comments

Comments
 (0)