Spaces:
Configuration error
Configuration error
Commit ·
f949ee8
1
Parent(s): 921b162
commit initial 09-12-2025 019
Browse files216 unmodified lines21721821922022122222322422022122222322422522622722822923023123223323423523623723823924024124224324424524624722622722824824925025125225325425525625725825926026126226326426526626726826927027127227327426 unmodified lines301302303263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292304305306307308309310311312313314315316317318319320321322323324325326327328329330331294295332333334335336337338297339340341342216 unmodified lines setTerminalLines((prev) => [...prev, text]); };
const handleRun = async () => { const node = getNodeByPath(tree, activePath); if (!node || node.type !== "file") { setOutput("Select a file to run."); return; // helper: detect input calls in source (python/java/js)function codeNeedsInput(code, langId) { if (!code) return false; try { const c = code.toString(); if (langId === "python") { return /\binput\s*\(/i.test(c); } if (langId === "java") { return /\bScanner\s*\(|\bnext(Int|Line|Double|())\b/i.test(c) || /\bSystem\.console\(\)/i.test(c); } if (langId === "javascript") { // node.js stdin reads or prompt usage heuristics return /process\.stdin|readline|prompt\(|window\.prompt/i.test(c); } // fallback: look for common words return /\binput\b|\bScanner\b|\bnextInt\b|prompt\(/i.test(c); } catch { return false; }}
const handleRun = async () => { const node = getNodeByPath(tree, activePath); if (!node || node.type !== "file") { setOutput("Select a file to run."); return; } const selectedLang = LANGUAGE_OPTIONS.find((l) => node.name.endsWith(l.ext))?.id; if (!selectedLang || !RUNNABLE_LANGS.includes(selectedLang)) { setOutput(`⚠️ Run not supported for this file type.`); const selectedLang = LANGUAGE_OPTIONS.find((l) => node.name.endsWith(l.ext))?.id; if (!selectedLang || !RUNNABLE_LANGS.includes(selectedLang)) { setOutput(`⚠️ Run not supported for this file type.`); return; }
// 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 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.", "" ); if (userText == null) { // user pressed cancel: proceed but warn 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. const prepared = userText.replace(/\\n/g, "\n"); setAccumStdin(prepared); }
// clear terminal state and start a fresh run resetTerminal();26 unmodified lines
// Called when user types input into terminal and presses Enter const sendTerminalInput = async () => { if (!awaitingInput) return; const userText = terminalInput; // echo user input in terminal appendTerminal(`> ${userText}`); const newAccum = (accumStdin || "") + userText + "\n"; setAccumStdin(newAccum); setTerminalInput(""); setIsRunning(true); try { // re-run program with updated stdin const node = getNodeByPath(tree, activePath); if (!node || node.type !== "file") { appendTerminal("No file selected."); setAwaitingInput(false); return; } const selectedLang = LANGUAGE_OPTIONS.find((l) => node.name.endsWith(l.ext))?.id; const res = await runCode(node.content, selectedLang, newAccum); const out = res.output ?? ""; setOutput(out); appendTerminal(out); setProblems(res.error ? parseProblems(res.output) : []); // detect if program still waiting if (outputLooksForInput(out)) { setAwaitingInput(true); } else { setAwaitingInput(false); } } catch (err) { appendTerminal(String(err)); if (!currentNode || currentNode.type !== "file") { appendTerminal("No file selected."); setAwaitingInput(false); return; }
// If not already in awaitingInput (heuristic missed), still allow sending input: if (!awaitingInput && !isRunning && terminalInput.trim()) { appendTerminal("[Info] Sending input to program (re-run)."); }
const userText = terminalInput; appendTerminal(`> ${userText}`); const newAccum = (accumStdin || "") + userText + "\n"; setAccumStdin(newAccum); setTerminalInput(""); setIsRunning(true); try { const selectedLang = LANGUAGE_OPTIONS.find((l) => currentNode.name.endsWith(l.ext))?.id; const res = await runCode(currentNode.content, selectedLang, newAccum); 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) { appendTerminal(String(err)); setAwaitingInput(false); } finally { setIsRunning(false); } };};
// Allow pressing Enter to send input const onTerminalKeyDown = (e) => {src/App.js+83-41