-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
148 lines (134 loc) · 4.05 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
import { parseArgs } from "https://deno.land/std/cli/parse_args.ts";
import { connect } from "npm:@fireproof/[email protected]";
import { fireproof } from "npm:@fireproof/[email protected]";
const args = parseArgs(Deno.args, {
string: ["localName", "remoteName", "shareURL"],
default: {
localName: "__z11",
remoteName: "todos",
},
});
// Parse shareURL if provided
if (args.shareURL) {
try {
const url = new URL(args.shareURL);
const params = new URLSearchParams(url.search);
const endpoint = params.get("endpoint");
if (endpoint) {
args.localName = params.get("localName") || args.localName;
args.remoteName = params.get("remoteName") || args.remoteName;
}
} catch (_err) {
console.error("Invalid shareURL format");
}
}
const db = fireproof(args.localName);
// Initialize connection
await connect(db, args.remoteName);
let __EVAL = (s: string) =>
eval(`
void (__EVAL = ${__EVAL.toString()});
${s}
`);
async function evaluate(expr: string) {
try {
const result = await __EVAL(expr);
console.log("<", result);
} catch (err) {
if (err instanceof Error) {
console.log(expr, "ERROR:", err.message);
}
}
}
console.log("JavaScript REPL for Fireproof (press Ctrl+C to exit)");
console.log(
"The 'db' variable is available for use. Enter expressions to evaluate:",
);
const history: string[] = [];
let historyIndex = 0;
let cursorPos = 0;
// Configure raw mode to handle keypresses
Deno.stdin.setRaw(true);
const decoder = new TextDecoder();
let currentInput = "";
// Helper to redraw the current line
function redrawLine() {
Deno.stdout.writeSync(new TextEncoder().encode(`\r\x1b[K> ${currentInput}`));
if (cursorPos < currentInput.length) {
// Move cursor back to position
Deno.stdout.writeSync(
new TextEncoder().encode(`\x1b[${currentInput.length - cursorPos}D`),
);
}
}
// Display initial prompt
Deno.stdout.writeSync(new TextEncoder().encode("> "));
while (true) {
const buf = new Uint8Array(1);
await Deno.stdin.read(buf);
const key = decoder.decode(buf);
if (key === "\x1b") { // ESC sequence
const seqBuf = new Uint8Array(2);
await Deno.stdin.read(seqBuf);
const seq = decoder.decode(seqBuf);
if (seq === "[A") { // Up arrow
if (historyIndex > 0) {
historyIndex--;
currentInput = history[historyIndex];
cursorPos = currentInput.length;
redrawLine();
}
} else if (seq === "[B") { // Down arrow
if (historyIndex < history.length) {
historyIndex++;
currentInput = historyIndex === history.length
? ""
: history[historyIndex];
cursorPos = currentInput.length;
redrawLine();
}
} else if (seq === "[C") { // Right arrow
if (cursorPos < currentInput.length) {
cursorPos++;
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[C"));
}
} else if (seq === "[D") { // Left arrow
if (cursorPos > 0) {
cursorPos--;
Deno.stdout.writeSync(new TextEncoder().encode("\x1b[D"));
}
}
} else if (key === "\x01") { // Ctrl+A - move to start
cursorPos = 0;
redrawLine();
} else if (key === "\x05") { // Ctrl+E - move to end
cursorPos = currentInput.length;
redrawLine();
} else if (key === "\r") { // Enter
Deno.stdout.writeSync(new TextEncoder().encode("\n"));
if (currentInput.trim()) {
history.push(currentInput);
historyIndex = history.length;
await evaluate(currentInput);
}
currentInput = "";
cursorPos = 0;
Deno.stdout.writeSync(new TextEncoder().encode("> "));
} else if (key === "\x03") { // Ctrl+C
Deno.stdout.writeSync(new TextEncoder().encode("\n"));
Deno.stdin.setRaw(false);
Deno.exit(0);
} else if (key === "\x7f") { // Backspace
if (cursorPos > 0) {
currentInput = currentInput.slice(0, cursorPos - 1) +
currentInput.slice(cursorPos);
cursorPos--;
redrawLine();
}
} else {
currentInput = currentInput.slice(0, cursorPos) + key +
currentInput.slice(cursorPos);
cursorPos++;
redrawLine();
}
}