Spaces:
Running
Running
Commit ·
0a3b0ff
1
Parent(s): f949ee8
commit initial 09-12-2025 020
Browse files- src/App.js +47 -38
src/App.js
CHANGED
|
@@ -206,12 +206,15 @@ function App() {
|
|
| 206 |
// When user types into terminal prompt and presses Enter, append that input to accumStdin + "\n",
|
| 207 |
// then re-run the program with updated accumStdin. Repeat until no input cues.
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
| 212 |
setAccumStdin("");
|
| 213 |
-
|
| 214 |
-
|
|
|
|
| 215 |
|
| 216 |
const appendTerminal = (text) => {
|
| 217 |
setTerminalLines((prev) => [...prev, text]);
|
|
@@ -253,51 +256,57 @@ function codeNeedsInput(code, langId) {
|
|
| 253 |
|
| 254 |
// If code likely needs input and accumStdin is empty, ask for initial input
|
| 255 |
const needs = codeNeedsInput(node.content, selectedLang);
|
| 256 |
-
|
| 257 |
-
|
|
|
|
| 258 |
const userText = window.prompt(
|
| 259 |
-
"This program appears to request console input (e.g. input() / Scanner).\n\nEnter input lines
|
| 260 |
""
|
| 261 |
);
|
| 262 |
-
|
| 263 |
-
|
|
|
|
| 264 |
appendTerminal("[Run cancelled by user]");
|
| 265 |
return;
|
| 266 |
}
|
| 267 |
-
|
| 268 |
-
//
|
| 269 |
const prepared = userText.replace(/\\n/g, "\n");
|
|
|
|
|
|
|
| 270 |
setAccumStdin(prepared);
|
| 271 |
}
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
} catch (err) {
|
| 293 |
-
const e = String(err);
|
| 294 |
-
setOutput(e);
|
| 295 |
-
appendTerminal(e);
|
| 296 |
setAwaitingInput(false);
|
| 297 |
-
} finally {
|
| 298 |
-
setIsRunning(false);
|
| 299 |
}
|
| 300 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
// Called when user types input into terminal and presses Enter
|
| 303 |
const sendTerminalInput = async () => {
|
|
|
|
| 206 |
// When user types into terminal prompt and presses Enter, append that input to accumStdin + "\n",
|
| 207 |
// then re-run the program with updated accumStdin. Repeat until no input cues.
|
| 208 |
|
| 209 |
+
const resetTerminal = (keepAccum = false) => {
|
| 210 |
+
setTerminalLines([]);
|
| 211 |
+
setTerminalInput("");
|
| 212 |
+
// only reset accumulated stdin if caller explicitly wants to clear it
|
| 213 |
+
if (!keepAccum) {
|
| 214 |
setAccumStdin("");
|
| 215 |
+
}
|
| 216 |
+
setAwaitingInput(false);
|
| 217 |
+
};
|
| 218 |
|
| 219 |
const appendTerminal = (text) => {
|
| 220 |
setTerminalLines((prev) => [...prev, text]);
|
|
|
|
| 256 |
|
| 257 |
// If code likely needs input and accumStdin is empty, ask for initial input
|
| 258 |
const needs = codeNeedsInput(node.content, selectedLang);
|
| 259 |
+
let stdinToSend = accumStdin || stdin || "";
|
| 260 |
+
|
| 261 |
+
if (needs && !stdinToSend) {
|
| 262 |
const userText = window.prompt(
|
| 263 |
+
"This program appears to request console input (e.g. input() / Scanner).\n\nEnter input lines — use Enter for new lines or paste multi-line input. Leave empty to run without input.",
|
| 264 |
""
|
| 265 |
);
|
| 266 |
+
|
| 267 |
+
if (userText === null) {
|
| 268 |
+
// user cancelled prompt -> do nothing (abort run)
|
| 269 |
appendTerminal("[Run cancelled by user]");
|
| 270 |
return;
|
| 271 |
}
|
| 272 |
+
|
| 273 |
+
// normalize any literal "\n" sequences into real newlines (helps users pasting)
|
| 274 |
const prepared = userText.replace(/\\n/g, "\n");
|
| 275 |
+
stdinToSend = prepared;
|
| 276 |
+
// set accumStdin state (so subsequent interactive sends append to it)
|
| 277 |
setAccumStdin(prepared);
|
| 278 |
}
|
| 279 |
|
| 280 |
+
// start a fresh terminal *but keep accumStdin* because we want to use it during run
|
| 281 |
+
// pass keepAccum = true to avoid clearing accumStdin
|
| 282 |
+
resetTerminal(true);
|
| 283 |
+
appendTerminal(`[Running with stdin length=${stdinToSend ? stdinToSend.length : 0}]`);
|
| 284 |
+
setIsRunning(true);
|
| 285 |
+
setProblems([]);
|
| 286 |
+
setOutput("");
|
| 287 |
+
|
| 288 |
+
try {
|
| 289 |
+
// IMPORTANT: use local stdinToSend (not state) so the run uses the value right away
|
| 290 |
+
const res = await runCode(node.content, selectedLang, stdinToSend);
|
| 291 |
+
const out = res.output ?? "";
|
| 292 |
+
setOutput(out);
|
| 293 |
+
appendTerminal(out);
|
| 294 |
+
setProblems(res.error ? parseProblems(res.output) : []);
|
| 295 |
+
|
| 296 |
+
if (outputLooksForInput(out)) {
|
| 297 |
+
setAwaitingInput(true);
|
| 298 |
+
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
setAwaitingInput(false);
|
|
|
|
|
|
|
| 300 |
}
|
| 301 |
+
} catch (err) {
|
| 302 |
+
const e = String(err);
|
| 303 |
+
setOutput(e);
|
| 304 |
+
appendTerminal(e);
|
| 305 |
+
setAwaitingInput(false);
|
| 306 |
+
} finally {
|
| 307 |
+
setIsRunning(false);
|
| 308 |
+
}
|
| 309 |
+
};
|
| 310 |
|
| 311 |
// Called when user types input into terminal and presses Enter
|
| 312 |
const sendTerminalInput = async () => {
|