Skip to content

Commit 597d971

Browse files
committed
feat: add clearhistory cmd to delete the cmd log(history)
1 parent 7fc8530 commit 597d971

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ private static void registerCommands(Map<String, Command> commands) {
7979
commands.put("history", new HistoryCommand());
8080
commands.put("pwd", new PwdCommand());
8181
commands.put("uptime", new UptimeCommand());
82+
commands.put("clearhistory", new ClearHistoryCommand());
8283
}
8384
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public void addToHistory(String command) {
4141
}
4242
}
4343

44+
public void clearHistory() {
45+
commandHistory.clear();
46+
}
47+
4448
/**
4549
* Resolve the given path (absolute or relative) to a File using the current directory.
4650
* 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+
}

0 commit comments

Comments
 (0)