-
Couldn't load subscription status.
- Fork 19
feat: Add timeout command to wait for a specified duration #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anshumanjadiya1102
merged 8 commits into
Drive-for-Java:main
from
nihaltp:feature/timeout
Oct 29, 2025
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f1cd225
feat: Add timeout command to wait for a specified duration
nihaltp 608ce62
Merge branch 'main' of https://github.com/Drive-for-Java/MyCMD into f…
nihaltp 7252ece
refactor: Improve input handling in TimeoutCommand for better interru…
nihaltp 1fd6eca
refactor: Improve input handling in TimeoutCommand for better interru…
nihaltp 82fb7b8
Merge branch 'feature/timeout' of https://github.com/nihaltp/MyCMD in…
nihaltp 28f1eaa
Merge branch 'feature/timeout' of https://github.com/nihaltp/MyCMD in…
nihaltp e4b7a88
Merge branch 'feature/timeout' of https://github.com/nihaltp/MyCMD in…
nihaltp 7d5de1a
refactor: Enhance input validation and interrupt handling in TimeoutC…
nihaltp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package com.mycmd.commands; | ||
|
|
||
| import com.mycmd.Command; | ||
| import com.mycmd.ShellContext; | ||
| import java.io.IOException; | ||
|
|
||
| public class TimeoutCommand implements Command { | ||
| @Override | ||
| public void execute(String[] args, ShellContext context) { | ||
| int seconds = -1; | ||
| boolean hasSecondsArg = false; | ||
| boolean hasSlashT = false; | ||
| boolean noBreak = false; | ||
|
|
||
| for (int i = 0; i < args.length; i++) { | ||
| if (args[i].equalsIgnoreCase("/t") | ||
| && i + 1 < args.length | ||
| && args[i + 1].matches("[+-]?\\d+")) { | ||
| if (hasSlashT) { | ||
| System.out.println( | ||
| "Error: Invalid syntax. '/t' option is not allowed more than '1' time(s)."); | ||
| return; | ||
| } | ||
| seconds = Integer.parseInt(args[i + 1]); | ||
| i++; | ||
| hasSecondsArg = true; | ||
| hasSlashT = true; | ||
| } else if (args[i].equalsIgnoreCase("/nobreak")) { | ||
| if (noBreak) { | ||
| System.out.println( | ||
| "Error: Invalid syntax. '/nobreak' option is not allowed more than '1' time(s)."); | ||
| return; | ||
| } | ||
| noBreak = true; | ||
| } else if (args[i].matches("[+-]?\\d+")) { | ||
| if (hasSecondsArg) { | ||
| System.out.println( | ||
| "Error: Invalid syntax. Default option is not allowed more than '1' time(s)."); | ||
| return; | ||
| } | ||
| seconds = Integer.parseInt(args[i]); | ||
| hasSecondsArg = true; | ||
| } else if (args[i].equalsIgnoreCase("/t")) { | ||
| System.out.println("Error: Invalid syntax. Value expected for '/t'."); | ||
| return; | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!hasSecondsArg) { | ||
| System.out.println("Error: Invalid syntax. Seconds value is required."); | ||
| return; | ||
| } | ||
|
|
||
| if (seconds < -1 || seconds > 99999) { | ||
| System.out.println( | ||
| "Error: Invalid value for timeout specified. Valid range is 0-99999 seconds."); | ||
| return; | ||
| } | ||
|
|
||
| if (seconds == -1) { | ||
| System.out.println(); | ||
| PauseCommand pauseCmd = new PauseCommand(); | ||
| pauseCmd.execute(new String[0], context); | ||
| return; | ||
| } | ||
|
|
||
| AtomicBoolean interrupted = new AtomicBoolean(false); | ||
| Thread inputThread = null; | ||
|
|
||
| if (!noBreak) { | ||
| inputThread = | ||
| new Thread( | ||
| () -> { | ||
| try { | ||
| int r; | ||
| // Read until a newline is encountered so we only exit on Enter | ||
| while ((r = System.in.read()) != -1) { | ||
| if (r == '\n') { | ||
| interrupted.set(true); | ||
| break; | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| // Ignore: if System.in is closed or an I/O error occurs we | ||
| // cannot reliably wait for Enter; treat as no-interrupt. | ||
| } | ||
| }); | ||
| inputThread.setDaemon(true); | ||
| inputThread.start(); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| System.out.println(); | ||
|
|
||
| for (; seconds > 0; seconds--) { | ||
| if (!noBreak && interrupted.get()) { | ||
| System.out.println("\r"); | ||
| System.out.println(); | ||
| if (inputThread != null && inputThread.isAlive()) { | ||
| inputThread.interrupt(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| System.out.print( | ||
| "\rWaiting for " | ||
| + seconds | ||
| + " seconds, press " | ||
| + (noBreak ? "CTRL+C to quit ..." : "enter key to continue ...")); | ||
| System.out.flush(); | ||
| try { | ||
| Thread.sleep(1000); | ||
| } catch (InterruptedException e) { | ||
| if (noBreak) { | ||
| continue; | ||
| } | ||
| System.out.println(); | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| // Drain any remaining bytes so subsequent commands don't immediately see | ||
| // leftover input. This is a best-effort drain; System.in.available() may | ||
| // not be supported on all streams, but for typical console streams it helps. | ||
| while (System.in.available() > 0) { | ||
| System.in.read(); | ||
| } | ||
| } catch (IOException e) { | ||
| // Ignore: if we can't drain the stream it's non-fatal; any leftover input | ||
| // will be handled by the next read and is acceptable. | ||
| } | ||
|
|
||
| System.out.println("\r"); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| @Override | ||
| public String description() { | ||
| return "Sets a timeout for command execution."; | ||
| } | ||
|
|
||
| @Override | ||
| public String usage() { | ||
| return "timeout <seconds>\n" | ||
| + "timeout /t <seconds>\n" | ||
| + "timeout /t <seconds> /nobreak\n" | ||
| + "timeout /t -1"; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.