swc/bundler/tests/.cache/deno/e4a376507b2178de7ad97da524b6e2de5718912d.ts
강동윤 fec189f2f3
fix(bundler): Fix stack overflow on Windows (#1464)
bundler:
 - Prevent stack overflow. (denoland/deno#9752)

testing:
 - Bump version
 - Fix handling of paths on windows.

testing_macros:
 - Bump version
 - Correctly ignore files.
2021-03-22 19:42:42 +09:00

149 lines
3.8 KiB
TypeScript

// Loaded from https://deno.land/x/cliffy@v0.18.0/prompt/toggle.ts
import type { KeyEvent } from "../keycode/key_event.ts";
import { blue, dim, underline } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericPrompt,
GenericPromptKeys,
GenericPromptOptions,
GenericPromptSettings,
} from "./_generic_prompt.ts";
/** Toggle key options. */
export interface ToggleKeys extends GenericPromptKeys {
active?: string[];
inactive?: string[];
}
/** Generic prompt options. */
export interface ToggleOptions extends GenericPromptOptions<boolean, string> {
active?: string;
inactive?: string;
keys?: ToggleKeys;
}
/** Toggle prompt settings. */
interface ToggleSettings extends GenericPromptSettings<boolean, string> {
active: string;
inactive: string;
keys: ToggleKeys;
}
/** Toggle prompt representation. */
export class Toggle extends GenericPrompt<boolean, string, ToggleSettings> {
protected status: string = typeof this.settings.default !== "undefined"
? this.format(this.settings.default)
: "";
/** Execute the prompt and show cursor on end. */
public static prompt(
options: string | ToggleOptions,
): Promise<boolean> {
if (typeof options === "string") {
options = { message: options };
}
return new this({
pointer: blue(Figures.POINTER_SMALL),
indent: " ",
active: "Yes",
inactive: "No",
...options,
keys: {
active: ["right", "y", "j", "s", "o"],
inactive: ["left", "n"],
...(options.keys ?? {}),
},
}).prompt();
}
protected message(): string {
let message = super.message() + " " + this.settings.pointer + " ";
if (this.status === this.settings.active) {
message += dim(this.settings.inactive + " / ") +
underline(this.settings.active);
} else if (this.status === this.settings.inactive) {
message += underline(this.settings.inactive) +
dim(" / " + this.settings.active);
} else {
message += dim(this.settings.inactive + " / " + this.settings.active);
}
return message;
}
/** Read user input from stdin, handle events and validate user input. */
protected read(): Promise<boolean> {
this.tty.cursorHide();
return super.read();
}
/**
* Handle user input event.
* @param event Key event.
*/
protected async handleEvent(event: KeyEvent): Promise<void> {
switch (true) {
case event.sequence === this.settings.inactive[0].toLowerCase():
case this.isKey(this.settings.keys, "inactive", event):
this.selectInactive();
break;
case event.sequence === this.settings.active[0].toLowerCase():
case this.isKey(this.settings.keys, "active", event):
this.selectActive();
break;
default:
await super.handleEvent(event);
}
}
/** Set active. */
protected selectActive() {
this.status = this.settings.active;
}
/** Set inactive. */
protected selectInactive() {
this.status = this.settings.inactive;
}
/**
* Validate input value.
* @param value User input value.
* @return True on success, false or error message on error.
*/
protected validate(value: string): boolean | string {
return [this.settings.active, this.settings.inactive].indexOf(value) !== -1;
}
/**
* Map input value to output value.
* @param value Input value.
* @return Output value.
*/
protected transform(value: string): boolean | undefined {
switch (value) {
case this.settings.active:
return true;
case this.settings.inactive:
return false;
}
}
/**
* Format output value.
* @param value Output value.
*/
protected format(value: boolean): string {
return value ? this.settings.active : this.settings.inactive;
}
/** Get input value. */
protected getValue(): string {
return this.status;
}
}