Skip to content

Commit 7fc8530

Browse files
committed
feat: add uptime command and moved target dir to .gitignore
1 parent c338ad3 commit 7fc8530

File tree

6 files changed

+53
-28
lines changed

6 files changed

+53
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ private static void registerCommands(Map<String, Command> commands) {
7878
commands.put("date", new DateCommand());
7979
commands.put("history", new HistoryCommand());
8080
commands.put("pwd", new PwdCommand());
81+
commands.put("uptime", new UptimeCommand());
8182
}
8283
}

src/main/java/com/mycmd/ShellContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
public class ShellContext {
88
private File currentDir;
99
private List<String> commandHistory;
10+
private final long startTime;
1011
private static final int MAX_HISTORY = 10;
1112

1213
public ShellContext() {
1314
this.currentDir = new File(System.getProperty("user.dir"));
1415
this.commandHistory = new ArrayList<>();
16+
this.startTime = System.currentTimeMillis();
1517
}
1618

1719
public File getCurrentDir() {
@@ -22,6 +24,10 @@ public void setCurrentDir(File dir) {
2224
this.currentDir = dir;
2325
}
2426

27+
public long getStartTime() {
28+
return startTime;
29+
}
30+
2531
public List<String> getCommandHistory() {
2632
return commandHistory;
2733
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
/**
7+
* Displays how long the shell has been running since startup.
8+
*
9+
* This command shows the uptime of the MyCMD shell session, displaying
10+
* the time elapsed since the shell was started. The uptime is calculated
11+
* from the shell start time stored in the ShellContext.
12+
*
13+
* Usage:
14+
* - uptime : Display shell uptime in hours, minutes, and seconds
15+
*
16+
* The output format shows the uptime as "Up since Xh Ym Zs" where:
17+
* - X is hours
18+
* - Y is minutes
19+
* - Z is seconds
20+
*
21+
* This is useful for monitoring how long a shell session has been active.
22+
*/
23+
public class UptimeCommand implements Command {
24+
@Override
25+
public void execute(String[] args, ShellContext context) {
26+
long uptimeMillis = System.currentTimeMillis() - context.getStartTime();
27+
28+
long totalSeconds = uptimeMillis / 1000;
29+
long hours = totalSeconds / 3600;
30+
long minutes = (totalSeconds % 3600) / 60;
31+
long seconds = totalSeconds % 60;
32+
33+
System.out.printf("Up since %dh %dm %ds%n", hours, minutes, seconds);
34+
}
35+
36+
@Override
37+
public String description() {
38+
return "Display how long the shell has been running since startup.";
39+
}
40+
41+
@Override
42+
public String usage() {
43+
return "uptime";
44+
}
45+
}

target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst

Whitespace-only changes.

target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)