Reformatted the code to use elems

This commit is contained in:
Justine 2023-03-01 17:40:03 +01:00
parent 9e1d0dfcde
commit 4c64bb8038
2 changed files with 229 additions and 171 deletions

View File

@ -15,7 +15,6 @@ Sqish is now my default shell on my home computer, and I'll keep adding features
Also keep in mind I'm a beginner in Rust, so the code is pretty dirty; I will probably come around to reformating it once I have all the features I want and ironed out all the bugs.
TODO:
* Use elems in all function, subdivide Keys actions into functions taking termelems.
* Allow redirecting >> to the end of a file ?
* Allow && in commands ?
* Improve word jumping

View File

@ -3,7 +3,7 @@ pub mod shell {
use std::io::Stdout;
use std::process::Command;
use std::io::stdin;
use std::io::{stdout, Stdin};
use std::io::{stdout};
use std::io::Write;
use std::path::Path;
use std::env;
@ -258,8 +258,6 @@ pub mod shell {
}
fn run_cmd(mycommand: &mut String,
current_number: &mut i32,
conf: &mut SqishConf,
@ -368,54 +366,10 @@ pub mod shell {
}).unwrap();
}
struct TermElements {
//Stores currently typed command
command: String,
//Current cursor position in the line
cur_pos: usize,
//Max position of cursor in the line
max_pos: usize,
//In / out
stdin: Stdin,
stdout: RawTerminal<Stdout>,
//Current history number
current_number: i32,
//Configuration
conf: SqishConf,
}
pub fn run_raw() {
let mut elems = TermElements {
command: String::new(),
cur_pos: 0,
max_pos: 0,
stdin: stdin(),
stdout: stdout().into_raw_mode().unwrap(),
current_number: get_curr_history_number(),
conf: handle_conf(&mut stdout().into_raw_mode().unwrap()),
};
//Handle Ctrl+C
set_ctrlc();
&elems.conf.update_prompt(get_curr_history_number());
//Initializing
write!(elems.stdout, "\r\n ---Sqish initializing--- \r\n{}", elems.conf.promptline);
elems.stdout.flush();
if elems.conf.init != String::from("") {
run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);
}
for c in elems.stdin.keys() {
let k = c.unwrap();
match k {
Key::Char('\t') => {
fn tab(elems: &mut TermElements) {
//Do NOT search on an empty command
if *&elems.command.len() == 0 {
continue;
return;
}
//Search
*&mut elems.command = replace_signs(&elems.command);
@ -428,59 +382,50 @@ pub mod shell {
write!(elems.stdout, "\r\n{}{}", elems.conf.promptline, res);
elems.stdout.flush();
}
Key::Char('\n') => {
fn enter(elems: &mut TermElements) {
elems.current_number = get_curr_history_number();
elems.cur_pos = 0;
elems.max_pos = 0;
elems.command = transform_alias(&elems.conf, elems.command);
*&mut elems.command = transform_alias(&elems.conf, elems.command.clone());
run_cmd(&mut elems.command,
&mut elems.current_number,
&mut elems.conf,
&mut elems.stdout);
},
}
Key::Ctrl('q'|'d') => {
RawTerminal::suspend_raw_mode(&elems.stdout);
writeln!(elems.stdout, "\r\nThanks for using Sqish !\r\nSayonara \r\n");
break;
},
Key::Ctrl('c') => {
fn cmd_clear(elems: &mut TermElements) {
elems.cur_pos = 0;
elems.max_pos = 0;
elems.current_number = get_curr_history_number();
elems.command.clear();
write!(elems.stdout, "\r\n--CANCEL--\r\n");
write!(elems.stdout, "{}", elems.conf.promptline);
},
}
Key::Ctrl('u') => {
fn undo_line(elems: &mut TermElements) {
clear_line(&elems.max_pos, &elems.cur_pos);
elems.max_pos = 0;
elems.cur_pos = 0;
elems.command.clear();
},
}
Key::Ctrl('a') => {
fn jmp_start(elems: &mut TermElements) {
if elems.cur_pos != 0 {
write!(elems.stdout, "{}", cursor::Left(elems.cur_pos as u16));
elems.cur_pos = 0;
}
},
}
Key::Ctrl('e') => {
fn jmp_end(elems: &mut TermElements) {
if elems.cur_pos != elems.max_pos {
let chars_left = elems.max_pos - elems.cur_pos;
write!(elems.stdout, "{}", cursor::Right(chars_left as u16));
elems.cur_pos = elems.max_pos;
}
},
Key::Char(c) => {
write_letter(&mut elems.command, &mut elems.cur_pos, &mut elems.max_pos, c);
}
Key::Delete => {
fn press_del(elems: &mut TermElements) {
if elems.command.chars().count() == 1
&& elems.cur_pos == 0 {
clear_line(&elems.max_pos, &elems.cur_pos);
@ -500,11 +445,11 @@ pub mod shell {
write!(elems.stdout, "{}", cursor::Restore);
elems.stdout.flush();
}
},
}
Key::Backspace => {
fn backspace(elems: &mut TermElements) {
if elems.cur_pos == 0 {
continue;
return;
}
if elems.command.chars().count() == 1 {
clear_line(&elems.max_pos, &elems.cur_pos);
@ -522,66 +467,79 @@ pub mod shell {
write!(elems.stdout, "{}", cursor::Restore);
elems.stdout.flush();
}
},
}
Key::Up => {
fn uparrow(elems: &mut TermElements) {
if elems.current_number > 2 {
elems.command = delete_current_cmd(elems.command);
elems.command = delete_current_cmd(elems.command.clone());
elems.current_number -= 1;
let lastcmd = match get_hist_from_number(&elems.current_number) {
Some(c) => c,
None => continue
None => return
};
elems.command = lastcmd.trim().to_string();
(elems.max_pos, elems.cur_pos) = get_cmd_curs_pos(&elems.command);
write!(elems.stdout, "{}", elems.command);
}
},
}
Key::Down => {
fn downarrow(elems: &mut TermElements) {
if elems.current_number < get_curr_history_number() {
elems.command = delete_current_cmd(elems.command);
elems.command = delete_current_cmd(elems.command.clone());
elems.current_number += 1;
let lastcmd = match get_hist_from_number(&elems.current_number) {
Some(c) => c,
None => continue
None => return
};
elems.command = lastcmd.trim().to_string();
(elems.max_pos, elems.cur_pos) = get_cmd_curs_pos(&elems.command);
write!(elems.stdout, "{}", elems.command);
}
},
}
Key::Left => {
fn leftarrow(elems: &mut TermElements) {
if elems.cur_pos > 0 {
elems.cur_pos -= 1;
write!(elems.stdout, "{}", cursor::Left(1));
}
},
}
Key::Right => {
fn rightarrow(elems: &mut TermElements) {
if elems.cur_pos < elems.max_pos {
print!("{}", cursor::Right(1));
elems.cur_pos += 1;
}
},
}
Key::Ctrl('l') => {
fn screen_clear(elems: &mut TermElements) {
write!(elems.stdout, "{}{}", termion::clear::All, termion::cursor::Goto(1,1));
write!(elems.stdout, "{}{}", elems.conf.promptline, elems.command);
}
},
struct TermElements {
//Stores currently typed command
command: String,
//Current cursor position in the line
cur_pos: usize,
//Max position of cursor in the line
max_pos: usize,
stdout: RawTerminal<Stdout>,
//Current history number
current_number: i32,
//Configuration
conf: SqishConf,
}
Key::Ctrl('p') => {
fn jmp_nxt_word(elems: &mut TermElements) {
let steps = find_next_space(&elems.cur_pos, &elems.max_pos, &elems.command);
//println!("steps: {:?} cur {:?} max {:?}", steps, elems.cur_pos, elems.max_pos);
if steps > 0 {
print!("{}", cursor::Right(steps as u16));
elems.cur_pos += steps;
}
},
}
Key::Ctrl('o') => {
fn jmp_prev_word(elems: &mut TermElements) {
let command_rev = elems.command.chars().rev().collect::<String>();
let curr_rev = elems.max_pos - elems.cur_pos + 1;
let steps = find_next_space(&curr_rev, &elems.max_pos, &command_rev);
@ -589,9 +547,9 @@ pub mod shell {
print!("{}", cursor::Left(steps as u16));
elems.cur_pos -= steps;
}
},
}
Key::Alt(x) => {
fn press_alt(elems: &mut TermElements, x: char) {
match x {
'a'..='z' => {
let x = String::from(x);
@ -606,7 +564,7 @@ pub mod shell {
} else {
elems.cur_pos = 0;
elems.max_pos = 0;
elems.command = transform_alias(&elems.conf, elems.command);
elems.command = transform_alias(&elems.conf, elems.command.clone());
run_cmd(&mut elems.command,
&mut elems.current_number,
&mut elems.conf,
@ -626,9 +584,110 @@ pub mod shell {
cmd = cmd.replace('"', "");
write!(elems.stdout, "{}", cmd);
},
_ => (),
};
}
//THE ENTRYPOINT FUNCTION
pub fn run_raw() {
let mut elems = TermElements {
command: String::new(),
cur_pos: 0,
max_pos: 0,
stdout: stdout().into_raw_mode().unwrap(),
current_number: get_curr_history_number(),
conf: handle_conf(&mut stdout().into_raw_mode().unwrap()),
};
let stdin = stdin();
//Handle Ctrl+C
set_ctrlc();
&elems.conf.update_prompt(get_curr_history_number());
//Initializing
write!(elems.stdout, "\r\n ---Sqish initializing--- \r\n{}", elems.conf.promptline);
elems.stdout.flush();
if elems.conf.init != String::from("") {
run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);
}
for c in stdin.keys() {
let k = c.unwrap();
match k {
Key::Char('\t') => {
tab(&mut elems);
}
Key::Char('\n') => {
enter(&mut elems);
},
Key::Ctrl('q'|'d') => {
writeln!(elems.stdout, "\r\nThanks for using Sqish !\r\nSayonara \r\n");
break;
},
Key::Ctrl('c') => {
cmd_clear(&mut elems);
},
Key::Ctrl('u') => {
undo_line(&mut elems);
},
Key::Ctrl('a') => {
jmp_start(&mut elems);
},
Key::Ctrl('e') => {
jmp_end(&mut elems);
},
Key::Char(c) => {
write_letter(&mut elems.command, &mut elems.cur_pos, &mut elems.max_pos, c);
}
Key::Delete => {
press_del(&mut elems);
},
Key::Backspace => {
backspace(&mut elems);
},
Key::Up => {
uparrow(&mut elems);
},
Key::Down => {
downarrow(&mut elems);
},
Key::Left => {
leftarrow(&mut elems);
},
Key::Right => {
rightarrow(&mut elems);
},
Key::Ctrl('l') => {
screen_clear(&mut elems);
},
Key::Ctrl('p') => {
jmp_nxt_word(&mut elems);
},
Key::Ctrl('o') => {
jmp_prev_word(&mut elems);
},
Key::Alt(x) => {
press_alt(&mut elems, x);
},
_ => (),