-
Notifications
You must be signed in to change notification settings - Fork 7
/
dialog.ts
175 lines (168 loc) · 5.03 KB
/
dialog.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Asynchronous dialog functions for both browsers and terminals.
*
* This includes `alert`, `confirm`, `prompt` and other non-standard dialogs.
* @experimental
* @module
*/
import { isBrowserWindow, isDeno, isNodeLike } from "./env.ts";
import progress from "./dialog/progress.ts";
export { progress };
export * from "./dialog/progress.ts";
export * from "./dialog/file.ts";
/**
* Options for dialog functions such as {@link alert}, {@link confirm} and
* {@link prompt}.
*/
export interface DialogOptions {
/**
* By default, a GUI dialog is displayed in the browser, and text mode is
* used in the terminal. Set this option to `true` will force the program
* to always display a GUI dialog, even in the terminal.
*
* When in the terminal, the GUI dialog is rendered with the OS's native
* dialog. If the dialog is failed to display, an error will be thrown.
*
* This option is only functional in `Windows`, `macOS` and `Linux`, it is
* ignored in other platforms and the browser.
*/
gui?: boolean;
}
/**
* Displays a dialog with a message, and to wait until the user dismisses the
* dialog.
*
* @example
* ```ts
* import { alert } from "@ayonli/jsext/dialog";
*
* await alert("Hello, world!");
* ```
*/
export async function alert(message: string, options: DialogOptions = {}): Promise<void> {
if (isBrowserWindow) {
const { alert } = await import("./dialog/web.ts");
await alert(message);
} else if (isDeno || isNodeLike) {
const { default: alert } = await import("./dialog/cli/alert.ts");
await alert(message, options);
} else {
throw new Error("Unsupported runtime");
}
}
/**
* Displays a dialog with a message, and to wait until the user either confirms
* or cancels the dialog.
*
* @example
* ```ts
* import { confirm } from "@ayonli/jsext/dialog";
*
* if (await confirm("Are you sure?")) {
* console.log("Confirmed");
* } else {
* console.log("Canceled");
* }
* ```
*/
export async function confirm(message: string, options: DialogOptions = {}): Promise<boolean> {
if (isBrowserWindow) {
const { confirm } = await import("./dialog/web.ts");
return await confirm(message);
} else if (isDeno || isNodeLike) {
const { default: confirm } = await import("./dialog/cli/confirm.ts");
return await confirm(message, options);
} else {
throw new Error("Unsupported runtime");
}
}
/**
* Options for the {@link prompt} function.
*/
export interface PromptOptions extends DialogOptions {
/**
* The default value of the input box.
*/
defaultValue?: string | undefined;
/**
* The type of the input box. The default value is `text`, when `password`
* is specified, the input will be masked.
*/
type?: "text" | "password";
/**
* Terminal only, used when `type` is `password`. The default value is
* `*`, use an empty string if you don't want to show any character.
*
* This option is ignored when `gui` is `true`.
*/
mask?: string | undefined;
}
/**
* Displays a dialog with a message prompting the user to input some text, and to
* wait until the user either submits the text or cancels the dialog.
*
* @example
* ```ts
* import { prompt } from "@ayonli/jsext/dialog";
*
* const name = await prompt("What's your name?");
*
* if (name) {
* console.log(`Hello, ${name}!`);
* }
* ```
*
* @example
* ```ts
* // with default value
* import { prompt } from "@ayonli/jsext/dialog";
*
* const name = await prompt("What's your name?", "John Doe");
*
* if (name) {
* console.log(`Hello, ${name}!`);
* }
* ```
*/
export async function prompt(
message: string,
defaultValue?: string | undefined
): Promise<string | null>;
/**
* @example
* ```ts
* // input password
* import { prompt } from "@ayonli/jsext/dialog";
*
* const password = await prompt("Enter your password:", { type: "password" });
*
* if (password) {
* console.log("Your password is:", password);
* }
* ```
*/
export async function prompt(message: string, options?: PromptOptions): Promise<string | null>;
export async function prompt(
message: string,
options: string | PromptOptions = ""
): Promise<string | null> {
const defaultValue = typeof options === "string"
? options
: options.defaultValue;
const type = typeof options === "object"
? options.type ?? "text"
: "text";
const mask = type === "password"
? typeof options === "object" ? (options.mask ?? "*") : "*"
: undefined;
const gui = typeof options === "object" ? (options.gui ?? false) : false;
if (isBrowserWindow) {
const { prompt } = await import("./dialog/web.ts");
return await prompt(message, { type, defaultValue });
} else if (isDeno || isNodeLike) {
const { default: prompt } = await import("./dialog/cli/prompt.ts");
return await prompt(message, { defaultValue, type, mask, gui });
} else {
throw new Error("Unsupported runtime");
}
}