Added the hk command
This commit is contained in:
38
src/lib.rs
38
src/lib.rs
@ -128,7 +128,37 @@ pub mod shell {
|
||||
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> {
|
||||
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') => {
|
||||
|
||||
Reference in New Issue
Block a user