import { GenericPrompt } from "./_generic_prompt.ts";import { GenericSuggestions, GenericSuggestionsKeys, GenericSuggestionsOptions, GenericSuggestionsSettings,} from "./_generic_suggestions.ts";import { blue, dim, yellow } from "./deps.ts";import { Figures } from "./figures.ts";
export type ConfirmKeys = GenericSuggestionsKeys;
type UnsupportedOptions = | "files" | "complete" | "suggestions" | "list" | "info";
export interface ConfirmOptions extends Omit<GenericSuggestionsOptions<boolean, string>, UnsupportedOptions> { active?: string; inactive?: string; keys?: ConfirmKeys;}
interface ConfirmSettings extends GenericSuggestionsSettings<boolean, string> { active: string; inactive: string; keys?: ConfirmKeys;}
export class Confirm extends GenericSuggestions<boolean, string, ConfirmSettings> { public static prompt( options: string | ConfirmOptions, ): Promise<boolean> { if (typeof options === "string") { options = { message: options }; }
return new this({ pointer: blue(Figures.POINTER_SMALL), prefix: yellow("? "), indent: " ", listPointer: blue(Figures.POINTER), maxRows: 8, active: "Yes", inactive: "No", ...options, files: false, complete: undefined, suggestions: [ options.active ?? "Yes", options.inactive ?? "No", ], list: false, info: false, }).prompt(); }
public static inject(value: string): void { GenericPrompt.inject(value); }
protected defaults(): string { let defaultMessage = "";
if (this.settings.default === true) { defaultMessage += this.settings.active[0].toUpperCase() + "/" + this.settings.inactive[0].toLowerCase(); } else if (this.settings.default === false) { defaultMessage += this.settings.active[0].toLowerCase() + "/" + this.settings.inactive[0].toUpperCase(); } else { defaultMessage += this.settings.active[0].toLowerCase() + "/" + this.settings.inactive[0].toLowerCase(); }
return defaultMessage ? dim(` (${defaultMessage})`) : ""; }
protected success(value: boolean): string | undefined { this.saveSuggestions(this.format(value)); return super.success(value); }
protected getValue(): string { return this.inputValue; }
protected validate(value: string): boolean | string { return typeof value === "string" && [ this.settings.active[0].toLowerCase(), this.settings.active.toLowerCase(), this.settings.inactive[0].toLowerCase(), this.settings.inactive.toLowerCase(), ].indexOf(value.toLowerCase()) !== -1; }
protected transform(value: string): boolean | undefined { switch (value.toLowerCase()) { case this.settings.active[0].toLowerCase(): case this.settings.active.toLowerCase(): return true; case this.settings.inactive[0].toLowerCase(): case this.settings.inactive.toLowerCase(): return false; } return; }
protected format(value: boolean): string { return value ? this.settings.active : this.settings.inactive; }}