-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal.ts
93 lines (86 loc) · 2.86 KB
/
terminal.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
// https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
import { equals, iterateReader } from './deps.ts';
const ENTER = new Uint8Array([13]);
const CTRL_C = new Uint8Array([3]);
const ARROW_UP = new Uint8Array([27, 91, 65]);
const ARROW_DOWN = new Uint8Array([27, 91, 66]);
const ARROW_RIGHT = new Uint8Array([27, 91, 67]);
const ARROW_LEFT = new Uint8Array([27, 91, 68]);
const BACKSPACE = new Uint8Array([127]);
const DEL = new Uint8Array([27, 91, 51, 126]);
if (import.meta.main) {
for await (const command of terminal()) {
command && console.log(`command: \u001b[31m\u001b[1m${command}\u001b[0m`);
}
}
export async function* terminal(prompt = "> ") {
try {
Deno.setRaw(Deno.stdin.rid, true);
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const history = new Set<string>();
let historyPointer = 0;
let command = "";
let pos = 0;
await Deno.stdout.write(encoder.encode(prompt));
while (true) {
for await (const chunk of iterateReader(Deno.stdin)) {
const key = {
enter: equals(chunk, ENTER),
up: equals(chunk, ARROW_UP),
down: equals(chunk, ARROW_DOWN),
left: equals(chunk, ARROW_LEFT),
right: equals(chunk, ARROW_RIGHT),
backspace: equals(chunk, BACKSPACE),
del: equals(chunk, DEL),
ctrlC: equals(chunk, CTRL_C),
};
if (key.enter) {
console.log("");
const finalCommand = command.trim();
history.delete(command);
history.add(command);
historyPointer = history.size;
command = "";
pos = 0;
yield finalCommand;
} else if (
(pos > 0 && key.backspace) ||
(pos < command.length && key.del)
) {
pos += key.backspace ? -1 : 0;
command = command.substr(0, pos) + command.substr(pos + 1);
} else if (key.ctrlC) {
return;
} else if (
(historyPointer > 0 && key.up) ||
(historyPointer < history.size && key.down)
) {
historyPointer += key.up ? -1 : 1;
command = [...history][historyPointer] ?? "";
pos = command.length;
} else if (
(pos > 0 && key.left) ||
(pos < command.length && key.right)
) {
pos += key.left ? -1 : 1;
} else if (chunk[0] > 27 && chunk[0] !== 127) {
const input = decoder.decode(chunk);
command = command.substr(0, pos) + input + command.substr(pos);
pos += input.length;
} else {
continue;
}
await Deno.stdout.write(
encoder.encode(
`\u001b[1000D\u001b[2K${prompt}${command}\u001b[1000D\u001b[${
pos + prompt.length
}C`
)
);
}
}
} finally {
Deno.setRaw(Deno.stdin.rid, false);
}
}