-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.ts
82 lines (71 loc) · 2.87 KB
/
ui.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
import { renderMarkdown } from "./deps.ts";
import { Select, SelectValueOptions, Input } from "./deps.ts";
import { Theme } from "./theme.ts";
import { backspace, upInCL } from "./utils.ts";
export class UI {
static listOptions = {
back: UI.selectListOption({name: "Back", value: "kopo_back"}),
separator: UI.selectListOption({name: "-".repeat(30), value: "kopo_separator", disabled: true}),
empty: UI.selectListOption({name: " ", value: "kopo_empty", disabled: true}),
disabled: (name: string) => UI.selectListOption({name, value: "kopo_disabled", disabled: true}),
}
static clearLine() {
console.log(upInCL(1) + " ".repeat(70) + upInCL(1));
}
static upInCL(lines?: number) {
console.log(upInCL(lines));
}
static cls() {
console.log('\x1Bc');
}
static async selectList(opts: {message: string, options: SelectValueOptions, default?: string, hint?: string, maxRows?: number}) {
return await Select.prompt({
message: `${backspace(5)}${opts.message}`,
options: opts.options.map(opt => (opt as any)['_ui_'] ? opt : UI.selectListOption(opt as any)),
listPointer: `${Theme.accent('>>')}\x1b[1m`,
// search: true,
// searchIcon: '?*',
// searchLabel: 'Search',
// transform: value => value+'!!', // selected value transform
// transform: value => '',
pointer: '>>', // after selected
keys: {
previous: ['w', '8', 'up'],
next: ['s', '2', 'down'],
nextPage: ['n'], previousPage: ['p']
},
default: opts.default,
maxRows: opts.maxRows ?? 20,
hint: opts.hint,
});
}
static selectListOption(opts: string | {name: string, value?: string, disabled?: boolean, disabledName?: string}) {
if(typeof opts === 'string') {
opts = {name: opts};
}
const value = opts.value ?? opts.name;
return {
name: (opts.disabled ? `${Theme.colors.gray(opts.disabledName ?? opts.name)}` : opts.name) + "\x1b[39m\x1b[0m",
value,
disabled: opts.disabled,
_ui_: true,
is(option: string) {
return option === value;
}
}
}
static listHint(hint: string) {
return UI.selectListOption({name: backspace(5) + renderMarkdown(`*${hint}*`), disabled: true});
}
static async input(opts: {message: string, suggestions?: string[] | Promise<string[]>}) {
return await Input.prompt({
message: `${backspace(5)}${opts.message}`,
suggestions: await opts.suggestions,
pointer: ">>",
// keys: {complete: ["enter", "right"]}
});
}
static async confirm(opts: {message: string}){
return confirm(opts.message);
}
}