FrederickSundeep commited on
Commit
950fad2
·
1 Parent(s): cae9f20

commit initial 09-12-2025 025

Browse files
Files changed (1) hide show
  1. 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
- 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 () => {
 
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 () => {