Spaces:
Configuration error
Configuration error
Commit ·
0a3b0ff
1
Parent(s): f949ee8
commit initial 09-12-2025 020
Browse files205 unmodified lines20620720820921021120921021121221321421521321421621721821922035 unmodified lines256257258256257259260261262259263264265266262263267268269270271267268272273274275276277278279273274275276277278279280281282283284285286287288289290291292293294295280281282283284285286287288289290291292293294295296297298299297298300301302303304305306307308300309310311312205 unmodified lines // When user types into terminal prompt and presses Enter, append that input to accumStdin + "\n", // then re-run the program with updated accumStdin. Repeat until no input cues.
const resetTerminal = () => { setTerminalLines([]); setTerminalInput("");const resetTerminal = (keepAccum = false) => { setTerminalLines([]); setTerminalInput(""); // only reset accumulated stdin if caller explicitly wants to clear it if (!keepAccum) { setAccumStdin(""); } setAwaitingInput(false); }; setAwaitingInput(false);};
const appendTerminal = (text) => { setTerminalLines((prev) => [...prev, text]);35 unmodified lines
// If code likely needs input and accumStdin is empty, ask for initial input const needs = codeNeedsInput(node.content, selectedLang); if (needs && !accumStdin) { // collect multi-line input: user can paste multiple lines separated by \n let stdinToSend = accumStdin || stdin || "";
if (needs && !stdinToSend) { const userText = window.prompt( "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.", "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.", "" );
if (userText == null) { // user pressed cancel: proceed but warn if (userText === null) { // user cancelled prompt -> do nothing (abort run) appendTerminal("[Run cancelled by user]"); return; } // userText may contain literal backslash-n if pasted; keep it as-is. // Normalize: if the user typed using Enter, it's already multi-line.
// normalize any literal "\n" sequences into real newlines (helps users pasting) const prepared = userText.replace(/\\n/g, "\n"); stdinToSend = prepared; // set accumStdin state (so subsequent interactive sends append to it) setAccumStdin(prepared); }
// clear terminal state and start a fresh run resetTerminal(); setIsRunning(true); setProblems([]); setOutput(""); try { // call backend with current accumStdin (empty on first run) const res = await runCode(node.content, selectedLang, accumStdin || stdin || ""); const out = res.output ?? ""; setOutput(out); appendTerminal(out); setProblems(res.error ? parseProblems(res.output) : []);
if (outputLooksForInput(out)) { // program likely wants input: show prompt setAwaitingInput(true); } else { setAwaitingInput(false); } } catch (err) { const e = String(err); setOutput(e); appendTerminal(e); // start a fresh terminal *but keep accumStdin* because we want to use it during run // pass keepAccum = true to avoid clearing accumStdin resetTerminal(true); appendTerminal(`[Running with stdin length=${stdinToSend ? stdinToSend.length : 0}]`); setIsRunning(true); setProblems([]); setOutput("");
try { // IMPORTANT: use local stdinToSend (not state) so the run uses the value right away const res = await runCode(node.content, selectedLang, stdinToSend); const out = res.output ?? ""; setOutput(out); appendTerminal(out); setProblems(res.error ? parseProblems(res.output) : []);
if (outputLooksForInput(out)) { setAwaitingInput(true); } else { setAwaitingInput(false); } finally { setIsRunning(false); } } catch (err) { const e = String(err); setOutput(e); appendTerminal(e); setAwaitingInput(false); } finally { setIsRunning(false); } };};
// Called when user types input into terminal and presses Enter const sendTerminalInput = async () => {src/App.js+47-38