Hotkeys now take \n as a press on enter

This commit is contained in:
Justine 2023-02-19 15:47:42 +01:00
parent 31b5fea2af
commit 3f4d13769e
2 changed files with 57 additions and 32 deletions

View File

@ -3,7 +3,7 @@ Rust Shell. This is an attempt to create a simple shell in Rust, because why not
TO DO:
* Add prompt RGB support
* Add hotkeys in sqishrc (albeit in the same manner as per colors, the conf Struct already has an hashmap field for it)
* Implement Alt+z cancels last hotkey, and also "$ENTER_" is like pressing enter
* Add aliases in sqishrc (same old same old)
* rc file shoudl probably just be called .sqishrc, and history .sqishrc.hist
* Add an Ascii header when starting with a waifu or smth I don't know I'm tired

View File

@ -1,5 +1,6 @@
#[allow(unused_must_use)]
pub mod shell {
use std::io::Stdout;
use std::process::Command;
use std::io::stdin;
use std::io::stdout;
@ -222,6 +223,40 @@ pub mod shell {
stdout.flush();
}
fn run_cmd(mycommand: &mut String,
current_number: &mut i32,
current_pos: &mut usize,
max_pos: &mut usize,
conf: &mut SqishConf,
stdout: &mut RawTerminal<Stdout>) {
*current_pos = 0;
*max_pos = 0;
if (mycommand != &String::from("\n")) && (mycommand != &String::from("exit")) {
let comm = replace_signs(&mycommand);
RawTerminal::suspend_raw_mode(&stdout);
let _res = handle_input(&comm);
RawTerminal::activate_raw_mode(&stdout);
mycommand.clear();
//Proper printing of return code
//for line in res.split('\n') {
// if line != "\r" {
// write!(stdout, "\r\n{}", line);
// }
//}
conf.update_prompt(get_curr_history_number());
write!(stdout, "\r\n{}", conf.promptline).unwrap();
stdout.flush();
*current_number += 1;
} else if mycommand == &String::from("exit") {
write!(stdout, "\r\n Sayonara \r\n");
std::process::exit(0);
} else {
conf.update_prompt(get_curr_history_number());
write!(stdout, "\r\n{}", conf.promptline).unwrap();
stdout.flush();
}
}
fn write_letter(mycommand: &mut String, current_pos: &mut usize, max_pos: &mut usize, c: char) {
let mut stdout = stdout().into_raw_mode().unwrap();
if *current_pos == *max_pos {
@ -302,32 +337,12 @@ pub mod shell {
}
Key::Char('\n') => {
current_pos = 0;
max_pos = 0;
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
let comm = replace_signs(&mycommand);
RawTerminal::suspend_raw_mode(&stdout);
let _res = handle_input(&comm);
RawTerminal::activate_raw_mode(&stdout);
mycommand.clear();
//Proper printing of return code
//for line in res.split('\n') {
// if line != "\r" {
// write!(stdout, "\r\n{}", line);
// }
//}
&conf.update_prompt(get_curr_history_number());
write!(stdout, "\r\n{}", conf.promptline).unwrap();
stdout.flush();
current_number += 1;
} else if mycommand == String::from("exit") {
write!(stdout, "\r\n Sayonara \r\n");
break;
} else {
&conf.update_prompt(get_curr_history_number());
write!(stdout, "\r\n{}", conf.promptline).unwrap();
stdout.flush();
}
run_cmd(&mut mycommand,
&mut current_number,
&mut current_pos,
&mut max_pos,
&mut conf,
&mut stdout);
},
Key::Ctrl('q') => {
@ -468,12 +483,22 @@ pub mod shell {
'a'..='y' => {
let x = String::from(x);
if conf.hotkeys.contains_key(&x) {
let hotcmd = &conf.hotkeys[&x];
let hotcmd = &conf.hotkeys[&x].clone();
for c in hotcmd.chars() {
write_letter(&mut mycommand,
&mut current_pos,
&mut max_pos,
c);
if c != '\n' {
write_letter(&mut mycommand,
&mut current_pos,
&mut max_pos,
c);
} else {
run_cmd(&mut mycommand,
&mut current_number,
&mut current_pos,
&mut max_pos,
&mut conf,
&mut stdout);
}
}
}
},