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 023d390..b79d37b 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -77,5 +77,8 @@ 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()); + 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 7800152..1a22c43 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; } @@ -35,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"; + } +} 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"; + } +} 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