From c338ad367913f49c1840a18b9e02346b4ccf39ab Mon Sep 17 00:00:00 2001 From: yuvaraj Date: Sat, 18 Oct 2025 16:17:43 +0530 Subject: [PATCH 1/3] feat: add pwd command and fix bug in help command --- src/main/java/com/mycmd/App.java | 1 + .../java/com/mycmd/commands/HelpCommand.java | 4 +-- .../java/com/mycmd/commands/PwdCommand.java | 35 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/mycmd/commands/PwdCommand.java diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 023d390..e04c4a6 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -77,5 +77,6 @@ private static void registerCommands(Map commands) { commands.put("tree", new TreeCommand()); commands.put("date", new DateCommand()); commands.put("history", new HistoryCommand()); + commands.put("pwd", new PwdCommand()); } } diff --git a/src/main/java/com/mycmd/commands/HelpCommand.java b/src/main/java/com/mycmd/commands/HelpCommand.java index 7b78072..052aaa9 100644 --- a/src/main/java/com/mycmd/commands/HelpCommand.java +++ b/src/main/java/com/mycmd/commands/HelpCommand.java @@ -20,8 +20,8 @@ public HelpCommand(Map commands) { @Override public void execute(String[] args, ShellContext context) { // Detailed help for a specific command - if (args.length > 1) { - String cmdName = args[1]; + if (args.length > 0) { + String cmdName = args[0]; Command cmd = commands.get(cmdName); if (cmd != null) { diff --git a/src/main/java/com/mycmd/commands/PwdCommand.java b/src/main/java/com/mycmd/commands/PwdCommand.java new file mode 100644 index 0000000..f304cbb --- /dev/null +++ b/src/main/java/com/mycmd/commands/PwdCommand.java @@ -0,0 +1,35 @@ +package com.mycmd.commands; + +import com.mycmd.Command; +import com.mycmd.ShellContext; + +/** + * Prints the current working directory. + * + * This command displays the absolute path of the current working directory + * stored in the shell context. It's equivalent to the Unix/Linux 'pwd' command + * and provides the same functionality as 'cd' without arguments. + * + * Usage: + * - pwd : Print the current working directory path + * + * The command always prints the absolute path of the current directory, + * making it useful for scripts and when you need to know your exact location + * in the file system. + */ +public class PwdCommand implements Command { + @Override + public void execute(String[] args, ShellContext context) { + System.out.println(context.getCurrentDir().getAbsolutePath()); + } + + @Override + public String description() { + return "Print the current working directory path."; + } + + @Override + public String usage() { + return "pwd"; + } +} From 7fc85301e6f1bc1dfd910d0c16197f641ec9d1db Mon Sep 17 00:00:00 2001 From: yuvaraj Date: Sat, 18 Oct 2025 16:29:46 +0530 Subject: [PATCH 2/3] feat: add uptime command and moved target dir to .gitignore --- .gitignore | 1 + src/main/java/com/mycmd/App.java | 1 + src/main/java/com/mycmd/ShellContext.java | 6 +++ .../com/mycmd/commands/UptimeCommand.java | 45 +++++++++++++++++++ .../compile/default-compile/createdFiles.lst | 0 .../compile/default-compile/inputFiles.lst | 28 ------------ 6 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 .gitignore create mode 100644 src/main/java/com/mycmd/commands/UptimeCommand.java delete mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst delete mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index e04c4a6..7835263 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -78,5 +78,6 @@ private static void registerCommands(Map commands) { commands.put("date", new DateCommand()); commands.put("history", new HistoryCommand()); commands.put("pwd", new PwdCommand()); + commands.put("uptime", new UptimeCommand()); } } diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 7800152..86dd0b2 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -7,11 +7,13 @@ public class ShellContext { private File currentDir; private List commandHistory; + private final long startTime; private static final int MAX_HISTORY = 10; public ShellContext() { this.currentDir = new File(System.getProperty("user.dir")); this.commandHistory = new ArrayList<>(); + this.startTime = System.currentTimeMillis(); } public File getCurrentDir() { @@ -22,6 +24,10 @@ public void setCurrentDir(File dir) { this.currentDir = dir; } + public long getStartTime() { + return startTime; + } + public List getCommandHistory() { return commandHistory; } diff --git a/src/main/java/com/mycmd/commands/UptimeCommand.java b/src/main/java/com/mycmd/commands/UptimeCommand.java new file mode 100644 index 0000000..6cd6c40 --- /dev/null +++ b/src/main/java/com/mycmd/commands/UptimeCommand.java @@ -0,0 +1,45 @@ +package com.mycmd.commands; + +import com.mycmd.Command; +import com.mycmd.ShellContext; + +/** + * Displays how long the shell has been running since startup. + * + * This command shows the uptime of the MyCMD shell session, displaying + * the time elapsed since the shell was started. The uptime is calculated + * from the shell start time stored in the ShellContext. + * + * Usage: + * - uptime : Display shell uptime in hours, minutes, and seconds + * + * The output format shows the uptime as "Up since Xh Ym Zs" where: + * - X is hours + * - Y is minutes + * - Z is seconds + * + * This is useful for monitoring how long a shell session has been active. + */ +public class UptimeCommand implements Command { + @Override + public void execute(String[] args, ShellContext context) { + long uptimeMillis = System.currentTimeMillis() - context.getStartTime(); + + long totalSeconds = uptimeMillis / 1000; + long hours = totalSeconds / 3600; + long minutes = (totalSeconds % 3600) / 60; + long seconds = totalSeconds % 60; + + System.out.printf("Up since %dh %dm %ds%n", hours, minutes, seconds); + } + + @Override + public String description() { + return "Display how long the shell has been running since startup."; + } + + @Override + public String usage() { + return "uptime"; + } +} diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst deleted file mode 100644 index e69de29..0000000 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst deleted file mode 100644 index 1c712e6..0000000 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ /dev/null @@ -1,28 +0,0 @@ -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\App.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\Command.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\CdCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\ClsCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\ColorCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\CopyCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\DateCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\DelCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\DirCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\EchoCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\ExitCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\HelpCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\HistoryCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\HostnameCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\LsCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\MkdirCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\MoveCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\PingCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\RmdirCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\TimeCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\TitleCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\TouchCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\TreeCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\TypeCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\VersionCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\commands\WhoamiCommand.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\ShellContext.java -C:\Users\thidass\Desktop\gittest\MyCMD\src\main\java\com\mycmd\StringUtils.java From 597d97178927a846a4ca6c7374a82159e76edca1 Mon Sep 17 00:00:00 2001 From: yuvaraj Date: Sat, 18 Oct 2025 16:35:32 +0530 Subject: [PATCH 3/3] feat: add clearhistory cmd to delete the cmd log(history) --- src/main/java/com/mycmd/App.java | 1 + src/main/java/com/mycmd/ShellContext.java | 4 +++ .../mycmd/commands/ClearHistoryCommand.java | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/main/java/com/mycmd/commands/ClearHistoryCommand.java diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 7835263..b79d37b 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -79,5 +79,6 @@ private static void registerCommands(Map commands) { commands.put("history", new HistoryCommand()); commands.put("pwd", new PwdCommand()); commands.put("uptime", new UptimeCommand()); + commands.put("clearhistory", new ClearHistoryCommand()); } } diff --git a/src/main/java/com/mycmd/ShellContext.java b/src/main/java/com/mycmd/ShellContext.java index 86dd0b2..1a22c43 100644 --- a/src/main/java/com/mycmd/ShellContext.java +++ b/src/main/java/com/mycmd/ShellContext.java @@ -41,6 +41,10 @@ public void addToHistory(String command) { } } + public void clearHistory() { + commandHistory.clear(); + } + /** * Resolve the given path (absolute or relative) to a File using the current directory. * If the provided path is absolute, returns it directly; otherwise returns a File rooted at currentDir. diff --git a/src/main/java/com/mycmd/commands/ClearHistoryCommand.java b/src/main/java/com/mycmd/commands/ClearHistoryCommand.java new file mode 100644 index 0000000..4721e86 --- /dev/null +++ b/src/main/java/com/mycmd/commands/ClearHistoryCommand.java @@ -0,0 +1,36 @@ +package com.mycmd.commands; + +import com.mycmd.Command; +import com.mycmd.ShellContext; + +/** + * Clears the stored command history. + * + * This command removes all previously executed commands from the shell's + * command history. After execution, the history list will be empty and + * the history command will show no previous commands. + * + * Usage: + * - clearhistory : Clear all stored command history + * + * This is useful for privacy purposes or when you want to start fresh + * with a clean command history. The history is cleared immediately + * and cannot be recovered. + */ +public class ClearHistoryCommand implements Command { + @Override + public void execute(String[] args, ShellContext context) { + context.clearHistory(); + System.out.println("Command history cleared."); + } + + @Override + public String description() { + return "Clear the stored command history."; + } + + @Override + public String usage() { + return "clearhistory"; + } +}