-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (171 loc) · 4.63 KB
/
script.js
File metadata and controls
189 lines (171 loc) · 4.63 KB
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
import { runVampire, parseArgs, shellQuote } from './vampire-runner.js';
const outEl = document.getElementById('out');
const tptpEl = document.getElementById('tptp');
const argsEl = document.getElementById('args');
const runBtn = document.getElementById('run');
const defaultArgs = "--manual_cs on --show_new on";
argsEl.value = defaultArgs;
const transcript = [];
const inputQueue = [];
let pendingInput = null;
let runActive = false;
let currentInput = "";
const promptPrefix = "> ";
function renderTerminal() {
const lines = [...transcript];
if (runActive) {
lines.push(promptPrefix + currentInput);
}
outEl.textContent = lines.join("\n");
outEl.scrollTop = outEl.scrollHeight;
}
const log = (s='') => {
transcript.push(String(s));
renderTerminal();
};
const clearOutput = () => {
transcript.length = 0;
currentInput = "";
renderTerminal();
};
function resetInputQueue() {
inputQueue.length = 0;
pendingInput = null;
currentInput = "";
}
function deliverInput(value) {
if (pendingInput) {
pendingInput(value);
pendingInput = null;
} else {
inputQueue.push(value);
}
}
function submitCurrentInput() {
if (!runActive) { return; }
const value = currentInput;
transcript.push(promptPrefix + value);
currentInput = "";
renderTerminal();
deliverInput(value);
}
function focusTerminal() {
if (outEl) { outEl.focus({ preventScroll: true }); }
}
function handleTyping(char) {
if (!runActive) { return; }
currentInput += char;
renderTerminal();
}
outEl.addEventListener('keydown', (e) => {
if (!runActive) { return; }
if (e.ctrlKey || e.metaKey || e.altKey) { return; }
if (e.key === 'Enter') {
e.preventDefault();
submitCurrentInput();
return;
}
if (e.key === 'Backspace') {
e.preventDefault();
currentInput = currentInput.slice(0, -1);
renderTerminal();
return;
}
if (e.key.length === 1) {
e.preventDefault();
handleTyping(e.key);
}
});
outEl.addEventListener('paste', (e) => {
if (!runActive) { return; }
e.preventDefault();
const text = (e.clipboardData || window.clipboardData).getData('text');
if (!text) { return; }
const lines = text.replace(/\r/g, '').split('\n');
if (lines.length === 1) {
handleTyping(lines[0]);
return;
}
// Submit all but the last line immediately, keep the last line in the prompt
handleTyping(lines[0]);
submitCurrentInput();
for (let i = 1; i < lines.length - 1; i++) {
currentInput = lines[i];
submitCurrentInput();
}
currentInput = lines[lines.length - 1];
renderTerminal();
});
function requestInput(promptText) {
if (promptText) log(promptText);
focusTerminal();
renderTerminal();
return new Promise(resolve => {
if (inputQueue.length) {
const next = inputQueue.shift();
resolve(next);
} else {
pendingInput = (val) => {
resolve(val);
};
}
});
}
async function runOnce() {
clearOutput();
resetInputQueue();
renderTerminal();
const input = tptpEl.value.trim();
if (!input) { log("No input."); return; }
runBtn.disabled = true;
runActive = true;
focusTerminal();
renderTerminal();
try {
let argv = parseArgs(argsEl.value || '');
argv.push("/work/input.p");
const cli = "vampire " + shellQuote(argv);
log("→ " + cli);
console.log(cli);
const { code } = await runVampire(input, argsEl.value, {
onStdout: log,
onStderr: log,
requestInput,
});
log(`(exit code ${code})`);
// Best-effort copy of full transcript to clipboard
const finalText = transcript.join("\n");
if (navigator?.clipboard?.writeText) {
navigator.clipboard.writeText(finalText).then(() => {
console.log("Copied run output to clipboard");
}).catch((err) => {
console.warn("Clipboard copy failed", err);
});
}
} catch (err) {
console.error("runOnce error", err);
const detail = err && err.stack ? `${err.message}\n${err.stack}` : (err && err.message) || err;
log("FATAL: " + detail);
} finally {
runActive = false;
resetInputQueue();
runBtn.disabled = false;
renderTerminal();
}
}
runBtn.onclick = () => runOnce();
log("Ready.");
renderTerminal();
window.runVampire = runVampire;
// Surface uncaught errors/rejections for debugging
window.addEventListener('error', (ev) => {
const msg = ev?.error?.stack || ev?.message || String(ev);
console.error("window.error", ev);
log(`FATAL(window.error): ${msg}`);
});
window.addEventListener('unhandledrejection', (ev) => {
const reason = ev?.reason;
const msg = reason?.stack || reason?.message || String(reason);
console.error("unhandledrejection", ev);
log(`FATAL(unhandledrejection): ${msg}`);
});