/
confirm.ts
133 lines (118 loc) 路 3.59 KB
/
confirm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { GenericPrompt } from "./_generic_prompt.ts";
import {
GenericSuggestions,
GenericSuggestionsKeys,
GenericSuggestionsOptions,
GenericSuggestionsSettings,
} from "./_generic_suggestions.ts";
import { blue, dim } from "./deps.ts";
import { Figures } from "./figures.ts";
export type ConfirmKeys = GenericSuggestionsKeys;
type UnsupportedInputOptions = "suggestions" | "list" | "info";
/** Confirm prompt options. */
export interface ConfirmOptions
extends
Omit<GenericSuggestionsOptions<boolean, string>, UnsupportedInputOptions> {
active?: string;
inactive?: string;
keys?: ConfirmKeys;
}
/** Confirm prompt settings. */
interface ConfirmSettings extends GenericSuggestionsSettings<boolean, string> {
active: string;
inactive: string;
keys?: ConfirmKeys;
}
/** Confirm prompt representation. */
export class Confirm
extends GenericSuggestions<boolean, string, ConfirmSettings> {
/** Execute the prompt and show cursor on end. */
public static prompt(
options: string | ConfirmOptions,
): Promise<boolean> {
if (typeof options === "string") {
options = { message: options };
}
return new this({
pointer: blue(Figures.POINTER_SMALL),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 8,
active: "Yes",
inactive: "No",
...options,
suggestions: [
options.active ?? "Yes",
options.inactive ?? "No",
],
list: false,
info: false,
}).prompt();
}
/**
* Inject prompt value. Can be used for unit tests or pre selections.
* @param value Input value.
*/
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);
}
/** Get input input. */
protected getValue(): string {
return this.inputValue;
}
/**
* 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 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;
}
/**
* Map input value to output value.
* @param value Input value.
* @return Output value.
*/
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;
}
/**
* Format output value.
* @param value Output value.
*/
protected format(value: boolean): string {
return value ? this.settings.active : this.settings.inactive;
}
}