Skip to content
Open
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
20 changes: 20 additions & 0 deletions packages/prompts/src/__tests__/output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ describe('output identity', () => {
}
});

it('writes the whole streamed message using the selected output and its width', async () => {
const c = capture(10);
const write = vi.spyOn(process.stdout, 'write').mockReturnValue(true);
await stream.message(['first', 'second'], { output: c.output, symbol: '!' });
expect(c.text()).toBe('! first\n second\n');
expect(write).not.toHaveBeenCalled();
});

it('keeps streamed chunks together when the output has no columns', async () => {
let content = '';
const output = new Writable({
write(chunk, _, done) {
content += chunk.toString();
done();
},
});
await stream.message(['some', 'thing', ' else'], { output, symbol: '!' });
expect(content).toBe('! something else\n');
});

it('uses one space in animated spinner frames', () => {
vi.stubEnv('CI', '');
vi.useFakeTimers();
Expand Down
19 changes: 8 additions & 11 deletions packages/prompts/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ import type { LogMessageOptions } from './log.js';

const prefix = ' ';

// TODO (43081j): this currently doesn't support custom `output` writable
// because we rely on `columns` existing (i.e. `process.stdout.columns).
//
// If we want to support `output` being passed in, we will need to use
// a condition like `if (output instance Writable)` to check if it has columns
export const stream = {
message: async (
iterable: Iterable<string> | AsyncIterable<string>,
{ symbol = '' }: LogMessageOptions = {},
{ symbol = '', output = process.stdout }: LogMessageOptions = {},
) => {
process.stdout.write(symbol ? `${symbol} ` : '');
output.write(symbol ? `${symbol} ` : '');
const initialWidth = symbol ? 2 : 0;
let lineWidth = initialWidth;
for await (let chunk of iterable) {
Expand All @@ -26,15 +21,17 @@ export const stream = {
lineWidth = initialWidth + strip(chunk.slice(chunk.lastIndexOf('\n'))).length;
}
const chunkLen = strip(chunk).length;
if (lineWidth + chunkLen < process.stdout.columns) {
const columns =
'columns' in output && typeof output.columns === 'number' ? output.columns : Infinity;
if (lineWidth + chunkLen < columns) {
lineWidth += chunkLen;
process.stdout.write(chunk);
output.write(chunk);
} else {
process.stdout.write(`\n${prefix}${chunk.trimStart()}`);
output.write(`\n${prefix}${chunk.trimStart()}`);
lineWidth = initialWidth + strip(chunk.trimStart()).length;
}
}
process.stdout.write('\n');
output.write('\n');
},
info: (iterable: Iterable<string> | AsyncIterable<string>) => {
return stream.message(iterable, { symbol: color.blue(S_INFO) });
Expand Down
Loading