Skip to content

Commit

Permalink
allow command aliases with arguments
Browse files Browse the repository at this point in the history
note: only aliases that do not contain spaces can be used with arguments
since either the entire command or only the first word are checked
against the aliases
  • Loading branch information
chrjabs committed Mar 12, 2024
1 parent 4865f6c commit 36309b7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/commands/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ pub fn read_and_execute(
.suffix(suffix)
.get_input(backend, context, &mut listener);

if let Some(s) = user_input {
if let Some(mut s) = user_input {
let mut trimmed = s.trim_start();
let _ = context.commandline_context_mut().history_mut().add(trimmed);

let (command, arg) = match trimmed.find(' ') {
Some(i) => (&trimmed[..i], &trimmed[i..]),
None => (trimmed, ""),
};

if let Some(alias) = context.config_ref().cmd_aliases.get(trimmed) {
trimmed = alias;
} else if let Some(alias) = context.config_ref().cmd_aliases.get(command) {
s.replace_range(..s.len() - arg.len(), alias);
trimmed = &s;
}

let command = Command::from_str(trimmed)?;
Expand Down

0 comments on commit 36309b7

Please sign in to comment.