Improved hk, renamed the project to sqsh

This commit is contained in:
Justine Pelletreau
2023-11-19 12:45:26 +01:00
parent 57c13fc15b
commit 890197e12c
11 changed files with 729 additions and 52 deletions

View File

@ -113,7 +113,7 @@ pub mod shell {
let res = get_history();
match res {
Ok(r) => {
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let filepath = dirs::home_dir().unwrap().join(".history.sqsh");
let file = File::open(filepath).unwrap();
*previous_command = Some(Stdio::from(file));
//if !commands.peek().is_some() {
@ -141,12 +141,18 @@ pub mod shell {
return hm;
}
fn set_hotkey(args: Vec<String>) {
if args.len() < 2 { return (); }
fn set_hotkey(args: Vec<String>) -> Result<String, String> {
if args.len() < 2 { return Err(String::from("Not enough arguments given for hotkey creation.")); }
let letter = args
.first()
.unwrap()
.to_lowercase();
let letter = letter
.chars()
.nth(0)
.ok_or('a')
.unwrap();
if !letter.is_alphabetic() { return Err(format!("Sign given is not alphabetic")); }
let mut command = args[1..]
.join(" ");
@ -156,6 +162,8 @@ pub mod shell {
let key = format!("SQISH_HK_{}", letter.to_uppercase());
hm.insert(key, command);
set_envvars(&hm);
return Ok(format!("Hotkey set for Alt+{letter}"));
}
@ -192,8 +200,13 @@ pub mod shell {
print!("\r\n");
},
"hk" => {
set_hotkey(args);
print!("\r\nHotkey registered.\r\n");
match set_hotkey(args) {
Ok(_) => { print!("\r\nHotkey registered.\r\n"); },
Err(e) => {
let line = format!("\r\n{e}\r\n");
print!("{}", &line);
},
};
}
command => {
if commands.peek().is_some() {
@ -397,7 +410,7 @@ pub mod shell {
*current_number += 1;
//EXITTING
} else if mycommand == &String::from("exit") {
write!(stdout, "\r\nThanks for using Sqish!\r\nSayonara \r\n");
write!(stdout, "\r\nSayonara\r\n");
RawTerminal::suspend_raw_mode(&stdout);
std::process::exit(0);
//EMPTY LINE
@ -461,7 +474,7 @@ pub mod shell {
fn handle_conf() -> SqishConf {
let conf = match SqishConf::from_sqishrc() {
let conf = match SqishConf::from_sqshrc() {
Ok(c) => c,
Err(_) => {
let conf = SqishConf {
@ -769,7 +782,7 @@ pub mod shell {
},
Key::Ctrl('q'|'d') => {
writeln!(elems.stdout, "\r\nThanks for using Sqish !\r\nSayonara \r\n");
writeln!(elems.stdout, "\r\nSayonara\r\n");
break;
},

View File

@ -1,4 +1,4 @@
pub use ::sqish::shell::run_raw;
pub use ::sqsh::shell::run_raw;
fn main() {

View File

@ -69,7 +69,7 @@ fn print_hist(previous_command: &mut Option<Stdio>, command_next: bool) -> Resul
let res = get_history();
match res {
Ok(r) => {
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let filepath = dirs::home_dir().unwrap().join(".history.sqsh");
let file = File::open(filepath).unwrap();
*previous_command = Some(Stdio::from(file));
//if !commands.peek().is_some() {

View File

@ -20,42 +20,42 @@ pub struct SqishConf {
}
impl SqishConf {
pub fn from_sqishrc() -> Result<SqishConf, String> {
pub fn from_sqshrc() -> Result<SqishConf, String> {
let mut conf_path = match dirs::home_dir() {
Some(p) => p,
None => {
return Err(String::from("Home dir could not be determined."));
},
};
conf_path.push(".sqishrc");
conf_path.push(".sqshrc");
let sqishrc_file = match fs::read_to_string(conf_path) {
let sqshrc_file = match fs::read_to_string(conf_path) {
Ok(s) => s,
Err(_) => {
return Err(String::from("Could not read ~/.sqishrc"));
return Err(String::from("Could not read ~/.sqshrc"));
},
};
let sqishrc = match YamlLoader::load_from_str(&sqishrc_file) {
let sqshrc = match YamlLoader::load_from_str(&sqshrc_file) {
Ok(s) => s,
Err(_) => {
return Err(String::from("sqishrc is not valid yaml"));
return Err(String::from("sqshrc is not valid yaml"));
},
};
let sqishrc = &sqishrc[0];
let out_str = String::from(sqishrc["prompt"].as_str().unwrap_or("$ "));
let startup = String::from(sqishrc["init"].as_str().unwrap_or(""));
let sqshrc = &sqshrc[0];
let out_str = String::from(sqshrc["prompt"].as_str().unwrap_or("$ "));
let startup = String::from(sqshrc["init"].as_str().unwrap_or(""));
//Loading hotkeys and aliases from yaml
//They can be empty, be careful...
let aliases_yaml = &sqishrc["aliases"];
let aliases_yaml = &sqshrc["aliases"];
let aliases = Self::yaml_dict2hashmap(aliases_yaml);
let hotkeys_yaml = &sqishrc["hotkeys"];
let hotkeys_yaml = &sqshrc["hotkeys"];
let hotkeys = Self::yaml_dict2hashmap(hotkeys_yaml);
let env_yaml = &sqishrc["env"];
let env_yaml = &sqshrc["env"];
let env = Self::yaml_dict2hashmap(env_yaml);
let mut out_conf = SqishConf {

View File

@ -7,7 +7,7 @@ use std::io::{prelude::*, BufReader};
pub fn write_to_history(command: &str) -> Result<(), std::io::Error> {
//Write a command to history
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let filepath = dirs::home_dir().unwrap().join(".history.sqsh");
let mut file = OpenOptions::new()
.write(true)
.append(true)
@ -28,7 +28,7 @@ pub fn write_to_history(command: &str) -> Result<(), std::io::Error> {
pub fn get_history_number() -> Result<i32, std::io::Error> {
//Get the latest number found in history file
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let filepath = dirs::home_dir().unwrap().join(".history.sqsh");
let file = File::open(filepath)?;
let reader = BufReader::new(file).lines();
let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?);
@ -47,7 +47,7 @@ pub fn get_curr_history_number() -> i32 {
}
pub fn get_history() -> Result<String, std::io::Error> {
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let filepath = dirs::home_dir().unwrap().join(".history.sqsh");
let file = File::open(filepath)?;
let contents = BufReader::new(file).lines();
let mut res = String::new();
@ -61,7 +61,7 @@ pub fn get_history() -> Result<String, std::io::Error> {
pub fn get_hist_from_number(number: &i32) -> Option<String> {
//Returns a command from its number in hist file
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let filepath = dirs::home_dir().unwrap().join(".history.sqsh");
if filepath.exists() == false {
return None;