Skip to content
Draft
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
25 changes: 25 additions & 0 deletions examples/basic/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { setTimeout } from 'node:timers/promises';
import * as p from '@clack/prompts';
import color from 'picocolors';

async function main() {
console.clear();

await setTimeout(1000);

p.intro(`${color.bgCyan(color.black(' create-app '))}`);

await p.log.message('Entering directory "src"');
await p.log.info('No files to update');
await p.log.warn('Directory is empty, skipping');
await p.log.warning('Directory is empty, skipping');
await p.log.error('Permission denied on file src/secret.js');
await p.log.success('Installation complete');
await p.log.step('Check files');
await p.log.step('Line 1\nLine 2');
await p.log.step(['Line 1 (array)', 'Line 2 (array)']);

p.outro(`Problems? ${color.underline(color.cyan('https://example.com/issues'))}`);
}

main().catch(console.error);
1 change: 1 addition & 0 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"jiti": "^1.17.0"
},
"scripts": {
"log": "jiti ./log.ts",
"start": "jiti ./index.ts",
"stream": "jiti ./stream.ts",
"progress": "jiti ./progress.ts",
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function main() {

p.intro(`${color.bgCyan(color.black(' create-app '))}`);

await p.stream.step(
await p.log.step(
(async function* () {
for (const line of lorem) {
for (const word of line.split(' ')) {
Expand Down
1 change: 1 addition & 0 deletions packages/prompts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"dependencies": {
"@clack/core": "workspace:*",
"@macfja/ansi": "^1.0.0",
"picocolors": "^1.0.0",
"sisteransi": "^1.0.5"
},
Expand Down
1 change: 0 additions & 1 deletion packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export * from './progress-bar.js';
export * from './select-key.js';
export * from './select.js';
export * from './spinner.js';
export * from './stream.js';
export * from './task.js';
export * from './task-log.js';
export * from './text.js';
82 changes: 49 additions & 33 deletions packages/prompts/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getColumns } from '@clack/core';
import { wrap } from '@macfja/ansi';
import color from 'picocolors';
import { cursor, erase } from 'sisteransi';
import {
type CommonOptions,
S_BAR,
Expand All @@ -13,57 +16,70 @@ export interface LogMessageOptions extends CommonOptions {
symbol?: string;
spacing?: number;
secondarySymbol?: string;
removeLeadingSpace?: boolean;
}

export const log = {
message: (
message: string | string[] = [],
message: async (
message: string | Iterable<string> | AsyncIterable<string> = [],
{
symbol = color.gray(S_BAR),
spacing = 0,
secondarySymbol = color.gray(S_BAR),
output = process.stdout,
spacing = 1,
removeLeadingSpace = true,
}: LogMessageOptions = {}
) => {
const parts: string[] = [];
for (let i = 0; i < spacing; i++) {
parts.push(`${secondarySymbol}`);
}
const messageParts = Array.isArray(message) ? message : message.split('\n');
if (messageParts.length > 0) {
const [firstLine, ...lines] = messageParts;
if (firstLine.length > 0) {
parts.push(`${symbol} ${firstLine}`);
} else {
parts.push(symbol);
output.write(`${color.gray(S_BAR)}\n`);
let first = true;
let lastLine = '';
const iterable = typeof message === 'string' ? [message] : message;
const isAsync = Symbol.asyncIterator in iterable;
for await (let chunk of iterable) {
const width = getColumns(output);
if (first) {
lastLine = `${symbol} `;
chunk = '\n'.repeat(spacing) + chunk;
first = false;
}
for (const ln of lines) {
if (ln.length > 0) {
parts.push(`${secondarySymbol} ${ln}`);
} else {
parts.push(secondarySymbol);
}
const newLineRE = removeLeadingSpace ? /\n */g : /\n/g;
const lines =
lastLine.substring(0, 3) +
wrap(`${lastLine.substring(3)}${chunk}`, width).replace(
newLineRE,
`\n${secondarySymbol} `
);
output?.write(cursor.move(-999, 0) + erase.lines(1));
output?.write(lines);
lastLine = lines.substring(Math.max(0, lines.lastIndexOf('\n') + 1));
if (!isAsync) {
lastLine = `${secondarySymbol} `;
output?.write('\n');
}
}
output.write(`${parts.join('\n')}\n`);
if (isAsync) {
output.write('\n');
}
},
info: (message: string, opts?: LogMessageOptions) => {
log.message(message, { ...opts, symbol: color.blue(S_INFO) });
info: async (message: string, opts?: LogMessageOptions) => {
await log.message(message, { ...opts, symbol: color.blue(S_INFO) });
},
success: (message: string, opts?: LogMessageOptions) => {
log.message(message, { ...opts, symbol: color.green(S_SUCCESS) });
success: async (message: string, opts?: LogMessageOptions) => {
await log.message(message, { ...opts, symbol: color.green(S_SUCCESS) });
},
step: (message: string, opts?: LogMessageOptions) => {
log.message(message, { ...opts, symbol: color.green(S_STEP_SUBMIT) });
step: async (message: string, opts?: LogMessageOptions) => {
await log.message(message, { ...opts, symbol: color.green(S_STEP_SUBMIT) });
},
warn: (message: string, opts?: LogMessageOptions) => {
log.message(message, { ...opts, symbol: color.yellow(S_WARN) });
warn: async (message: string, opts?: LogMessageOptions) => {
await log.message(message, { ...opts, symbol: color.yellow(S_WARN) });
},
/** alias for `log.warn()`. */
warning: (message: string, opts?: LogMessageOptions) => {
log.warn(message, opts);
warning: async (message: string, opts?: LogMessageOptions) => {
await log.warn(message, opts);
},
error: (message: string, opts?: LogMessageOptions) => {
log.message(message, { ...opts, symbol: color.red(S_ERROR) });
error: async (message: string, opts?: LogMessageOptions) => {
await log.message(message, { ...opts, symbol: color.red(S_ERROR) });
},
};

export const stream = log;
55 changes: 0 additions & 55 deletions packages/prompts/src/stream.ts

This file was deleted.

30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.