Skip to content

Commit

Permalink
fix(tasks): prefix stdout for progress, prompt, select
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 15, 2021
1 parent d6a30d3 commit 0e25098
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/tasks/stdio/progress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TypeGuard } from 'type-core';
import { Empty, TypeGuard } from 'type-core';
import isUnicodeSupported from 'is-unicode-supported';
import ora from 'ora';
import { Task } from '../../definitions';
import { addPrefix } from '../../helpers/prefix';
import { stringifyPrintRoute } from '../../helpers/stringify';
import { getLogLevelPrefix, isLogLevelActive } from '../../helpers/logging';
import { isInteractive } from '../../utils/is-interactive';
Expand All @@ -26,7 +27,10 @@ export interface ProgressOptions {
* while maintaining the context's stdout and stderr.
* @returns Task
*/
export function progress(task: Task, options?: ProgressOptions): Task.Async {
export function progress(
options: ProgressOptions | Empty,
task: Task
): Task.Async {
return create(async (ctx) => {
const opts = options || {};
const silent = silence(task);
Expand All @@ -40,12 +44,19 @@ export function progress(task: Task, options?: ProgressOptions): Task.Async {
}

const spinner = ora({
color: 'cyan',
color: undefined,
indent: 0,
stream: ctx.stdio[1],
isEnabled: true,
discardStdin: false,
hideCursor: true
hideCursor: true,
spinner: {
frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'].map(
(str) => {
return addPrefix(style(str, { color: 'cyan' }), null, 'print', ctx);
}
)
}
});

spinner.start(isUnicodeSupported() ? ' ' + message : message);
Expand All @@ -56,13 +67,18 @@ export function progress(task: Task, options?: ProgressOptions): Task.Async {
} catch (err) {
spinner.stopAndPersist({
text: message,
symbol: getLogLevelPrefix('info')
symbol: addPrefix(getLogLevelPrefix('info'), null, 'print', ctx)
});
throw err;
}
spinner.stopAndPersist({
text: message,
symbol: getLogLevelPrefix(wasCancelled ? 'info' : 'success')
symbol: addPrefix(
getLogLevelPrefix(wasCancelled ? 'info' : 'success'),
null,
'print',
ctx
)
});
});
}
3 changes: 2 additions & 1 deletion src/tasks/stdio/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createInterface } from 'readline';
import { into } from 'pipettes';
import { Task } from '../../definitions';
import { getBadge } from '../../helpers/badges';
import { addPrefix } from '../../helpers/prefix';
import { stringifyError } from '../../helpers/stringify';
import { isInteractive } from '../../utils/is-interactive';
import { isCancelled } from '../../utils/is-cancelled';
Expand Down Expand Up @@ -94,7 +95,7 @@ export function prompt(options: PromptOptions | Empty, task: Task): Task.Async {
}),
into(null, function read() {
return new Promise<string | null>((resolve, reject) => {
readline.question(message, (res) => {
readline.question(addPrefix(message, null, 'print', ctx), (res) => {
let valid = false;
let error: [Error] | null = null;
const response =
Expand Down
12 changes: 9 additions & 3 deletions src/tasks/stdio/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { raises } from '../exception/raises';
import { create } from '../creation/create';
import { print } from './print';
import { log } from './log';
import { addPrefix } from '~/helpers/prefix';

export interface SelectOptions {
/**
Expand Down Expand Up @@ -98,9 +99,14 @@ export function select(
cleanup: true,
values: names,
...(fallback >= 0 ? { defaultValue: fallback } : {}),
selected: getBadge('selected'),
unselected: getBadge('unselected'),
indentation: 4,
selected: addPrefix(getBadge('selected'), ' '.repeat(3), 'print', ctx),
unselected: addPrefix(
getBadge('unselected'),
' '.repeat(3),
'print',
ctx
),
indentation: 0,
outputStream: ctx.stdio[1],
inputStream: Object.create(stdin, {
setRawMode: {
Expand Down

0 comments on commit 0e25098

Please sign in to comment.