import { GenericPrompt } from "./_generic_prompt.ts";import { underline } from "./deps.ts";import { GenericInput, GenericInputKeys, GenericInputPromptOptions, GenericInputPromptSettings,} from "./_generic_input.ts";
export interface SecretOptions extends GenericInputPromptOptions<string, string> { keys?: SecretKeys; label?: string; hidden?: boolean; minLength?: number; maxLength?: number;}
interface SecretSettings extends GenericInputPromptSettings<string, string> { label: string; hidden: boolean; minLength: number; maxLength: number; keys?: SecretKeys;}
export type SecretKeys = GenericInputKeys;
export class Secret extends GenericInput<string, string> { protected readonly settings: SecretSettings;
public static prompt(options: string | SecretOptions): Promise<string> { return new this(options).prompt(); }
public static inject(value: string): void { GenericPrompt.inject(value); }
constructor(options: string | SecretOptions) { super(); if (typeof options === "string") { options = { message: options }; } this.settings = this.getDefaultSettings(options); }
public getDefaultSettings(options: SecretOptions): SecretSettings { return { ...super.getDefaultSettings(options), label: options.label ?? "Secret", hidden: options.hidden ?? false, minLength: options.minLength ?? 0, maxLength: options.maxLength ?? Infinity, }; }
protected input(): string { return underline( this.settings.hidden ? "" : "*".repeat(this.inputValue.length), ); }
protected read(): Promise<boolean> { if (this.settings.hidden) { this.settings.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; }}