Hotkeys now take \n as a press on enter
This commit is contained in:
parent
31b5fea2af
commit
3f4d13769e
@ -3,7 +3,7 @@ Rust Shell. This is an attempt to create a simple shell in Rust, because why not
|
|||||||
|
|
||||||
TO DO:
|
TO DO:
|
||||||
* Add prompt RGB support
|
* 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)
|
* Add aliases in sqishrc (same old same old)
|
||||||
* rc file shoudl probably just be called .sqishrc, and history .sqishrc.hist
|
* 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
|
* Add an Ascii header when starting with a waifu or smth I don't know I'm tired
|
||||||
|
87
src/lib.rs
87
src/lib.rs
@ -1,5 +1,6 @@
|
|||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
pub mod shell {
|
pub mod shell {
|
||||||
|
use std::io::Stdout;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::io::stdin;
|
use std::io::stdin;
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
@ -222,6 +223,40 @@ pub mod shell {
|
|||||||
stdout.flush();
|
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) {
|
fn write_letter(mycommand: &mut String, current_pos: &mut usize, max_pos: &mut usize, c: char) {
|
||||||
let mut stdout = stdout().into_raw_mode().unwrap();
|
let mut stdout = stdout().into_raw_mode().unwrap();
|
||||||
if *current_pos == *max_pos {
|
if *current_pos == *max_pos {
|
||||||
@ -302,32 +337,12 @@ pub mod shell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Key::Char('\n') => {
|
Key::Char('\n') => {
|
||||||
current_pos = 0;
|
run_cmd(&mut mycommand,
|
||||||
max_pos = 0;
|
&mut current_number,
|
||||||
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
|
&mut current_pos,
|
||||||
let comm = replace_signs(&mycommand);
|
&mut max_pos,
|
||||||
RawTerminal::suspend_raw_mode(&stdout);
|
&mut conf,
|
||||||
let _res = handle_input(&comm);
|
&mut stdout);
|
||||||
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();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
Key::Ctrl('q') => {
|
Key::Ctrl('q') => {
|
||||||
@ -468,12 +483,22 @@ pub mod shell {
|
|||||||
'a'..='y' => {
|
'a'..='y' => {
|
||||||
let x = String::from(x);
|
let x = String::from(x);
|
||||||
if conf.hotkeys.contains_key(&x) {
|
if conf.hotkeys.contains_key(&x) {
|
||||||
let hotcmd = &conf.hotkeys[&x];
|
let hotcmd = &conf.hotkeys[&x].clone();
|
||||||
for c in hotcmd.chars() {
|
for c in hotcmd.chars() {
|
||||||
write_letter(&mut mycommand,
|
if c != '\n' {
|
||||||
&mut current_pos,
|
write_letter(&mut mycommand,
|
||||||
&mut max_pos,
|
&mut current_pos,
|
||||||
c);
|
&mut max_pos,
|
||||||
|
c);
|
||||||
|
} else {
|
||||||
|
run_cmd(&mut mycommand,
|
||||||
|
&mut current_number,
|
||||||
|
&mut current_pos,
|
||||||
|
&mut max_pos,
|
||||||
|
&mut conf,
|
||||||
|
&mut stdout);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user