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 .* ;
5+ import java .time . Instant ;
66
77public class ShellContext {
88 private File currentDir ;
9- private List <String > commandHistory ;
10- private final long startTime ;
11- private static final int MAX_HISTORY = 10 ;
9+ private List <String > history ;
10+ private Map <String , String > aliases ;
11+ private static final String ALIAS_FILE = ".mycmd_aliases" ;
12+ private static final int MAX_HISTORY = 100 ;
13+ private final List <String > commandHistory ;
14+ private final Instant startTime ;
1215
1316 public ShellContext () {
1417 this .currentDir = new File (System .getProperty ("user.dir" ));
18+ this .history = new ArrayList <>();
19+ this .aliases = new HashMap <>();
1520 this .commandHistory = new ArrayList <>();
16- this .startTime = System .currentTimeMillis ();
21+ this .startTime = Instant .now ();
22+ loadAliases ();
1723 }
1824
1925 public File getCurrentDir () {
@@ -24,25 +30,89 @@ public void setCurrentDir(File dir) {
2430 this .currentDir = dir ;
2531 }
2632
27- public long getStartTime () {
28- return startTime ;
33+ public void addToHistory (String command ) {
34+ history .add (command );
35+ commandHistory .add (command ); // Add to command history
36+ if (history .size () > MAX_HISTORY ) {
37+ history .remove (0 );
38+ }
39+ }
40+
41+ public List <String > getHistory () {
42+ return new ArrayList <>(history );
2943 }
3044
3145 public List <String > getCommandHistory () {
3246 return commandHistory ;
3347 }
3448
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 );
49+ public Instant getStartTime () {
50+ return startTime ;
51+ }
52+
53+ public void clearHistory () {
54+ history .clear ();
55+ }
56+
57+ // Alias management methods
58+ public void addAlias (String name , String command ) {
59+ aliases .put (name , command );
60+ saveAliases ();
61+ }
62+
63+ public void removeAlias (String name ) {
64+ aliases .remove (name );
65+ saveAliases ();
66+ }
67+
68+ public String getAlias (String name ) {
69+ return aliases .get (name );
70+ }
71+
72+ public Map <String , String > getAliases () {
73+ return new HashMap <>(aliases );
74+ }
75+
76+ public boolean hasAlias (String name ) {
77+ return aliases .containsKey (name );
78+ }
79+
80+ private void loadAliases () {
81+ File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
82+ if (!aliasFile .exists ()) {
83+ return ;
84+ }
85+
86+ try (BufferedReader reader = new BufferedReader (new FileReader (aliasFile ))) {
87+ String line ;
88+ while ((line = reader .readLine ()) != null ) {
89+ line = line .trim ();
90+ if (line .isEmpty () || line .startsWith ("#" )) {
91+ continue ;
92+ }
93+ String [] parts = line .split ("=" , 2 );
94+ if (parts .length == 2 ) {
95+ String name = parts [0 ].trim ();
96+ String command = parts [1 ].trim ();
97+ aliases .put (name , command );
98+ }
4099 }
100+ } catch (IOException e ) {
101+ System .err .println ("Warning: Could not load aliases: " + e .getMessage ());
41102 }
42103 }
43104
44- public void clearHistory () {
45- commandHistory .clear ();
105+ private void saveAliases () {
106+ File aliasFile = new File (System .getProperty ("user.home" ), ALIAS_FILE );
107+ try (BufferedWriter writer = new BufferedWriter (new FileWriter (aliasFile ))) {
108+ writer .write ("# MyCMD Aliases Configuration\n " );
109+ writer .write ("# Format: aliasName=command\n \n " );
110+ for (Map .Entry <String , String > entry : aliases .entrySet ()) {
111+ writer .write (entry .getKey () + "=" + entry .getValue () + "\n " );
112+ }
113+ } catch (IOException e ) {
114+ System .err .println ("Warning: Could not save aliases: " + e .getMessage ());
115+ }
46116 }
47117
48118 /**
0 commit comments