11package com .mycmd ;
22
3+ import lombok .Getter ;
4+ import lombok .Setter ;
5+ import lombok .NonNull ;
6+ import lombok .AccessLevel ;
37import java .io .*;
48import java .util .*;
59import java .time .Instant ;
610
11+ @ Getter (AccessLevel .PUBLIC )
712public class ShellContext {
13+
14+ @ Setter
15+ @ NonNull
816 private File currentDir ;
917 private List <String > history ;
1018 private Map <String , String > aliases ;
19+
1120 private static final String ALIAS_FILE = ".mycmd_aliases" ;
1221 private static final int MAX_HISTORY = 100 ;
22+
1323 private final List <String > commandHistory ;
14- private final Instant startTime ;
24+ private final Instant startTime ;
25+
26+ private final Map <String , String > envVars = new HashMap <>();
1527
1628 public ShellContext () {
1729 this .currentDir = new File (System .getProperty ("user.dir" ));
@@ -22,39 +34,34 @@ public ShellContext() {
2234 loadAliases ();
2335 }
2436
25- public File getCurrentDir () {
26- return currentDir ;
27- }
28-
29- public void setCurrentDir (File dir ) {
30- this .currentDir = dir ;
31- }
3237
3338 public void addToHistory (String command ) {
3439 history .add (command );
35- commandHistory .add (command ); // Add to command history
40+ commandHistory .add (command );
3641 if (history .size () > MAX_HISTORY ) {
3742 history .remove (0 );
3843 }
3944 }
4045
46+
47+ /** RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */
4148 public List <String > getHistory () {
4249 return new ArrayList <>(history );
4350 }
4451
45- public List <String > getCommandHistory () {
46- return commandHistory ;
52+ public Map <String , String > getAliases () {
53+ return new HashMap <>( aliases ) ;
4754 }
48-
49- public Instant getStartTime () {
50- return startTime ;
55+
56+ public Map < String , String > getEnvVars () {
57+ return new HashMap <>( envVars ) ;
5158 }
5259
60+
5361 public void clearHistory () {
5462 history .clear ();
5563 }
5664
57- // Alias management methods
5865 public void addAlias (String name , String command ) {
5966 aliases .put (name , command );
6067 saveAliases ();
@@ -64,21 +71,26 @@ public void removeAlias(String name) {
6471 aliases .remove (name );
6572 saveAliases ();
6673 }
67-
74+
6875 public String getAlias (String name ) {
6976 return aliases .get (name );
7077 }
7178
72- public Map <String , String > getAliases () {
73- return new HashMap <>(aliases );
74- }
75-
7679 public boolean hasAlias (String name ) {
7780 return aliases .containsKey (name );
7881 }
7982
83+ public void setEnvVar (String key , String value ) {
84+ envVars .put (key , value );
85+ }
86+
87+ public String getEnvVar (String key ) {
88+ return envVars .get (key );
89+ }
90+
8091 private void loadAliases () {
8192 File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
93+ // ... (method body remains the same)
8294 if (!aliasFile .exists ()) {
8395 return ;
8496 }
@@ -104,6 +116,7 @@ private void loadAliases() {
104116
105117 private void saveAliases () {
106118 File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
119+ // ... (method body remains the same)
107120 try (BufferedWriter writer = new BufferedWriter (new FileWriter (aliasFile ))) {
108121 writer .write ("# MyCMD Aliases Configuration\n " );
109122 writer .write ("# Format: aliasName=command\n \n " );
@@ -130,22 +143,4 @@ public File resolvePath(String path) {
130143 return new File (currentDir , path );
131144 }
132145 }
133-
134- // environmental variable map
135- // author: Kaveesha Fernando
136- // date: 2024-06-10
137- private final Map <String , String > envVars = new HashMap <>();
138-
139- public void setEnvVar (String key , String value ) {
140- envVars .put (key , value );
141- }
142-
143- public String getEnvVar (String key ) {
144- return envVars .get (key );
145- }
146-
147- public Map <String , String > getEnvVars () {
148- return new HashMap <>(envVars );
149- }
150-
151- }
146+ }
0 commit comments