11package com .mycmd ;
22
3- import java .io .File ;
4- import java .util .ArrayList ;
5- import java .util .List ;
3+ import java .io .*;
4+ import java .util .*;
65
76public class ShellContext {
87 private File currentDir ;
9- private List <String > commandHistory ;
10- private final long startTime ;
11- private static final int MAX_HISTORY = 10 ;
8+ private List <String > history ;
9+ private Map <String , String > aliases ;
10+ private static final String ALIAS_FILE = ".mycmd_aliases" ;
11+ private static final int MAX_HISTORY = 100 ;
1212
1313 public ShellContext () {
1414 this .currentDir = new File (System .getProperty ("user.dir" ));
15- this .commandHistory = new ArrayList <>();
16- this .startTime = System .currentTimeMillis ();
15+ this .history = new ArrayList <>();
16+ this .aliases = new HashMap <>();
17+ loadAliases ();
1718 }
1819
1920 public File getCurrentDir () {
@@ -24,25 +25,80 @@ public void setCurrentDir(File dir) {
2425 this .currentDir = dir ;
2526 }
2627
27- public long getStartTime () {
28- return startTime ;
28+ public void addToHistory (String command ) {
29+ history .add (command );
30+ if (history .size () > MAX_HISTORY ) {
31+ history .remove (0 );
32+ }
2933 }
3034
31- public List <String > getCommandHistory () {
32- return commandHistory ;
35+ public List <String > getHistory () {
36+ return new ArrayList <>( history ) ;
3337 }
3438
35- public void addToHistory (String command ) {
36- if (command != null && !command .trim ().isEmpty ()) {
37- commandHistory .add (command .trim ());
38- if (commandHistory .size () > MAX_HISTORY ) {
39- commandHistory .remove (0 );
39+ public void clearHistory () {
40+ history .clear ();
41+ }
42+
43+ // Alias management methods
44+ public void addAlias (String name , String command ) {
45+ aliases .put (name , command );
46+ saveAliases ();
47+ }
48+
49+ public void removeAlias (String name ) {
50+ aliases .remove (name );
51+ saveAliases ();
52+ }
53+
54+ public String getAlias (String name ) {
55+ return aliases .get (name );
56+ }
57+
58+ public Map <String , String > getAliases () {
59+ return new HashMap <>(aliases );
60+ }
61+
62+ public boolean hasAlias (String name ) {
63+ return aliases .containsKey (name );
64+ }
65+
66+ private void loadAliases () {
67+ File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
68+ if (!aliasFile .exists ()) {
69+ return ;
70+ }
71+
72+ try (BufferedReader reader = new BufferedReader (new FileReader (aliasFile ))) {
73+ String line ;
74+ while ((line = reader .readLine ()) != null ) {
75+ line = line .trim ();
76+ if (line .isEmpty () || line .startsWith ("#" )) {
77+ continue ;
78+ }
79+ String [] parts = line .split ("=" , 2 );
80+ if (parts .length == 2 ) {
81+ String name = parts [0 ].trim ();
82+ String command = parts [1 ].trim ();
83+ aliases .put (name , command );
84+ }
4085 }
86+ } catch (IOException e ) {
87+ System .err .println ("Warning: Could not load aliases: " + e .getMessage ());
4188 }
4289 }
4390
44- public void clearHistory () {
45- commandHistory .clear ();
91+ private void saveAliases () {
92+ File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
93+ try (BufferedWriter writer = new BufferedWriter (new FileWriter (aliasFile ))) {
94+ writer .write ("# MyCMD Aliases Configuration\n " );
95+ writer .write ("# Format: aliasName=command\n \n " );
96+ for (Map .Entry <String , String > entry : aliases .entrySet ()) {
97+ writer .write (entry .getKey () + "=" + entry .getValue () + "\n " );
98+ }
99+ } catch (IOException e ) {
100+ System .err .println ("Warning: Could not save aliases: " + e .getMessage ());
101+ }
46102 }
47103
48104 /**
0 commit comments