Skip to content

Commit 894f0ac

Browse files
Merge pull request #59 from yuvii-b/feature/new-cmds
feat: add essential shell commands - pwd, uptime and clearhistory
2 parents 8adcde5 + 597d971 commit 894f0ac

File tree

9 files changed

+132
-30
lines changed

9 files changed

+132
-30
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,8 @@ private static void registerCommands(Map<String, Command> commands) {
7777
commands.put("tree", new TreeCommand());
7878
commands.put("date", new DateCommand());
7979
commands.put("history", new HistoryCommand());
80+
commands.put("pwd", new PwdCommand());
81+
commands.put("uptime", new UptimeCommand());
82+
commands.put("clearhistory", new ClearHistoryCommand());
8083
}
8184
}

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

Lines changed: 10 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
}
@@ -35,6 +41,10 @@ public void addToHistory(String command) {
3541
}
3642
}
3743

44+
public void clearHistory() {
45+
commandHistory.clear();
46+
}
47+
3848
/**
3949
* Resolve the given path (absolute or relative) to a File using the current directory.
4050
* If the provided path is absolute, returns it directly; otherwise returns a File rooted at currentDir.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
/**
7+
* Clears the stored command history.
8+
*
9+
* This command removes all previously executed commands from the shell's
10+
* command history. After execution, the history list will be empty and
11+
* the history command will show no previous commands.
12+
*
13+
* Usage:
14+
* - clearhistory : Clear all stored command history
15+
*
16+
* This is useful for privacy purposes or when you want to start fresh
17+
* with a clean command history. The history is cleared immediately
18+
* and cannot be recovered.
19+
*/
20+
public class ClearHistoryCommand implements Command {
21+
@Override
22+
public void execute(String[] args, ShellContext context) {
23+
context.clearHistory();
24+
System.out.println("Command history cleared.");
25+
}
26+
27+
@Override
28+
public String description() {
29+
return "Clear the stored command history.";
30+
}
31+
32+
@Override
33+
public String usage() {
34+
return "clearhistory";
35+
}
36+
}

src/main/java/com/mycmd/commands/HelpCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public HelpCommand(Map<String, Command> commands) {
2020
@Override
2121
public void execute(String[] args, ShellContext context) {
2222
// Detailed help for a specific command
23-
if (args.length > 1) {
24-
String cmdName = args[1];
23+
if (args.length > 0) {
24+
String cmdName = args[0];
2525
Command cmd = commands.get(cmdName);
2626

2727
if (cmd != null) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
/**
7+
* Prints the current working directory.
8+
*
9+
* This command displays the absolute path of the current working directory
10+
* stored in the shell context. It's equivalent to the Unix/Linux 'pwd' command
11+
* and provides the same functionality as 'cd' without arguments.
12+
*
13+
* Usage:
14+
* - pwd : Print the current working directory path
15+
*
16+
* The command always prints the absolute path of the current directory,
17+
* making it useful for scripts and when you need to know your exact location
18+
* in the file system.
19+
*/
20+
public class PwdCommand implements Command {
21+
@Override
22+
public void execute(String[] args, ShellContext context) {
23+
System.out.println(context.getCurrentDir().getAbsolutePath());
24+
}
25+
26+
@Override
27+
public String description() {
28+
return "Print the current working directory path.";
29+
}
30+
31+
@Override
32+
public String usage() {
33+
return "pwd";
34+
}
35+
}
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)