Skip to content

Commit

Permalink
refactor(prompt): refactor indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Dec 28, 2020
1 parent 37fcbaf commit ad7923f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion prompt/_generic_list.ts
Expand Up @@ -119,7 +119,8 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
}

protected message(): string {
let message = ` ${yellow("?")} ` + bold(this.settings.message) +
let message = `${this.settings.indent}${yellow("?")} ` +
bold(this.settings.message) +
this.defaults();
if (this.settings.search) {
message += " " + this.settings.searchLabel + " ";
Expand Down
17 changes: 12 additions & 5 deletions prompt/_generic_prompt.ts
@@ -1,7 +1,7 @@
import { AnsiEscape } from "../ansi_escape/ansi_escape.ts";
import { KeyCode } from "../keycode/key_code.ts";
import type { KeyEvent } from "../keycode/key_event.ts";
import { blue, bold, dim, green, red, yellow } from "./deps.ts";
import { blue, bold, dim, green, italic, red, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";

/** Prompt validation return tape. */
Expand All @@ -20,13 +20,15 @@ export interface GenericPromptOptions<T, V> {
transform?: (value: V) => T | undefined;
hint?: string;
pointer?: string;
indent?: string;
keys?: GenericPromptKeys;
}

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

/** Static generic prompt interface. */
Expand Down Expand Up @@ -56,6 +58,7 @@ export abstract class GenericPrompt<
protected static injectedValue: unknown | undefined;
protected readonly settings: S;
protected readonly tty = AnsiEscape.from(Deno.stdout);
protected readonly indent: string;
protected readonly cursor: Cursor = {
x: 0,
y: 0,
Expand All @@ -80,6 +83,7 @@ export abstract class GenericPrompt<
...(settings.keys ?? {}),
},
};
this.indent = this.settings.indent ?? " ";
}

/** Execute the prompt and show cursor on end. */
Expand Down Expand Up @@ -182,7 +186,8 @@ export abstract class GenericPrompt<
}

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

protected defaults(): string {
Expand All @@ -195,7 +200,8 @@ export abstract class GenericPrompt<

/** Get prompt success message. */
protected success(value: T): string | undefined {
return ` ${yellow("?")} ` + bold(this.settings.message) + this.defaults() +
return `${this.settings.indent}${yellow("?")} ` +
bold(this.settings.message) + this.defaults() +
" " + this.settings.pointer +
" " + green(this.format(value));
}
Expand All @@ -208,13 +214,14 @@ export abstract class GenericPrompt<

protected error(): string | undefined {
return this.#lastError
? red(bold(` ${Figures.CROSS} `) + this.#lastError)
? this.settings.indent + red(bold(`${Figures.CROSS} `) + this.#lastError)
: undefined;
}

protected hint(): string | undefined {
return this.settings.hint
? dim(blue(` ${Figures.POINTER} `) + this.settings.hint)
? this.settings.indent +
italic(blue(dim(`${Figures.POINTER} `) + this.settings.hint))
: undefined;
}

Expand Down
2 changes: 0 additions & 2 deletions prompt/_generic_suggestions.ts
Expand Up @@ -22,7 +22,6 @@ export interface GenericSuggestionsOptions<T, V>
suggestions?: Array<string | number>;
list?: boolean;
info?: boolean;
indent?: string;
listPointer?: string;
maxRows?: number;
}
Expand All @@ -34,7 +33,6 @@ export interface GenericSuggestionsSettings<T, V>
suggestions?: Array<string | number>;
list?: boolean;
info?: boolean;
indent: string;
listPointer: string;
maxRows: number;
}
Expand Down
1 change: 1 addition & 0 deletions prompt/deps.ts
Expand Up @@ -3,6 +3,7 @@ export {
bold,
dim,
green,
italic,
red,
stripColor,
underline,
Expand Down
1 change: 1 addition & 0 deletions prompt/secret.ts
Expand Up @@ -40,6 +40,7 @@ export class Secret extends GenericInput<string, string, SecretSettings> {

return new this({
pointer: blue(Figures.POINTER_SMALL),
indent: " ",
label: "Password",
hidden: false,
minLength: 0,
Expand Down
1 change: 1 addition & 0 deletions prompt/toggle.ts
Expand Up @@ -44,6 +44,7 @@ export class Toggle extends GenericPrompt<boolean, string, ToggleSettings> {

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

0 comments on commit ad7923f

Please sign in to comment.