import { GenericPrompt } from "./_generic_prompt.ts";import { blue, underline, yellow } from "./deps.ts";import { Figures } from "./figures.ts";import { GenericInput, GenericInputKeys, GenericInputPromptOptions, GenericInputPromptSettings,} from "./_generic_input.ts";
export type SecretKeys = GenericInputKeys;
export interface SecretOptions extends GenericInputPromptOptions<string, string> { label?: string; hidden?: boolean; minLength?: number; maxLength?: number; keys?: SecretKeys;}
interface SecretSettings extends GenericInputPromptSettings<string, string> { label: string; hidden: boolean; minLength: number; maxLength: number; keys?: SecretKeys;}
export class Secret extends GenericInput<string, string, SecretSettings> { public static prompt(options: string | SecretOptions): Promise<string> { if (typeof options === "string") { options = { message: options }; }
return new this({ pointer: blue(Figures.POINTER_SMALL), prefix: yellow("? "), indent: " ", label: "Password", hidden: false, minLength: 0, maxLength: Infinity, ...options, }).prompt(); }
public static inject(value: string): void { GenericPrompt.inject(value); }
protected input(): string { return underline( this.settings.hidden ? "" : "*".repeat(this.inputValue.length), ); }
protected read(): Promise<boolean> { if (this.settings.hidden) { this.tty.cursorHide(); } return super.read(); }
protected validate(value: string): boolean | string { if (typeof value !== "string") { return false; } if (value.length < this.settings.minLength) { return `${this.settings.label} must be longer than ${this.settings.minLength} but has a length of ${value.length}.`; } if (value.length > this.settings.maxLength) { return `${this.settings.label} can't be longer than ${this.settings.maxLength} but has a length of ${value.length}.`; } return true; }
protected transform(value: string): string | undefined { return value; }
protected format(value: string): string { return this.settings.hidden ? "*".repeat(8) : "*".repeat(value.length); }
protected getValue(): string { return this.inputValue; }}