|
1 | 1 | package com.mycmd; |
2 | 2 |
|
3 | 3 | import java.io.File; |
4 | | -import java.util.HashMap; |
5 | | -import java.util.Map; |
6 | | -import java.util.Scanner; |
| 4 | +import java.util.*; |
7 | 5 |
|
| 6 | +/** |
| 7 | + * Context object that holds the shell's state including current directory, |
| 8 | + * environment variables, command history, aliases, and shared Scanner. |
| 9 | + */ |
8 | 10 | public class ShellContext { |
| 11 | + private File currentDir; |
| 12 | + private final Map<String, String> environment; |
| 13 | + private final List<String> history; |
| 14 | + private final Map<String, String> aliases; |
| 15 | + private Scanner scanner; |
| 16 | + private long startTime; |
9 | 17 |
|
10 | | - private File currentDir; |
11 | | - private final Map<String, String> aliases; |
| 18 | + public ShellContext() { |
| 19 | + this.currentDir = new File(System.getProperty("user.dir")); |
| 20 | + this.environment = new HashMap<>(); |
| 21 | + this.history = new ArrayList<>(); |
| 22 | + this.aliases = new HashMap<>(); |
| 23 | + this.scanner = null; |
| 24 | + this.startTime = System.currentTimeMillis(); |
| 25 | + } |
| 26 | + |
| 27 | + // ==================== Scanner Management ==================== |
| 28 | + |
| 29 | + /** |
| 30 | + * Set the shared Scanner instance for all commands to use. |
| 31 | + * Should only be called once by App.java during initialization. |
| 32 | + */ |
| 33 | + public void setScanner(Scanner scanner) { |
| 34 | + this.scanner = scanner; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the shared Scanner instance. |
| 39 | + * All commands should use this instead of creating their own Scanner. |
| 40 | + * @return the shared Scanner instance |
| 41 | + * @throws IllegalStateException if Scanner hasn't been initialized |
| 42 | + */ |
| 43 | + public Scanner getScanner() { |
| 44 | + if (scanner == null) { |
| 45 | + throw new IllegalStateException("Scanner not initialized in ShellContext"); |
| 46 | + } |
| 47 | + return scanner; |
| 48 | + } |
| 49 | + |
| 50 | + // ==================== Directory Management ==================== |
| 51 | + |
| 52 | + public File getCurrentDir() { |
| 53 | + return currentDir; |
| 54 | + } |
| 55 | + |
| 56 | + public void setCurrentDir(File dir) { |
| 57 | + if (dir != null && dir.exists() && dir.isDirectory()) { |
| 58 | + this.currentDir = dir; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Resolve a path relative to the current directory. |
| 64 | + * If the path is absolute, return it as-is. |
| 65 | + * If relative, resolve it against the current directory. |
| 66 | + */ |
| 67 | + public File resolvePath(String path) { |
| 68 | + File file = new File(path); |
| 69 | + if (file.isAbsolute()) { |
| 70 | + return file; |
| 71 | + } |
| 72 | + return new File(currentDir, path); |
| 73 | + } |
| 74 | + |
| 75 | + // ==================== Environment Variables ==================== |
| 76 | + |
| 77 | + public String getEnvVar(String key) { |
| 78 | + return environment.get(key); |
| 79 | + } |
| 80 | + |
| 81 | + public void setEnvVar(String key, String value) { |
| 82 | + environment.put(key, value); |
| 83 | + } |
12 | 84 |
|
13 | | - private Scanner scanner; |
| 85 | + public Map<String, String> getAllEnvVars() { |
| 86 | + return new HashMap<>(environment); |
| 87 | + } |
14 | 88 |
|
15 | | - public ShellContext() { |
16 | | - this.currentDir = new File(System.getProperty("user.dir")); |
17 | | - this.aliases = new HashMap<>(); |
18 | | - } |
| 89 | + /** |
| 90 | + * Legacy method name support for compatibility. |
| 91 | + */ |
| 92 | + public Map<String, String> getEnvVars() { |
| 93 | + return getAllEnvVars(); |
| 94 | + } |
19 | 95 |
|
20 | | - public File getCurrentDir() { |
21 | | - return currentDir; |
22 | | - } |
| 96 | + // ==================== Command History ==================== |
| 97 | + |
| 98 | + public void addToHistory(String command) { |
| 99 | + history.add(command); |
| 100 | + } |
23 | 101 |
|
24 | | - public void setCurrentDir(File dir) { |
25 | | - if (dir != null && dir.exists() && dir.isDirectory()) { |
26 | | - this.currentDir = dir; |
| 102 | + public List<String> getHistory() { |
| 103 | + return new ArrayList<>(history); |
27 | 104 | } |
28 | | - } |
29 | 105 |
|
30 | | - public Map<String, String> getAliases() { |
31 | | - return aliases; |
32 | | - } |
| 106 | + /** |
| 107 | + * Legacy method name support for compatibility. |
| 108 | + */ |
| 109 | + public List<String> getCommandHistory() { |
| 110 | + return getHistory(); |
| 111 | + } |
33 | 112 |
|
34 | | - public void addAlias(String name, String command) { |
35 | | - aliases.put(name, command); |
36 | | - } |
| 113 | + public void clearHistory() { |
| 114 | + history.clear(); |
| 115 | + } |
37 | 116 |
|
38 | | - public String resolveAlias(String cmd) { |
39 | | - return aliases.getOrDefault(cmd, cmd); |
40 | | - } |
| 117 | + // ==================== Aliases ==================== |
| 118 | + |
| 119 | + /** |
| 120 | + * Get all aliases (for compatibility with existing code). |
| 121 | + */ |
| 122 | + public Map<String, String> getAliases() { |
| 123 | + return new HashMap<>(aliases); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Add an alias (for compatibility with existing code). |
| 128 | + */ |
| 129 | + public void addAlias(String name, String command) { |
| 130 | + aliases.put(name, command); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Resolve an alias (for compatibility with existing code). |
| 135 | + */ |
| 136 | + public String resolveAlias(String cmd) { |
| 137 | + return aliases.getOrDefault(cmd, cmd); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Set an alias. |
| 142 | + */ |
| 143 | + public void setAlias(String alias, String command) { |
| 144 | + aliases.put(alias, command); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get alias command. |
| 149 | + */ |
| 150 | + public String getAlias(String alias) { |
| 151 | + return aliases.get(alias); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Check if alias exists. |
| 156 | + */ |
| 157 | + public boolean hasAlias(String alias) { |
| 158 | + return aliases.containsKey(alias); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Remove an alias. |
| 163 | + */ |
| 164 | + public void removeAlias(String alias) { |
| 165 | + aliases.remove(alias); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get all aliases. |
| 170 | + */ |
| 171 | + public Map<String, String> getAllAliases() { |
| 172 | + return new HashMap<>(aliases); |
| 173 | + } |
| 174 | + |
| 175 | + // ==================== Start Time (for uptime command) ==================== |
| 176 | + |
| 177 | + public long getStartTime() { |
| 178 | + return startTime; |
| 179 | + } |
| 180 | + |
| 181 | + public void setStartTime(long startTime) { |
| 182 | + this.startTime = startTime; |
| 183 | + } |
41 | 184 | } |
0 commit comments