Added the hk command

This commit is contained in:
Justine Pelletreau 2023-11-17 18:07:03 +01:00
parent 9cba5cfde7
commit 57c13fc15b
2 changed files with 46 additions and 4 deletions

View File

@ -29,8 +29,8 @@ TODO (Bugz):
## sqishrc (Config) ## sqishrc (Config)
See the included sqishrc file included for comments, and copy it as ~/.sqishrc for use (optionnal). See the included sqishrc file included for comments, and copy it as ~/.sqishrc for use (optionnal).
## Built-in shortcuts ## Built-ins
### As Hotkeys ### Hotkeys
Some shortcuts are built in. They may be unique to Sqish or be copied from Zsh for example. Some shortcuts are built in. They may be unique to Sqish or be copied from Zsh for example.
* Ctrl+Q or Ctrl+D : Quits the shell immediatly * Ctrl+Q or Ctrl+D : Quits the shell immediatly
* Ctrl+U : Clears currently written line * Ctrl+U : Clears currently written line
@ -47,5 +47,9 @@ Some shortcuts are built in. They may be unique to Sqish or be copied from Zsh f
* !12 : Returns command number 12 from history (Replace the number 12 with any number from history). You can add commands after it, like: "!12 | grep squirrel" * !12 : Returns command number 12 from history (Replace the number 12 with any number from history). You can add commands after it, like: "!12 | grep squirrel"
* !! : Appends the previous command to the current line. Same principle as !12. * !! : Appends the previous command to the current line. Same principle as !12.
### Built-in commands
* hk <LETTER from A to Z> <command> : For the current session, records the hotkey Alt+LETTER to run the command given. Does not run the command itself. Takes precedence over config-defined hotkeys. For example, after running :
```
hk a ls -ltr
```
...pressing Alt+A would run "ls -ltr" directly, for the current session.

View File

@ -128,7 +128,37 @@ pub mod shell {
return Ok(CmdOutput::new_empty()); return Ok(CmdOutput::new_empty());
} }
fn read_hotkeys(hotkeys: &HashMap<String, String>) -> HashMap<String, String> {
//Read the env vars
//and add hotkeys as we find them
let mut hm = hotkeys.clone();
for (key, value) in env::vars() {
if key.starts_with("SQISH_HK_") && key.chars().count() == 10 {
let letter = key.chars().last().unwrap();
&hm.insert(String::from(letter).to_lowercase(), value.clone());
}
}
return hm;
}
fn set_hotkey(args: Vec<String>) {
if args.len() < 2 { return (); }
let letter = args
.first()
.unwrap()
.to_lowercase();
let mut command = args[1..]
.join(" ");
command.push_str("\n");
let mut hm = HashMap::new();
let key = format!("SQISH_HK_{}", letter.to_uppercase());
hm.insert(key, command);
set_envvars(&hm);
}
fn process_line(input: &str) -> Result<CmdOutput, CmdOutput> { fn process_line(input: &str) -> Result<CmdOutput, CmdOutput> {
let mut commands = input.trim().split("|").peekable(); let mut commands = input.trim().split("|").peekable();
let mut previous_command = None; let mut previous_command = None;
@ -161,6 +191,10 @@ pub mod shell {
export_var(args.join(" "))?; export_var(args.join(" "))?;
print!("\r\n"); print!("\r\n");
}, },
"hk" => {
set_hotkey(args);
print!("\r\nHotkey registered.\r\n");
}
command => { command => {
if commands.peek().is_some() { if commands.peek().is_some() {
let stdin = match previous_command { let stdin = match previous_command {
@ -714,6 +748,9 @@ pub mod shell {
elems.stdout.flush(); elems.stdout.flush();
set_envvars(&elems.conf.env); set_envvars(&elems.conf.env);
//Read hotkeys set as environment variables
elems.conf.hotkeys = read_hotkeys(&elems.conf.hotkeys);
if elems.conf.init != String::from("") { if elems.conf.init != String::from("") {
run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout); run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);
} else { } else {
@ -728,6 +765,7 @@ pub mod shell {
} }
Key::Char('\n') => { Key::Char('\n') => {
enter(&mut elems); enter(&mut elems);
elems.conf.hotkeys = read_hotkeys(&elems.conf.hotkeys);
}, },
Key::Ctrl('q'|'d') => { Key::Ctrl('q'|'d') => {