-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
241 lines (223 loc) Β· 6.62 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
KeyCode,
parse,
} from "https://deno.land/x/[email protected]/keycode/mod.ts";
import { green, cyan, magenta } from "./deps.ts";
import { plugins } from "./plugins/mod.ts";
import {
availableCommands,
evaluateSystemCommand,
spawn,
} from "./src/helpers.ts";
import Brew from "./plugins/brew.ts";
const history: string[] = [];
const useSuggestions = [
...(await availableCommands()),
...plugins.map((p) => `use ${p.name}`),
];
async function repl(
message = "> ",
suggestions = [
"use",
"help",
"history",
"list",
"exit",
...history,
...useSuggestions,
],
evaluate: (command: string) => Promise<void> = evaluateSystemCommand
) {
const command = await readline(message, suggestions);
if (command === "exit") {
if (message === "> ") {
console.log("Bye!");
return;
}
repl();
return;
}
if (command === "list" && message === "> ") {
console.log("Available plugins:");
plugins.forEach((plugin) => console.log(green(plugin.name)));
console.log(`type ${cyan("use <plugin>")} to use a plugin`);
history.push("list");
repl(message, suggestions, evaluate);
return;
}
if (command === "help" && message === "> ") {
console.log(`Common Commands:
use Use a plugin
help Show this message
history Show command history
list List available plugins
exit Exit the repl`);
repl(message, suggestions, evaluate);
return;
}
if (command!.startsWith("use ")) {
const pluginName = command!.split(" ")[1];
const selectedPlugin = plugins.find((p) => p.name === pluginName);
if (selectedPlugin) {
history.push(`use ${selectedPlugin.name}`);
await selectedPlugin.install();
repl(
`${selectedPlugin.name} > `,
[
...Object.keys(selectedPlugin.commands),
...history,
...useSuggestions,
"exit",
"use",
],
(command: string) => selectedPlugin.evaluate(command)
);
return;
} else {
console.log(`plugin ${green(pluginName)} not found`);
}
repl(message, suggestions, evaluate);
return;
}
if (command === "history") {
console.log(history.join("\n"));
repl(message, suggestions, evaluate);
return;
}
if (command === "") {
repl(message, suggestions, evaluate);
return;
}
history.push(command!);
await evaluate(command!);
repl(message, [...suggestions, ...history], evaluate);
}
// Learn more at https://deno.land/manual/examples/module_metadata#concepts
if (import.meta.main) {
console.log(
magenta(` .
.(((((((((((((((((((((((((((((
.(( (((
.(( (#### .(((
.(( ****.##### (((
.(( *****.#### (((
.(( ,**.#####.* .(((
.(( ####.***** (((
.(( ####.***** (((
.(( .#.**** .(((
.(( * .(((
.(( (((
./((((((((((((((((((((((((((( .
.`)
);
console.log("Repl v0.7.1 π β¨");
console.log("exit using ctrl+c, or exit, type help for more info");
repl();
}
async function readline(message: string, suggestions: string[]) {
Deno.stdout.writeSync(new TextEncoder().encode(message));
let input: string[] = [];
let cursor = 0;
while (true) {
const data = new Uint8Array(8);
Deno.stdin.setRaw(true);
const nread = await Deno.stdin.read(data);
Deno.stdin.setRaw(false);
if (nread === null) {
break;
}
const keys: Array<KeyCode> = parse(data.subarray(0, nread));
for (const key of keys) {
if (key.ctrl && key.name === "c") {
console.log("\nexit");
Deno.exit();
}
if (key.name === "up") {
continue;
}
if (key.name === "down") {
continue;
}
if (key.name === "left") {
if (cursor > 0) {
cursor = cursor - 1;
} else {
continue;
}
}
if (key.name === "right") {
if (cursor < input.length) {
cursor = cursor + 1;
} else {
continue;
}
}
if (key.name === "return") {
console.log();
return input.join("");
}
if (key.sequence === "\x7f") {
if (cursor > 0) {
cursor = cursor - 1;
input.splice(cursor, 1);
}
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[2K\r"));
Deno.stdout.writeSync(new TextEncoder().encode(message));
Deno.stdout.writeSync(new TextEncoder().encode(`${input.join("")}`));
if (cursor < input.length) {
Deno.stdout.writeSync(
new TextEncoder().encode(`\x1b[${input.length - cursor}D`)
);
continue;
}
Deno.stdout.writeSync(
new TextEncoder().encode(`\x1b[${input.length - cursor - 3}C`)
);
}
if (
key.sequence?.match(/^[0-9a-zA-Z!@#$%^&*()_+=\[\]{};:'",<.>/?\\| -]+$/u)
) {
cursor++;
input.splice(cursor, 0, key.sequence);
if (input.join("").split(" ").length > 1) {
Deno.stdout.writeSync(new TextEncoder().encode(key.sequence));
continue;
}
const selected = await fzf(suggestions, input.join(""));
Deno.stdout.writeSync(new TextEncoder().encode(selected));
if (selected) {
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[2K\r"));
Deno.stdout.writeSync(new TextEncoder().encode(message));
input = selected.split("");
cursor = input.length;
Deno.stdout.writeSync(new TextEncoder().encode(`${input.join("")}`));
continue;
}
}
Deno.stdout.writeSync(new TextEncoder().encode(key.sequence));
}
}
}
async function fzf(suggestions: string[], message: string) {
await setupFzf();
const command = new Deno.Command("sh", {
args: [
"-c",
'echo "' + suggestions.join("\n") + '" | fzf' + " -q " + message,
],
stdin: "inherit",
stdout: "piped",
stderr: "inherit",
});
const child = command.spawn();
const { stdout } = await child.output();
const decoder = new TextDecoder();
const output = decoder.decode(stdout);
const lines = output.split("\n");
const selected = lines[lines.length - 2];
return selected;
}
async function setupFzf() {
await new Brew().install();
await spawn("sh", ["-c", "type fzf > /dev/null || brew install fzf"]);
}