-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputHandler.ts
89 lines (82 loc) · 2.69 KB
/
inputHandler.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
import { refreshDisplay } from "./displayManager.ts";
import { ColorFunction } from "./coloredInput.ts";
export async function handleInput(
onEnter: (input: string) => void,
getColorFn: () => ColorFunction,
getSuggestion?: () => string,
): Promise<void> {
Deno.stdin.setRaw(true);
let currentInput = "";
let cursorPosition = 0;
const reader = Deno.stdin.readable.getReader();
let currentColorFn = getColorFn();
let currentSuggestion = getSuggestion?.();
try {
await refreshDisplay(
currentInput,
cursorPosition,
currentColorFn,
currentSuggestion,
);
while (true) {
const { value: chunk, done } = await reader.read();
if (done) break;
for (let i = 0; i < chunk.length; i++) {
const key = chunk[i];
if (key === 3) { // Ctrl+C
console.log();
return;
}
if (key === 13) { // Enter
console.log();
onEnter(currentInput);
currentInput = "";
cursorPosition = 0;
currentColorFn = getColorFn(); // Change color function only after Enter
currentSuggestion = getSuggestion?.();
} else if (key === 127) { // Backspace
if (cursorPosition > 0) {
currentInput = currentInput.slice(0, cursorPosition - 1) +
currentInput.slice(cursorPosition);
cursorPosition--;
}
} else if (key === 9) { // Tab
if (currentSuggestion && currentInput.length === 0) {
currentInput = currentSuggestion;
cursorPosition = currentInput.length;
currentSuggestion = getSuggestion?.();
}
} else if (key === 27) { // Escape (start of arrow key sequence)
if (i + 2 < chunk.length && chunk[i + 1] === 91) {
if (chunk[i + 2] === 68) { // Left arrow
if (cursorPosition > 0) {
cursorPosition--;
}
i += 2; // Skip the next two bytes
} else if (chunk[i + 2] === 67) { // Right arrow
if (cursorPosition < currentInput.length) {
cursorPosition++;
}
i += 2; // Skip the next two bytes
}
}
} else {
// Add character to the input at the cursor position
const char = String.fromCharCode(key);
currentInput = currentInput.slice(0, cursorPosition) + char +
currentInput.slice(cursorPosition);
cursorPosition++;
}
await refreshDisplay(
currentInput,
cursorPosition,
currentColorFn,
currentSuggestion,
);
}
}
} finally {
Deno.stdin.setRaw(false);
reader.releaseLock();
}
}