FrederickSundeep commited on
Commit
0a3b0ff
·
1 Parent(s): f949ee8

commit initial 09-12-2025 020

Browse files
Files changed (1) hide show
  1. 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
- const resetTerminal = () => {
210
- setTerminalLines([]);
211
- setTerminalInput("");
 
 
212
  setAccumStdin("");
213
- setAwaitingInput(false);
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
- if (needs && !accumStdin) {
257
- // collect multi-line input: user can paste multiple lines separated by \n
 
258
  const userText = window.prompt(
259
- "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.",
260
  ""
261
  );
262
- if (userText == null) {
263
- // user pressed cancel: proceed but warn
 
264
  appendTerminal("[Run cancelled by user]");
265
  return;
266
  }
267
- // userText may contain literal backslash-n if pasted; keep it as-is.
268
- // Normalize: if the user typed using Enter, it's already multi-line.
269
  const prepared = userText.replace(/\\n/g, "\n");
 
 
270
  setAccumStdin(prepared);
271
  }
272
 
273
- // clear terminal state and start a fresh run
274
- resetTerminal();
275
- setIsRunning(true);
276
- setProblems([]);
277
- setOutput("");
278
- try {
279
- // call backend with current accumStdin (empty on first run)
280
- const res = await runCode(node.content, selectedLang, accumStdin || stdin || "");
281
- const out = res.output ?? "";
282
- setOutput(out);
283
- appendTerminal(out);
284
- setProblems(res.error ? parseProblems(res.output) : []);
285
-
286
- if (outputLooksForInput(out)) {
287
- // program likely wants input: show prompt
288
- setAwaitingInput(true);
289
- } else {
290
- setAwaitingInput(false);
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 () => {