forked from Drive-for-Java/MyCMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellContext.java
More file actions
146 lines (121 loc) · 4.2 KB
/
Copy pathShellContext.java
File metadata and controls
146 lines (121 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.mycmd;
import lombok.Getter;
import lombok.Setter;
import lombok.NonNull;
import lombok.AccessLevel;
import java.io.*;
import java.util.*;
import java.time.Instant;
@Getter(AccessLevel.PUBLIC)
public class ShellContext {
@Setter
@NonNull
private File currentDir;
private List<String> history;
private Map<String, String> aliases;
private static final String ALIAS_FILE = ".mycmd_aliases";
private static final int MAX_HISTORY = 100;
private final List<String> commandHistory;
private final Instant startTime;
private final Map<String, String> envVars = new HashMap<>();
public ShellContext() {
this.currentDir = new File(System.getProperty("user.dir"));
this.history = new ArrayList<>();
this.aliases = new HashMap<>();
this.commandHistory = new ArrayList<>();
this.startTime = Instant.now();
loadAliases();
}
public void addToHistory(String command) {
history.add(command);
commandHistory.add(command);
if (history.size() > MAX_HISTORY) {
history.remove(0);
}
}
/** RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */
public List<String> getHistory() {
return new ArrayList<>(history);
}
public Map<String, String> getAliases() {
return new HashMap<>(aliases);
}
public Map<String, String> getEnvVars() {
return new HashMap<>(envVars);
}
public void clearHistory() {
history.clear();
}
public void addAlias(String name, String command) {
aliases.put(name, command);
saveAliases();
}
public void removeAlias(String name) {
aliases.remove(name);
saveAliases();
}
public String getAlias(String name) {
return aliases.get(name);
}
public boolean hasAlias(String name) {
return aliases.containsKey(name);
}
public void setEnvVar(String key, String value) {
envVars.put(key, value);
}
public String getEnvVar(String key) {
return envVars.get(key);
}
private void loadAliases() {
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
// ... (method body remains the same)
if (!aliasFile.exists()) {
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(aliasFile))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
String[] parts = line.split("=", 2);
if (parts.length == 2) {
String name = parts[0].trim();
String command = parts[1].trim();
aliases.put(name, command);
}
}
} catch (IOException e) {
System.err.println("Warning: Could not load aliases: " + e.getMessage());
}
}
private void saveAliases() {
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
// ... (method body remains the same)
try (BufferedWriter writer = new BufferedWriter(new FileWriter(aliasFile))) {
writer.write("# MyCMD Aliases Configuration\n");
writer.write("# Format: aliasName=command\n\n");
for (Map.Entry<String, String> entry : aliases.entrySet()) {
writer.write(entry.getKey() + "=" + entry.getValue() + "\n");
}
} catch (IOException e) {
System.err.println("Warning: Could not save aliases: " + e.getMessage());
}
}
/**
* 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.
*/
public File resolvePath(String path) {
if (path == null || path.trim().isEmpty()) {
return currentDir;
}
File f = new File(path);
if (f.isAbsolute()) {
return f;
} else {
return new File(currentDir, path);
}
}
}