22
33import lombok .Getter ;
44import lombok .Setter ;
5+ import lombok .NonNull ;
56import java .io .*;
67import java .util .*;
78import java .time .Instant ;
89
910@ Getter
10- @ Setter
1111public class ShellContext {
12+
13+ @ Setter
14+ @ NonNull
1215 private File currentDir ;
16+
1317 private List <String > history ;
1418 private Map <String , String > aliases ;
1519 private static final String ALIAS_FILE = ".mycmd_aliases" ;
@@ -30,12 +34,30 @@ public ShellContext() {
3034
3135 public void addToHistory (String command ) {
3236 history .add (command );
33- commandHistory .add (command ); // Add to command history
37+ commandHistory .add (command );
3438 if (history .size () > MAX_HISTORY ) {
3539 history .remove (0 );
3640 }
3741 }
3842
43+ /** * OVERRIDES Lombok's generated getter to return a DEFENSIVE COPY
44+ * to prevent external code from modifying the shell directly.
45+ */
46+ @ Override
47+ public List <String > getHistory () {
48+ return new ArrayList <>(history );
49+ }
50+
51+ @ Override
52+ public Map <String , String > getAliases () {
53+ return new HashMap <>(aliases );
54+ }
55+
56+ @ Override
57+ public Map <String , String > getEnvVars () {
58+ return new HashMap <>(envVars );
59+ }
60+
3961
4062 public void clearHistory () {
4163 history .clear ();
@@ -51,11 +73,27 @@ public void removeAlias(String name) {
5173 aliases .remove (name );
5274 saveAliases ();
5375 }
76+
77+ public String getAlias (String name ) {
78+ return aliases .get (name );
79+ }
5480
5581 public boolean hasAlias (String name ) {
5682 return aliases .containsKey (name );
5783 }
5884
85+ // Environmental variable accessors
86+ public void setEnvVar (String key , String value ) {
87+ envVars .put (key , value );
88+ }
89+
90+ public String getEnvVar (String key ) {
91+ return envVars .get (key );
92+ }
93+
94+
95+ // --- Private Persistence Methods ---
96+
5997 private void loadAliases () {
6098 File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
6199 if (!aliasFile .exists ()) {
@@ -109,5 +147,4 @@ public File resolvePath(String path) {
109147 return new File (currentDir , path );
110148 }
111149 }
112-
113- }
150+ }
0 commit comments