Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CheckList.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

## ⚙️ Automation & Scripting
- [x] echo
- [ ] pause
- [x] pause
- [x] cls
- [x] exit
- [x] title
Expand Down Expand Up @@ -188,7 +188,7 @@
- [x] md / mkdir
- [x] move
- [ ] path
- [ ] pause
- [x] pause
- [ ] popd
- [ ] prompt
- [ ] pushd
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/mycmd/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ private static void registerCommands(Map<String, Command> commands) {
commands.put("rename", new RenameCommand());
commands.put("set", new SetCommand());
commands.put("systeminfo", new SysteminfoCommand());
commands.put("pause", new PauseCommand());
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/mycmd/commands/PauseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mycmd.commands;

import com.mycmd.Command;
import com.mycmd.ShellContext;

public class PauseCommand implements Command {
/**
* Execute the pause command.
*
* <p>Prints "Press Enter to continue..." and waits for the user to press Enter before continuing.</p>
* <p>If an exception occurs during the pause, it is ignored.</p>
*
* @param args The arguments to the command.
* @param context The context of the shell.
*/
@Override
public void execute(String[] args, ShellContext context) {
System.out.println("Press Enter to continue...");
try {
System.in.read();
} catch (Exception e) {
// Ignore exceptions during pause
}
}
Comment on lines +17 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Input buffering conflict causes command corruption.

The main loop in App.java uses a Scanner reading from System.in, but this implementation directly calls System.in.read(). This creates two critical problems:

  1. Incomplete read: System.in.read() only consumes a single byte, not the entire line. If the user types anything before pressing Enter, leftover characters remain in the input buffer.

  2. Buffer conflict: Direct System.in access bypasses the Scanner's buffering. Remaining characters will be consumed by the next sc.nextLine() call in the main loop, corrupting the subsequent command.

Example failure scenario:

> pause
Press Enter to continue...
hello[Enter]    ← user input
> ello          ← corrupted next prompt (consumes remaining "ello\n")
Unknown command: 'ello'

Recommended fix: Store the Scanner in ShellContext and use it consistently across all commands.

  1. Add Scanner to ShellContext:
// In ShellContext.java
private Scanner scanner;

public ShellContext(Scanner scanner) {
    this.scanner = scanner;
    // ... rest of initialization
}

public Scanner getScanner() {
    return scanner;
}
  1. Update App.java to pass Scanner to context:
Scanner sc = new Scanner(System.in);
ShellContext context = new ShellContext(sc);
  1. Update PauseCommand to use the shared Scanner:
 @Override
 public void execute(String[] args, ShellContext context) {
     System.out.println("Press Enter to continue...");
-    try {
-        System.in.read();
-    } catch (Exception e) {
-        // Ignore exceptions during pause
-    }
+    context.getScanner().nextLine();
 }

This ensures all input operations use the same buffered input source, preventing buffer corruption.

🤖 Prompt for AI Agents
In src/main/java/com/mycmd/commands/PauseCommand.java around lines 17 to 24, the
command uses System.in.read() which only consumes a single byte and bypasses the
Scanner used by App.java causing leftover input and buffer conflicts; replace
direct System.in access by retrieving the shared Scanner from ShellContext (add
a Scanner field and getter in ShellContext and ensure App constructs
ShellContext with the single Scanner it created) and then call
scanner.nextLine() in PauseCommand to consume the entire line and wait for
Enter, handling exceptions as needed.


@Override
public String description() {
return "Pause execution until user presses Enter.";
}

@Override
public String usage() {
return "pause";
}
}