From 6abe6d886dafcc8684109008431c7158592cf8a2 Mon Sep 17 00:00:00 2001 From: Justine Pelletreau Date: Fri, 17 Feb 2023 18:12:39 +0100 Subject: [PATCH] Now reading aliases and hotkeys --- sqishrc.yaml | 10 ++++++++++ src/shell/config.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/sqishrc.yaml b/sqishrc.yaml index 5211355..2be6d6e 100644 --- a/sqishrc.yaml +++ b/sqishrc.yaml @@ -24,3 +24,13 @@ #For example $RGB|108|84|30_ gives you an ugly shade of brown prompt: "$COLORLBLACK_ !$HISTNUMBER_$COLORCYAN_[$USER_@$HOSTNAME_]$RGB|125|0|125_$DIR_> $COLORRESET_" +#Classic aliases +aliases: + gcl: git clone + gpl: git pull + +#Hotkeys : When pressing Alt + the letter (a, b... up to y), enters the text inside the command +#Alt + Z Cancels last hotkey +hotkeys: + a: "| less" + b: "exit" diff --git a/src/shell/config.rs b/src/shell/config.rs index ada3782..7f97297 100644 --- a/src/shell/config.rs +++ b/src/shell/config.rs @@ -7,7 +7,7 @@ use regex::Regex; extern crate dirs; extern crate yaml_rust; -use yaml_rust::{YamlLoader}; +use yaml_rust::{YamlLoader, Yaml}; #[derive(Debug)] pub struct SqishConf { @@ -44,13 +44,23 @@ impl SqishConf { let sqishrc = &sqishrc[0]; let out_str = String::from(sqishrc["prompt"].as_str().unwrap()); + //Loading hotkeys and aliases from yaml + //They can be empty, be careful... + let aliases_yaml = &sqishrc["aliases"]; + let aliases = Self::yaml_dict2hashmap(aliases_yaml); + + let hotkeys_yaml = &sqishrc["hotkeys"]; + let hotkeys = Self::yaml_dict2hashmap(hotkeys_yaml); + let mut out_conf = SqishConf { promptline: out_str.clone(), promptline_base: out_str, - aliases: HashMap::new(), - hotkeys: HashMap::new(), + aliases: aliases, + hotkeys: hotkeys, }; + println!("{:?}", out_conf); + out_conf.handle_rgb(); out_conf.handle_colors(); @@ -164,5 +174,18 @@ impl SqishConf { } } + ///Takes a YAML dict and converts it into an hashmap. + fn yaml_dict2hashmap(yaml_dict: &Yaml) -> HashMap { + let mut my_hashmap = HashMap::new(); + + if let Yaml::Hash(h) = yaml_dict { + for (k, v) in h.iter() { + if let (Yaml::String(k), Yaml::String(v)) = (k, v) { + my_hashmap.insert(k.clone(), v.clone()); + } + } + } + return my_hashmap; + } }