Skip to content

Commit

Permalink
fix(tasks): fix select feedback messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 7, 2021
1 parent ff3cecc commit 0cf4fd8
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/tasks/stdio/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,33 @@ export interface SelectOptions {
default?: string | null;
}

/**
* Uses a context's stdio to prompt for input.
* Takes in a record of `tasks`.
* @returns Task
*/
export function select(
options: SelectOptions | Empty,
tasks: Members<Task | Empty>
): Task.Async {
return create(async (ctx) => {
const opts = shallow(
{
message: 'Select to continue:',
message: 'Select to continue',
timeout: -1,
default: null as string | null
},
options || undefined
);

into(
ctx,
print(
style(figures(figures.pointer), { bold: true, color: 'yellow' }),
' ' + opts.message
)
);

const names = Object.keys(tasks);
const fallback: number = TypeGuard.isString(opts.default)
? names.indexOf(opts.default)
Expand All @@ -52,7 +65,11 @@ export function select(
if (!isInteractive(ctx)) {
if (fallback >= 0 && opts.default) {
return series(
log('info', 'Default selection [non-interactive]:', opts.default),
log(
'info',
'Default selection [non-interactive]:',
style(opts.default, { bold: true })
),
tasks[opts.default]
);
}
Expand All @@ -61,14 +78,6 @@ export function select(
);
}

into(
ctx,
print(style(figures(figures.pointer)) + ' ' + opts.message, {
bold: true,
color: 'yellow'
})
);

const stdin = ctx.stdio[0];
function cancel(): void {
stdin.emit('keypress', undefined, {
Expand All @@ -94,7 +103,7 @@ export function select(
...(fallback >= 0 ? { defaultValue: fallback } : {}),
selected: figures(figures.circleFilled),
unselected: figures(figures.circle),
indentation: 2,
indentation: 4,
outputStream: ctx.stdio[2] as NodeJS.WriteStream,
inputStream: Object.create(stdin, {
setRawMode: {
Expand All @@ -117,14 +126,21 @@ export function select(

// Explicit response by user
if (response !== null) {
return series(log('info', 'Select:', response), tasks[response]);
return series(
log('info', 'Select:', style(response, { bold: true })),
tasks[response]
);
}
// No response and no timeout triggered
if (!didTimeout) return raises(Error(`User cancellation`));
// No response and timeout triggered with a default selection available
if (fallback >= 0 && opts.default) {
return series(
log('info', 'Default selection [timeout]:', opts.default),
log(
'info',
'Default selection [timeout]:',
style(opts.default, { bold: true })
),
tasks[opts.default]
);
}
Expand Down

0 comments on commit 0cf4fd8

Please sign in to comment.