Skip to content

Commit f1cd225

Browse files
committed
feat: Add timeout command to wait for a specified duration
Closes #86
1 parent 2b80892 commit f1cd225

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ private static void registerCommands(Map<String, Command> commands) {
108108
commands.put("set", new SetCommand());
109109
commands.put("systeminfo", new SysteminfoCommand());
110110
commands.put("pause", new PauseCommand());
111+
commands.put("timeout", new TimeoutCommand());
111112
}
112113
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
import java.io.IOException;
6+
7+
public class TimeoutCommand implements Command {
8+
@Override
9+
public void execute(String[] args, ShellContext context) {
10+
int seconds = -1;
11+
boolean hasSecondsArg = false;
12+
boolean hasSlashT = false;
13+
boolean noBreak = false;
14+
15+
for (int i = 0; i < args.length; i++) {
16+
if (args[i].equalsIgnoreCase("/t")
17+
&& i + 1 < args.length
18+
&& args[i + 1].matches("[+-]?\\d+")) {
19+
if (hasSlashT) {
20+
System.out.println(
21+
"Error: Invalid syntax. '/t' option is not allowed more than '1' time(s).");
22+
return;
23+
}
24+
seconds = Integer.parseInt(args[i + 1]);
25+
i++;
26+
hasSecondsArg = true;
27+
hasSlashT = true;
28+
} else if (args[i].equalsIgnoreCase("/nobreak")) {
29+
if (noBreak) {
30+
System.out.println(
31+
"Error: Invalid syntax. '/nobreak' option is not allowed more than '1' time(s).");
32+
return;
33+
}
34+
noBreak = true;
35+
} else if (args[i].matches("[+-]?\\d+")) {
36+
if (hasSecondsArg) {
37+
System.out.println(
38+
"Error: Invalid syntax. Default option is not allowed more than '1' time(s).");
39+
return;
40+
}
41+
seconds = Integer.parseInt(args[i]);
42+
hasSecondsArg = true;
43+
} else if (args[i].equalsIgnoreCase("/t")) {
44+
System.out.println("Error: Invalid syntax. Value expected for '/t'.");
45+
return;
46+
}
47+
}
48+
49+
if (!hasSecondsArg) {
50+
System.out.println("Error: Invalid syntax. Seconds value is required.");
51+
return;
52+
}
53+
54+
if (seconds < -1 || seconds > 99999) {
55+
System.out.println(
56+
"Error: Invalid value for timeout specified. Valid range is 0-99999 seconds.");
57+
return;
58+
}
59+
60+
if (seconds == -1) {
61+
System.out.println();
62+
PauseCommand pauseCmd = new PauseCommand();
63+
pauseCmd.execute(new String[0], context);
64+
return;
65+
}
66+
67+
Thread inputThread =
68+
new Thread(
69+
() -> {
70+
try {
71+
System.in.read();
72+
} catch (IOException e) {
73+
// Ignore
74+
}
75+
});
76+
inputThread.setDaemon(true);
77+
78+
if (!noBreak) {
79+
inputThread.start();
80+
}
81+
System.out.println();
82+
83+
for (; seconds > 0; seconds--) {
84+
if (!noBreak && !inputThread.isAlive()) {
85+
System.out.println("\r");
86+
System.out.println();
87+
return;
88+
}
89+
90+
System.out.print(
91+
"\rWaiting for "
92+
+ seconds
93+
+ " seconds, press "
94+
+ (noBreak ? "CTRL+C to quit ..." : "enter key to continue ..."));
95+
System.out.flush();
96+
try {
97+
Thread.sleep(1000);
98+
} catch (InterruptedException e) {
99+
if (noBreak) {
100+
continue;
101+
}
102+
System.out.println();
103+
Thread.currentThread().interrupt();
104+
break;
105+
}
106+
}
107+
try {
108+
while (System.in.available() > 0) {
109+
System.in.read();
110+
}
111+
} catch (IOException e) {
112+
// Ignore
113+
}
114+
System.out.println("\r");
115+
System.out.println();
116+
}
117+
118+
@Override
119+
public String description() {
120+
return "Sets a timeout for command execution.";
121+
}
122+
123+
@Override
124+
public String usage() {
125+
return "timeout <seconds>\n"
126+
+ "timeout /t <seconds>\n"
127+
+ "timeout /t <seconds> /nobreak\n"
128+
+ "timeout /t -1";
129+
}
130+
}

0 commit comments

Comments
 (0)