Skip to content

Commit 5f66472

Browse files
Merge branch 'main' into feat/Kaveesha/ipconfig
2 parents 432392b + 1e7f7c1 commit 5f66472

File tree

6 files changed

+234
-29
lines changed

6 files changed

+234
-29
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public static void main(String[] args) {
2424
String input = sc.nextLine().trim();
2525
if (input.isEmpty()) continue;
2626

27+
// Resolve aliases before processing
28+
input = resolveAliases(input, context);
29+
2730
String[] parts = input.split("\\s+");
2831
String cmd = parts[0].toLowerCase();
2932
String[] cmdArgs = Arrays.copyOfRange(parts, 1, parts.length);
@@ -55,6 +58,21 @@ public static void main(String[] args) {
5558
}
5659
}
5760

61+
private static String resolveAliases(String input, ShellContext context) {
62+
String[] parts = input.split("\\s+", 2);
63+
String cmd = parts[0];
64+
String rest = parts.length > 1 ? parts[1] : "";
65+
66+
// Check if the command is an alias
67+
if (context.hasAlias(cmd)) {
68+
String aliasCommand = context.getAlias(cmd);
69+
// Replace the alias with its command, preserving arguments
70+
return rest.isEmpty() ? aliasCommand : aliasCommand + " " + rest;
71+
}
72+
73+
return input;
74+
}
75+
5876
private static void registerCommands(Map<String, Command> commands) {
5977
commands.put("dir", new DirCommand());
6078
commands.put("cd", new CdCommand());
@@ -82,5 +100,7 @@ private static void registerCommands(Map<String, Command> commands) {
82100
commands.put("uptime", new UptimeCommand());
83101
commands.put("clearhistory", new ClearHistoryCommand());
84102
commands.put("ipconfig", new IpConfig());
103+
commands.put("alias", new AliasCommand());
104+
commands.put("unalias", new UnaliasCommand());
85105
}
86106
}

src/main/java/com/mycmd/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ default String description() {
2929
default String usage() {
3030
return "";
3131
}
32-
}
32+
}

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

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package com.mycmd;
22

3-
import java.io.File;
4-
import java.util.ArrayList;
5-
import java.util.List;
3+
import java.io.*;
4+
import java.util.*;
5+
import java.time.Instant;
66

77
public class ShellContext {
88
private File currentDir;
9-
private List<String> commandHistory;
10-
private final long startTime;
11-
private static final int MAX_HISTORY = 10;
9+
private List<String> history;
10+
private Map<String, String> aliases;
11+
private static final String ALIAS_FILE = ".mycmd_aliases";
12+
private static final int MAX_HISTORY = 100;
13+
private final List<String> commandHistory;
14+
private final Instant startTime;
1215

1316
public ShellContext() {
1417
this.currentDir = new File(System.getProperty("user.dir"));
18+
this.history = new ArrayList<>();
19+
this.aliases = new HashMap<>();
1520
this.commandHistory = new ArrayList<>();
16-
this.startTime = System.currentTimeMillis();
21+
this.startTime = Instant.now();
22+
loadAliases();
1723
}
1824

1925
public File getCurrentDir() {
@@ -24,25 +30,89 @@ public void setCurrentDir(File dir) {
2430
this.currentDir = dir;
2531
}
2632

27-
public long getStartTime() {
28-
return startTime;
33+
public void addToHistory(String command) {
34+
history.add(command);
35+
commandHistory.add(command); // Add to command history
36+
if (history.size() > MAX_HISTORY) {
37+
history.remove(0);
38+
}
39+
}
40+
41+
public List<String> getHistory() {
42+
return new ArrayList<>(history);
2943
}
3044

3145
public List<String> getCommandHistory() {
3246
return commandHistory;
3347
}
3448

35-
public void addToHistory(String command) {
36-
if (command != null && !command.trim().isEmpty()) {
37-
commandHistory.add(command.trim());
38-
if (commandHistory.size() > MAX_HISTORY) {
39-
commandHistory.remove(0);
49+
public Instant getStartTime() {
50+
return startTime;
51+
}
52+
53+
public void clearHistory() {
54+
history.clear();
55+
}
56+
57+
// Alias management methods
58+
public void addAlias(String name, String command) {
59+
aliases.put(name, command);
60+
saveAliases();
61+
}
62+
63+
public void removeAlias(String name) {
64+
aliases.remove(name);
65+
saveAliases();
66+
}
67+
68+
public String getAlias(String name) {
69+
return aliases.get(name);
70+
}
71+
72+
public Map<String, String> getAliases() {
73+
return new HashMap<>(aliases);
74+
}
75+
76+
public boolean hasAlias(String name) {
77+
return aliases.containsKey(name);
78+
}
79+
80+
private void loadAliases() {
81+
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
82+
if (!aliasFile.exists()) {
83+
return;
84+
}
85+
86+
try (BufferedReader reader = new BufferedReader(new FileReader(aliasFile))) {
87+
String line;
88+
while ((line = reader.readLine()) != null) {
89+
line = line.trim();
90+
if (line.isEmpty() || line.startsWith("#")) {
91+
continue;
92+
}
93+
String[] parts = line.split("=", 2);
94+
if (parts.length == 2) {
95+
String name = parts[0].trim();
96+
String command = parts[1].trim();
97+
aliases.put(name, command);
98+
}
4099
}
100+
} catch (IOException e) {
101+
System.err.println("Warning: Could not load aliases: " + e.getMessage());
41102
}
42103
}
43104

44-
public void clearHistory() {
45-
commandHistory.clear();
105+
private void saveAliases() {
106+
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
107+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(aliasFile))) {
108+
writer.write("# MyCMD Aliases Configuration\n");
109+
writer.write("# Format: aliasName=command\n\n");
110+
for (Map.Entry<String, String> entry : aliases.entrySet()) {
111+
writer.write(entry.getKey() + "=" + entry.getValue() + "\n");
112+
}
113+
} catch (IOException e) {
114+
System.err.println("Warning: Could not save aliases: " + e.getMessage());
115+
}
46116
}
47117

48118
/**
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
9+
public class AliasCommand implements Command {
10+
@Override
11+
public void execute(String[] args, ShellContext context) throws IOException {
12+
// no args: list aliases
13+
if (args == null || args.length == 0) {
14+
Map<String, String> aliases = context.getAliases();
15+
if (aliases.isEmpty()) {
16+
System.out.println("No aliases defined.");
17+
} else {
18+
aliases.forEach((k, v) -> System.out.println(k + "=" + v));
19+
}
20+
return;
21+
}
22+
23+
// single arg of form name=command
24+
if (args.length == 1 && args[0].contains("=")) {
25+
String[] parts = args[0].split("=", 2);
26+
String name = parts[0].trim();
27+
String cmd = parts[1].trim();
28+
if (name.isEmpty() || cmd.isEmpty()) {
29+
System.out.println("Invalid alias format. Usage: alias name=command");
30+
return;
31+
}
32+
context.addAlias(name, cmd);
33+
System.out.println("Alias added: " + name + "=" + cmd);
34+
return;
35+
}
36+
37+
// multiple args: first is name, rest form command
38+
if (args.length >= 2) {
39+
String name = args[0];
40+
StringBuilder sb = new StringBuilder();
41+
for (int i = 1; i < args.length; i++) {
42+
if (i > 1) sb.append(' ');
43+
sb.append(args[i]);
44+
}
45+
String cmd = sb.toString();
46+
if (name.trim().isEmpty() || cmd.trim().isEmpty()) {
47+
System.out.println("Invalid alias. Usage: alias name command... or alias name=command");
48+
return;
49+
}
50+
context.addAlias(name, cmd);
51+
System.out.println("Alias added: " + name + "=" + cmd);
52+
return;
53+
}
54+
55+
System.out.println("Invalid usage. Usage: alias [name=command] | alias [name command...]");
56+
}
57+
58+
@Override
59+
public String description() {
60+
return "Create or list command aliases.";
61+
}
62+
63+
@Override
64+
public String usage() {
65+
return "alias # list aliases\n" +
66+
"alias name=command # create alias\n" +
67+
"alias name command... # create alias";
68+
}
69+
}
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+
import java.io.IOException;
7+
8+
public class UnaliasCommand implements Command {
9+
@Override
10+
public void execute(String[] args, ShellContext context) throws IOException {
11+
if (args == null || args.length == 0) {
12+
System.out.println("Usage: unalias name [name2 ...]");
13+
return;
14+
}
15+
16+
for (String name : args) {
17+
if (name == null || name.trim().isEmpty()) continue;
18+
if (context.hasAlias(name)) {
19+
context.removeAlias(name);
20+
System.out.println("Removed alias: " + name);
21+
} else {
22+
System.out.println("Alias not found: " + name);
23+
}
24+
}
25+
}
26+
27+
@Override
28+
public String description() {
29+
return "Remove one or more aliases.";
30+
}
31+
32+
@Override
33+
public String usage() {
34+
return "unalias name [name2 ...]";
35+
}
36+
}

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import com.mycmd.Command;
44
import com.mycmd.ShellContext;
55

6+
import java.io.IOException;
7+
import java.time.Duration;
8+
import java.time.Instant;
9+
610
/**
711
* Displays how long the shell has been running since startup.
812
*
@@ -13,24 +17,30 @@
1317
* Usage:
1418
* - uptime : Display shell uptime in hours, minutes, and seconds
1519
*
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+
* The output format shows the uptime as "Uptime: Xh Ym Zs" or with days if applicable.
2021
*
2122
* This is useful for monitoring how long a shell session has been active.
2223
*/
2324
public class UptimeCommand implements Command {
2425
@Override
25-
public void execute(String[] args, ShellContext context) {
26-
long uptimeMillis = System.currentTimeMillis() - context.getStartTime();
26+
public void execute(String[] args, ShellContext context) throws IOException {
27+
Instant startTime = context.getStartTime();
28+
Instant now = Instant.now();
29+
30+
// Calculate duration between start time and now
31+
Duration uptime = Duration.between(startTime, now);
2732

28-
long totalSeconds = uptimeMillis / 1000;
29-
long hours = totalSeconds / 3600;
30-
long minutes = (totalSeconds % 3600) / 60;
31-
long seconds = totalSeconds % 60;
33+
long seconds = uptime.getSeconds();
34+
long days = seconds / 86400;
35+
long hours = (seconds % 86400) / 3600;
36+
long minutes = (seconds % 3600) / 60;
37+
long secs = seconds % 60;
3238

33-
System.out.printf("Up since %dh %dm %ds%n", hours, minutes, seconds);
39+
System.out.print("Uptime: ");
40+
if (days > 0) {
41+
System.out.print(days + " day" + (days != 1 ? "s" : "") + ", ");
42+
}
43+
System.out.printf("%02d:%02d:%02d%n", hours, minutes, secs);
3444
}
3545

3646
@Override
@@ -42,4 +52,4 @@ public String description() {
4252
public String usage() {
4353
return "uptime";
4454
}
45-
}
55+
}

0 commit comments

Comments
 (0)