From 57c13fc15b1e515ee4ab0998ba7825bd79389dbf Mon Sep 17 00:00:00 2001 From: Justine Pelletreau Date: Fri, 17 Nov 2023 18:07:03 +0100 Subject: [PATCH] Added the hk command --- README.md | 12 ++++++++---- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7fa72f1..c9bb67c 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,8 @@ TODO (Bugz): ## sqishrc (Config) See the included sqishrc file included for comments, and copy it as ~/.sqishrc for use (optionnal). -## Built-in shortcuts -### As Hotkeys +## Built-ins +### Hotkeys 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+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" * !! : Appends the previous command to the current line. Same principle as !12. - - +### Built-in commands +* hk : 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. diff --git a/src/lib.rs b/src/lib.rs index b2ed939..fd565b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,7 +128,37 @@ pub mod shell { return Ok(CmdOutput::new_empty()); } + fn read_hotkeys(hotkeys: &HashMap) -> HashMap { + //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) { + 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 { let mut commands = input.trim().split("|").peekable(); let mut previous_command = None; @@ -161,6 +191,10 @@ pub mod shell { export_var(args.join(" "))?; print!("\r\n"); }, + "hk" => { + set_hotkey(args); + print!("\r\nHotkey registered.\r\n"); + } command => { if commands.peek().is_some() { let stdin = match previous_command { @@ -714,6 +748,9 @@ pub mod shell { elems.stdout.flush(); 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("") { run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout); } else { @@ -728,6 +765,7 @@ pub mod shell { } Key::Char('\n') => { enter(&mut elems); + elems.conf.hotkeys = read_hotkeys(&elems.conf.hotkeys); }, Key::Ctrl('q'|'d') => {