Skip to content

Commit

Permalink
feat(prompt): add prefix option (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jun 30, 2021
1 parent 39a8075 commit a4943cb
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 13 deletions.
4 changes: 2 additions & 2 deletions prompt/_generic_list.ts
Expand Up @@ -5,7 +5,7 @@ import {
GenericInputPromptOptions,
GenericInputPromptSettings,
} from "./_generic_input.ts";
import { blue, bold, dim, stripColor, yellow } from "./deps.ts";
import { blue, bold, dim, stripColor } from "./deps.ts";
import { Figures } from "./figures.ts";
import { distance } from "../_utils/distance.ts";

Expand Down Expand Up @@ -139,7 +139,7 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
}

protected message(): string {
let message = `${this.settings.indent}${yellow("?")} ` +
let message = `${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) +
this.defaults();
if (this.settings.search) {
Expand Down
8 changes: 5 additions & 3 deletions prompt/_generic_prompt.ts
@@ -1,7 +1,7 @@
import type { Cursor } from "../ansi/cursor_position.ts";
import { tty } from "../ansi/tty.ts";
import { KeyCode, parse } from "../keycode/key_code.ts";
import { blue, bold, dim, green, italic, red, yellow } from "./deps.ts";
import { blue, bold, dim, green, italic, red } from "./deps.ts";
import { Figures } from "./figures.ts";

/** Prompt validation return tape. */
Expand All @@ -23,13 +23,15 @@ export interface GenericPromptOptions<T, V> {
indent?: string;
keys?: GenericPromptKeys;
cbreak?: boolean;
prefix?: string;
}

/** Generic prompt settings. */
export interface GenericPromptSettings<T, V>
extends GenericPromptOptions<T, V> {
pointer: string;
indent: string;
prefix: string;
}

/** Static generic prompt interface. */
Expand Down Expand Up @@ -183,7 +185,7 @@ export abstract class GenericPrompt<
}

protected message(): string {
return `${this.settings.indent}${yellow("?")} ` +
return `${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) + this.defaults();
}

Expand All @@ -197,7 +199,7 @@ export abstract class GenericPrompt<

/** Get prompt success message. */
protected success(value: T): string | undefined {
return `${this.settings.indent}${yellow("?")} ` +
return `${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) + this.defaults() +
" " + this.settings.pointer +
" " + green(this.format(value));
Expand Down
3 changes: 2 additions & 1 deletion prompt/checkbox.ts
@@ -1,5 +1,5 @@
import type { KeyCode } from "../keycode/mod.ts";
import { blue, dim, green, red } from "./deps.ts";
import { blue, dim, green, red, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericList,
Expand Down Expand Up @@ -69,6 +69,7 @@ export class Checkbox
public static prompt(options: CheckboxOptions): Promise<string[]> {
return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 10,
Expand Down
3 changes: 2 additions & 1 deletion prompt/confirm.ts
Expand Up @@ -5,7 +5,7 @@ import {
GenericSuggestionsOptions,
GenericSuggestionsSettings,
} from "./_generic_suggestions.ts";
import { blue, dim } from "./deps.ts";
import { blue, dim, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";

export type ConfirmKeys = GenericSuggestionsKeys;
Expand Down Expand Up @@ -41,6 +41,7 @@ export class Confirm

return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 8,
Expand Down
3 changes: 2 additions & 1 deletion prompt/input.ts
Expand Up @@ -5,7 +5,7 @@ import {
GenericSuggestionsOptions,
GenericSuggestionsSettings,
} from "./_generic_suggestions.ts";
import { blue } from "./deps.ts";
import { blue, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";

export type InputKeys = GenericSuggestionsKeys;
Expand Down Expand Up @@ -35,6 +35,7 @@ export class Input extends GenericSuggestions<string, string, InputSettings> {

return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 8,
Expand Down
3 changes: 2 additions & 1 deletion prompt/list.ts
Expand Up @@ -5,7 +5,7 @@ import {
GenericSuggestionsOptions,
GenericSuggestionsSettings,
} from "./_generic_suggestions.ts";
import { blue, dim, underline } from "./deps.ts";
import { blue, dim, underline, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";

/** List key options. */
Expand Down Expand Up @@ -42,6 +42,7 @@ export class List extends GenericSuggestions<string[], string, ListSettings> {

return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 8,
Expand Down
3 changes: 2 additions & 1 deletion prompt/number.ts
Expand Up @@ -7,7 +7,7 @@ import {
GenericSuggestionsSettings,
} from "./_generic_suggestions.ts";
import { parseNumber } from "./_utils.ts";
import { blue } from "./deps.ts";
import { blue, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";

/** Number key options. */
Expand Down Expand Up @@ -45,6 +45,7 @@ export class Number extends GenericSuggestions<number, string, NumberSettings> {

return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 8,
Expand Down
3 changes: 2 additions & 1 deletion prompt/secret.ts
@@ -1,5 +1,5 @@
import { GenericPrompt } from "./_generic_prompt.ts";
import { blue, underline } from "./deps.ts";
import { blue, underline, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericInput,
Expand Down Expand Up @@ -40,6 +40,7 @@ export class Secret extends GenericInput<string, string, SecretSettings> {

return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
label: "Password",
hidden: false,
Expand Down
3 changes: 2 additions & 1 deletion prompt/select.ts
@@ -1,4 +1,4 @@
import { blue, underline } from "./deps.ts";
import { blue, underline, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericList,
Expand Down Expand Up @@ -53,6 +53,7 @@ export class Select<S extends SelectSettings = SelectSettings>
public static prompt(options: SelectOptions): Promise<string> {
return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 10,
Expand Down
1 change: 1 addition & 0 deletions prompt/test/integration/fixtures/input_no_prefix.in
@@ -0,0 +1 @@
bar
2 changes: 2 additions & 0 deletions prompt/test/integration/fixtures/input_no_prefix.out
@@ -0,0 +1,2 @@
Whats your name? (foo) › \x1b[27G\x1b[G\x1b[0J Whats your name? (foo) › bar
\x1b[?25h\x1b[?25h
7 changes: 7 additions & 0 deletions prompt/test/integration/fixtures/input_no_prefix.ts
@@ -0,0 +1,7 @@
import { Input } from "../../../input.ts";

await Input.prompt({
message: "Whats your name?",
default: "foo",
prefix: "",
});
3 changes: 3 additions & 0 deletions prompt/test/integration/fixtures/input_no_prefix.windows.out
@@ -0,0 +1,3 @@
Whats your name? (foo) »
\x1b[1A\x1b[27G\x1b[G\x1b[0J Whats your name? (foo) » bar
\x1b[?25h\x1b[?25h
1 change: 1 addition & 0 deletions prompt/test/integration/fixtures/input_prefix.in
@@ -0,0 +1 @@
bar
2 changes: 2 additions & 0 deletions prompt/test/integration/fixtures/input_prefix.out
@@ -0,0 +1,2 @@
PREFIX Whats your name? (foo) › \x1b[34G\x1b[G\x1b[0J PREFIX Whats your name? (foo) › bar
\x1b[?25h\x1b[?25h
7 changes: 7 additions & 0 deletions prompt/test/integration/fixtures/input_prefix.ts
@@ -0,0 +1,7 @@
import { Input } from "../../../input.ts";

await Input.prompt({
message: "Whats your name?",
default: "foo",
prefix: "PREFIX ",
});
3 changes: 3 additions & 0 deletions prompt/test/integration/fixtures/input_prefix.windows.out
@@ -0,0 +1,3 @@
PREFIX Whats your name? (foo) »
\x1b[1A\x1b[34G\x1b[G\x1b[0J PREFIX Whats your name? (foo) » bar
\x1b[?25h\x1b[?25h
3 changes: 2 additions & 1 deletion prompt/toggle.ts
@@ -1,5 +1,5 @@
import type { KeyCode } from "../keycode/key_code.ts";
import { blue, dim, underline } from "./deps.ts";
import { blue, dim, underline, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericPrompt,
Expand Down Expand Up @@ -44,6 +44,7 @@ export class Toggle extends GenericPrompt<boolean, string, ToggleSettings> {

return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
active: "Yes",
inactive: "No",
Expand Down

0 comments on commit a4943cb

Please sign in to comment.