Skip to content

Commit 6c65113

Browse files
committed
feat(prompts): add stream API
1 parent d444439 commit 6c65113

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

packages/prompts/src/index.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,48 @@ export const log = {
675675
},
676676
};
677677

678+
const prefix = `${color.gray(S_BAR)} `;
679+
export const stream = {
680+
message: async (iterable: Iterable<string>|AsyncIterable<string>, { symbol = color.gray(S_BAR) }: LogMessageOptions = {}) => {
681+
process.stdout.write(`${color.gray(S_BAR)}\n${symbol} `);
682+
let lineWidth = 3;
683+
for await (let chunk of iterable) {
684+
chunk = chunk.replace(/\n/g, `\n${prefix}`);
685+
if (chunk.includes('\n')) {
686+
lineWidth = 3 + strip(chunk.slice(chunk.lastIndexOf('\n'))).length;
687+
}
688+
const chunkLen = strip(chunk).length;
689+
if ((lineWidth + chunkLen) < process.stdout.columns) {
690+
lineWidth += chunkLen;
691+
process.stdout.write(chunk);
692+
} else {
693+
process.stdout.write(`\n${prefix}${chunk.trimStart()}`);
694+
lineWidth = 3 + strip(chunk.trimStart()).length;
695+
}
696+
}
697+
process.stdout.write('\n');
698+
},
699+
info: (iterable: Iterable<string>|AsyncIterable<string>) => {
700+
return stream.message(iterable, { symbol: color.blue(S_INFO) });
701+
},
702+
success: (iterable: Iterable<string>|AsyncIterable<string>) => {
703+
return stream.message(iterable, { symbol: color.green(S_SUCCESS) });
704+
},
705+
step: (iterable: Iterable<string>|AsyncIterable<string>) => {
706+
return stream.message(iterable, { symbol: color.green(S_STEP_SUBMIT) });
707+
},
708+
warn: (iterable: Iterable<string>|AsyncIterable<string>) => {
709+
return stream.message(iterable, { symbol: color.yellow(S_WARN) });
710+
},
711+
/** alias for `log.warn()`. */
712+
warning: (iterable: Iterable<string>|AsyncIterable<string>) => {
713+
return stream.warn(iterable);
714+
},
715+
error: (iterable: Iterable<string>|AsyncIterable<string>) => {
716+
return stream.message(iterable, { symbol: color.red(S_ERROR) });
717+
},
718+
}
719+
678720
export const spinner = () => {
679721
const frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'];
680722
const delay = unicode ? 80 : 120;

0 commit comments

Comments
 (0)