@@ -115,6 +115,7 @@ export class WrappedTerminal extends Terminal {
115115 _is_handling_key_events = false ;
116116
117117 _vars : Map < string , string > = new Map ( ) ;
118+ _aliases : Map < string , string > = new Map ( ) ;
118119
119120 // TODO: this exporting is a bit lazy, but it works for now
120121
@@ -163,8 +164,24 @@ export class WrappedTerminal extends Terminal {
163164 this . _vars . set ( name , value ) ;
164165 }
165166
166- unset_variable ( name : string ) : void {
167- this . _vars . delete ( name ) ;
167+ unset_variable ( name : string ) : boolean {
168+ return this . _vars . delete ( name ) ;
169+ }
170+
171+ list_aliases ( ) : Map < string , string > {
172+ return this . _aliases ;
173+ }
174+
175+ get_alias ( name : string ) : string | undefined {
176+ return this . _aliases . get ( name ) ;
177+ }
178+
179+ set_alias ( name : string , value : string ) : void {
180+ this . _aliases . set ( name , value ) ;
181+ }
182+
183+ unset_alias ( name : string ) : boolean {
184+ return this . _aliases . delete ( name ) ;
168185 }
169186
170187
@@ -248,6 +265,39 @@ export class WrappedTerminal extends Terminal {
248265 // remove leading and trailing whitespace and split by spaces, unless contained in single or double quotes
249266 // TODO: use a proper stack based parser for readability and maintainability
250267 const sub = line . trim ( ) . split ( / + (? = (?: (?: [ ^ " ' ] * [ " ' ] [ ^ " ' ] * [ " ' ] ) * [ ^ " ' ] * $ ) ) / ) ;
268+ const raw_parts = sub . slice ( ) ;
269+
270+ // handle aliases
271+ // for each part, check if it's an alias, and if so, replace it with the value
272+ // if the value ends with a space, check the next part as well
273+ for ( let i = 0 ; i < sub . length ; i ++ ) {
274+ const part = sub [ i ] ;
275+ const alias_value = this . get_alias ( part ) ;
276+
277+ if ( ! alias_value ) {
278+ // not an alias, abort (alias only applies to the first word unless chaining)
279+ break ;
280+ }
281+
282+ // split the alias value into parts
283+ const alias_parts = alias_value . split ( / + (? = (?: (?: [ ^ " ' ] * [ " ' ] [ ^ " ' ] * [ " ' ] ) * [ ^ " ' ] * $ ) ) / ) ;
284+
285+ // if ends with a space, remove the trailing empty part
286+ if ( alias_value . endsWith ( " " ) ) {
287+ alias_parts . pop ( ) ;
288+ }
289+
290+ // remove the current part and insert the alias parts
291+ sub . splice ( i , 1 , ...alias_parts ) ;
292+
293+ // adjust the index to account for the new parts
294+ i += alias_parts . length - 1 ;
295+
296+ // if the alias value ends with a space, check the next part as well
297+ if ( ! alias_value . endsWith ( " " ) ) {
298+ break ;
299+ }
300+ }
251301
252302 const skip_variable_sub_idxs = [ ] ;
253303
@@ -271,7 +321,6 @@ export class WrappedTerminal extends Terminal {
271321 }
272322 }
273323
274-
275324 // the first word is the command, the rest are arguments
276325 const command = sub [ 0 ] ;
277326
@@ -344,6 +393,7 @@ export class WrappedTerminal extends Terminal {
344393 term : this ,
345394 args,
346395 unsubbed_args,
396+ raw_parts,
347397 }
348398
349399 let old_title = "" ;
0 commit comments