Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
3 changes: 3 additions & 0 deletions src/main/java/com/mycmd/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@ private static void registerCommands(Map<String, Command> 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());
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/mycmd/ShellContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public class ShellContext {
private File currentDir;
private List<String> 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() {
Expand All @@ -22,6 +24,10 @@ public void setCurrentDir(File dir) {
this.currentDir = dir;
}

public long getStartTime() {
return startTime;
}

public List<String> getCommandHistory() {
return commandHistory;
}
Expand All @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/mycmd/commands/ClearHistoryCommand.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/mycmd/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public HelpCommand(Map<String, Command> 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) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/mycmd/commands/PwdCommand.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/mycmd/commands/UptimeCommand.java
Original file line number Diff line number Diff line change
@@ -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";
}
}

This file was deleted.