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
174 lines (148 loc) · 5.22 KB
/
Copy pathShellContext.java
File metadata and controls
174 lines (148 loc) · 5.22 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.mycmd;
import java.io.*;
import java.time.Instant;
import java.util.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
@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<>();
private Scanner scanner;
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();
this.scanner = null; // Will be set by App.java
loadAliases();
}
// ==================== Scanner Management ====================
/**
* Set the shared Scanner instance for all commands to use.
* Should only be called once by App.java during initialization.
*/
public void setScanner(Scanner scanner) {
if (this.scanner != null) {
throw new IllegalStateException("Scanner already initialized");
}
if (scanner == null) {
throw new IllegalArgumentException("Scanner cannot be null");
}
this.scanner = scanner;
}
/**
* Get the shared Scanner instance.
* All commands should use this instead of creating their own Scanner.
* @return the shared Scanner instance
* @throws IllegalStateException if Scanner hasn't been initialized
*/
public Scanner getScanner() {
if (scanner == null) {
throw new IllegalStateException("Scanner not initialized in ShellContext");
}
return scanner;
}
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);
}
}
}