Spaces:
Running
Running
Commit ·
950fad2
1
Parent(s): cae9f20
commit initial 09-12-2025 025
Browse files- src/App.js +33 -39
src/App.js
CHANGED
|
@@ -256,57 +256,51 @@ function codeNeedsInput(code, langId) {
|
|
| 256 |
|
| 257 |
// If code likely needs input and accumStdin is empty, ask for initial input
|
| 258 |
const needs = codeNeedsInput(node.content, selectedLang);
|
| 259 |
-
|
| 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
|
| 264 |
""
|
| 265 |
);
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
// user cancelled prompt -> do nothing (abort run)
|
| 269 |
appendTerminal("[Run cancelled by user]");
|
| 270 |
return;
|
| 271 |
}
|
| 272 |
-
|
| 273 |
-
//
|
| 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 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
setAwaitingInput(false);
|
|
|
|
|
|
|
| 300 |
}
|
| 301 |
-
}
|
| 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 () => {
|
|
|
|
| 256 |
|
| 257 |
// If code likely needs input and accumStdin is empty, ask for initial input
|
| 258 |
const needs = codeNeedsInput(node.content, selectedLang);
|
| 259 |
+
if (needs && !accumStdin) {
|
| 260 |
+
// collect multi-line input: user can paste multiple lines separated by \n
|
|
|
|
| 261 |
const userText = window.prompt(
|
| 262 |
+
"This program appears to request console input (e.g. input() / Scanner).\n\nEnter input lines separated by a newline (\\n). Leave empty to run without input.",
|
| 263 |
""
|
| 264 |
);
|
| 265 |
+
if (userText == null) {
|
| 266 |
+
// user pressed cancel: proceed but warn
|
|
|
|
| 267 |
appendTerminal("[Run cancelled by user]");
|
| 268 |
return;
|
| 269 |
}
|
| 270 |
+
// userText may contain literal backslash-n if pasted; keep it as-is.
|
| 271 |
+
// Normalize: if the user typed using Enter, it's already multi-line.
|
| 272 |
const prepared = userText.replace(/\\n/g, "\n");
|
|
|
|
|
|
|
| 273 |
setAccumStdin(prepared);
|
| 274 |
}
|
| 275 |
|
| 276 |
+
// clear terminal state and start a fresh run
|
| 277 |
+
resetTerminal();
|
| 278 |
+
setIsRunning(true);
|
| 279 |
+
setProblems([]);
|
| 280 |
+
setOutput("");
|
| 281 |
+
try {
|
| 282 |
+
// call backend with current accumStdin (empty on first run)
|
| 283 |
+
const res = await runCode(node.content, selectedLang, accumStdin || stdin || "");
|
| 284 |
+
const out = res.output ?? "";
|
| 285 |
+
setOutput(out);
|
| 286 |
+
appendTerminal(out);
|
| 287 |
+
setProblems(res.error ? parseProblems(res.output) : []);
|
| 288 |
+
|
| 289 |
+
if (outputLooksForInput(out)) {
|
| 290 |
+
// program likely wants input: show prompt
|
| 291 |
+
setAwaitingInput(true);
|
| 292 |
+
} else {
|
| 293 |
+
setAwaitingInput(false);
|
| 294 |
+
}
|
| 295 |
+
} catch (err) {
|
| 296 |
+
const e = String(err);
|
| 297 |
+
setOutput(e);
|
| 298 |
+
appendTerminal(e);
|
| 299 |
setAwaitingInput(false);
|
| 300 |
+
} finally {
|
| 301 |
+
setIsRunning(false);
|
| 302 |
}
|
| 303 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
// Called when user types input into terminal and presses Enter
|
| 306 |
const sendTerminalInput = async () => {
|