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. 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: TODO:
* Use elems in all function, subdivide Keys actions into functions taking termelems.
* Allow redirecting >> to the end of a file ? * Allow redirecting >> to the end of a file ?
* Allow && in commands ? * Allow && in commands ?
* Improve word jumping * Improve word jumping

View File

@ -3,7 +3,7 @@ pub mod shell {
use std::io::Stdout; use std::io::Stdout;
use std::process::Command; use std::process::Command;
use std::io::stdin; use std::io::stdin;
use std::io::{stdout, Stdin}; use std::io::{stdout};
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::env; use std::env;
@ -258,8 +258,6 @@ pub mod shell {
} }
fn run_cmd(mycommand: &mut String, fn run_cmd(mycommand: &mut String,
current_number: &mut i32, current_number: &mut i32,
conf: &mut SqishConf, conf: &mut SqishConf,
@ -368,6 +366,156 @@ pub mod shell {
}).unwrap(); }).unwrap();
} }
fn tab(elems: &mut TermElements) {
//Do NOT search on an empty command
if *&elems.command.len() == 0 {
return;
}
//Search
*&mut elems.command = replace_signs(&elems.command);
let (res, list) = Search::build(&elems.command).unwrap().autocomplete();
if list.len() > 1 { write!(elems.stdout, "\r\n{}", list); }
elems.command.clear();
elems.command.push_str(&res);
elems.max_pos = res.len();
elems.cur_pos = elems.max_pos;
write!(elems.stdout, "\r\n{}{}", elems.conf.promptline, res);
elems.stdout.flush();
}
fn enter(elems: &mut TermElements) {
elems.current_number = get_curr_history_number();
elems.cur_pos = 0;
elems.max_pos = 0;
*&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);
}
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);
}
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();
}
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;
}
}
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;
}
}
fn press_del(elems: &mut TermElements) {
if elems.command.chars().count() == 1
&& elems.cur_pos == 0 {
clear_line(&elems.max_pos, &elems.cur_pos);
elems.command.clear();
elems.max_pos = 0;
elems.cur_pos = 0;
} else if elems.cur_pos < elems.max_pos {
elems.command.remove(elems.cur_pos);
if elems.cur_pos > 0 {
elems.cur_pos -= 1;
write!(elems.stdout, "{}", cursor::Left(1));
}
elems.max_pos -= 1;
write!(elems.stdout, "{}", cursor::Save);
clear_line(&elems.max_pos, &elems.cur_pos);
write!(elems.stdout, "{}", elems.command);
write!(elems.stdout, "{}", cursor::Restore);
elems.stdout.flush();
}
}
fn backspace(elems: &mut TermElements) {
if elems.cur_pos == 0 {
return;
}
if elems.command.chars().count() == 1 {
clear_line(&elems.max_pos, &elems.cur_pos);
elems.command.clear();
elems.cur_pos = 0;
elems.max_pos = 0;
} else {
elems.cur_pos -= 1;
elems.max_pos -= 1;
elems.command.remove(elems.cur_pos);
write!(elems.stdout, "{}", cursor::Left(1));
write!(elems.stdout, "{}", cursor::Save);
clear_line(&elems.max_pos, &elems.cur_pos);
write!(elems.stdout, "{}", elems.command);
write!(elems.stdout, "{}", cursor::Restore);
elems.stdout.flush();
}
}
fn uparrow(elems: &mut TermElements) {
if elems.current_number > 2 {
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 => return
};
elems.command = lastcmd.trim().to_string();
(elems.max_pos, elems.cur_pos) = get_cmd_curs_pos(&elems.command);
write!(elems.stdout, "{}", elems.command);
}
}
fn downarrow(elems: &mut TermElements) {
if elems.current_number < get_curr_history_number() {
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 => return
};
elems.command = lastcmd.trim().to_string();
(elems.max_pos, elems.cur_pos) = get_cmd_curs_pos(&elems.command);
write!(elems.stdout, "{}", elems.command);
}
}
fn leftarrow(elems: &mut TermElements) {
if elems.cur_pos > 0 {
elems.cur_pos -= 1;
write!(elems.stdout, "{}", cursor::Left(1));
}
}
fn rightarrow(elems: &mut TermElements) {
if elems.cur_pos < elems.max_pos {
print!("{}", cursor::Right(1));
elems.cur_pos += 1;
}
}
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 { struct TermElements {
//Stores currently typed command //Stores currently typed command
command: String, command: String,
@ -375,8 +523,6 @@ pub mod shell {
cur_pos: usize, cur_pos: usize,
//Max position of cursor in the line //Max position of cursor in the line
max_pos: usize, max_pos: usize,
//In / out
stdin: Stdin,
stdout: RawTerminal<Stdout>, stdout: RawTerminal<Stdout>,
//Current history number //Current history number
current_number: i32, current_number: i32,
@ -384,18 +530,78 @@ pub mod shell {
conf: SqishConf, conf: SqishConf,
} }
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;
}
}
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);
if steps > 0 {
print!("{}", cursor::Left(steps as u16));
elems.cur_pos -= steps;
}
}
fn press_alt(elems: &mut TermElements, x: char) {
match x {
'a'..='z' => {
let x = String::from(x);
if elems.conf.hotkeys.contains_key(&x) {
let hotcmd = &elems.conf.hotkeys[&x].clone();
for c in hotcmd.chars() {
if c != '\n' {
write_letter(&mut elems.command,
&mut elems.cur_pos,
&mut elems.max_pos,
c);
} else {
elems.cur_pos = 0;
elems.max_pos = 0;
elems.command = transform_alias(&elems.conf, elems.command.clone());
run_cmd(&mut elems.command,
&mut elems.current_number,
&mut elems.conf,
&mut elems.stdout);
}
}
}
},
'.' => {
append_prev_arg(&mut elems.command, &mut elems.cur_pos, &mut elems.max_pos);
},
'!' => {
let mut cmd = format!("\r\n-- HOTKEYS --> \r\n{:?}\r\n", elems.conf.hotkeys);
cmd = format!("{}\r\n-- ALIASES -->\r\n{:?}\r\n", cmd, elems.conf.aliases);
cmd = cmd.replace(",", "\r\n");
cmd = cmd.replace('"', "");
write!(elems.stdout, "{}", cmd);
},
_ => (),
};
}
//THE ENTRYPOINT FUNCTION
pub fn run_raw() { pub fn run_raw() {
let mut elems = TermElements { let mut elems = TermElements {
command: String::new(), command: String::new(),
cur_pos: 0, cur_pos: 0,
max_pos: 0, max_pos: 0,
stdin: stdin(),
stdout: stdout().into_raw_mode().unwrap(), stdout: stdout().into_raw_mode().unwrap(),
current_number: get_curr_history_number(), current_number: get_curr_history_number(),
conf: handle_conf(&mut stdout().into_raw_mode().unwrap()), conf: handle_conf(&mut stdout().into_raw_mode().unwrap()),
}; };
let stdin = stdin();
//Handle Ctrl+C //Handle Ctrl+C
set_ctrlc(); set_ctrlc();
@ -409,71 +615,35 @@ pub mod shell {
run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout); run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);
} }
for c in elems.stdin.keys() { for c in stdin.keys() {
let k = c.unwrap(); let k = c.unwrap();
match k { match k {
Key::Char('\t') => { Key::Char('\t') => {
//Do NOT search on an empty command tab(&mut elems);
if *&elems.command.len() == 0 {
continue;
}
//Search
*&mut elems.command = replace_signs(&elems.command);
let (res, list) = Search::build(&elems.command).unwrap().autocomplete();
if list.len() > 1 { write!(elems.stdout, "\r\n{}", list); }
elems.command.clear();
elems.command.push_str(&res);
elems.max_pos = res.len();
elems.cur_pos = elems.max_pos;
write!(elems.stdout, "\r\n{}{}", elems.conf.promptline, res);
elems.stdout.flush();
} }
Key::Char('\n') => { Key::Char('\n') => {
elems.current_number = get_curr_history_number(); enter(&mut elems);
elems.cur_pos = 0;
elems.max_pos = 0;
elems.command = transform_alias(&elems.conf, elems.command);
run_cmd(&mut elems.command,
&mut elems.current_number,
&mut elems.conf,
&mut elems.stdout);
}, },
Key::Ctrl('q'|'d') => { Key::Ctrl('q'|'d') => {
RawTerminal::suspend_raw_mode(&elems.stdout);
writeln!(elems.stdout, "\r\nThanks for using Sqish !\r\nSayonara \r\n"); writeln!(elems.stdout, "\r\nThanks for using Sqish !\r\nSayonara \r\n");
break; break;
}, },
Key::Ctrl('c') => { Key::Ctrl('c') => {
elems.cur_pos = 0; cmd_clear(&mut elems);
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') => { Key::Ctrl('u') => {
clear_line(&elems.max_pos, &elems.cur_pos); undo_line(&mut elems);
elems.max_pos = 0;
elems.cur_pos = 0;
elems.command.clear();
}, },
Key::Ctrl('a') => { Key::Ctrl('a') => {
if elems.cur_pos != 0 { jmp_start(&mut elems);
write!(elems.stdout, "{}", cursor::Left(elems.cur_pos as u16));
elems.cur_pos = 0;
}
}, },
Key::Ctrl('e') => { Key::Ctrl('e') => {
if elems.cur_pos != elems.max_pos { jmp_end(&mut elems);
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) => { Key::Char(c) => {
@ -481,154 +651,43 @@ pub mod shell {
} }
Key::Delete => { Key::Delete => {
if elems.command.chars().count() == 1 press_del(&mut elems);
&& elems.cur_pos == 0 {
clear_line(&elems.max_pos, &elems.cur_pos);
elems.command.clear();
elems.max_pos = 0;
elems.cur_pos = 0;
} else if elems.cur_pos < elems.max_pos {
elems.command.remove(elems.cur_pos);
if elems.cur_pos > 0 {
elems.cur_pos -= 1;
write!(elems.stdout, "{}", cursor::Left(1));
}
elems.max_pos -= 1;
write!(elems.stdout, "{}", cursor::Save);
clear_line(&elems.max_pos, &elems.cur_pos);
write!(elems.stdout, "{}", elems.command);
write!(elems.stdout, "{}", cursor::Restore);
elems.stdout.flush();
}
}, },
Key::Backspace => { Key::Backspace => {
if elems.cur_pos == 0 { backspace(&mut elems);
continue;
}
if elems.command.chars().count() == 1 {
clear_line(&elems.max_pos, &elems.cur_pos);
elems.command.clear();
elems.cur_pos = 0;
elems.max_pos = 0;
} else {
elems.cur_pos -= 1;
elems.max_pos -= 1;
elems.command.remove(elems.cur_pos);
write!(elems.stdout, "{}", cursor::Left(1));
write!(elems.stdout, "{}", cursor::Save);
clear_line(&elems.max_pos, &elems.cur_pos);
write!(elems.stdout, "{}", elems.command);
write!(elems.stdout, "{}", cursor::Restore);
elems.stdout.flush();
}
}, },
Key::Up => { Key::Up => {
if elems.current_number > 2 { uparrow(&mut elems);
elems.command = delete_current_cmd(elems.command);
elems.current_number -= 1;
let lastcmd = match get_hist_from_number(&elems.current_number) {
Some(c) => c,
None => continue
};
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 => { Key::Down => {
if elems.current_number < get_curr_history_number() { downarrow(&mut elems);
elems.command = delete_current_cmd(elems.command);
elems.current_number += 1;
let lastcmd = match get_hist_from_number(&elems.current_number) {
Some(c) => c,
None => continue
};
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 => { Key::Left => {
if elems.cur_pos > 0 { leftarrow(&mut elems);
elems.cur_pos -= 1;
write!(elems.stdout, "{}", cursor::Left(1));
}
}, },
Key::Right => { Key::Right => {
if elems.cur_pos < elems.max_pos { rightarrow(&mut elems);
print!("{}", cursor::Right(1));
elems.cur_pos += 1;
}
}, },
Key::Ctrl('l') => { Key::Ctrl('l') => {
write!(elems.stdout, "{}{}", termion::clear::All, termion::cursor::Goto(1,1)); screen_clear(&mut elems);
write!(elems.stdout, "{}{}", elems.conf.promptline, elems.command);
}, },
Key::Ctrl('p') => { Key::Ctrl('p') => {
let steps = find_next_space(&elems.cur_pos, &elems.max_pos, &elems.command); jmp_nxt_word(&mut elems);
//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') => { Key::Ctrl('o') => {
let command_rev = elems.command.chars().rev().collect::<String>(); jmp_prev_word(&mut elems);
let curr_rev = elems.max_pos - elems.cur_pos + 1;
let steps = find_next_space(&curr_rev, &elems.max_pos, &command_rev);
if steps > 0 {
print!("{}", cursor::Left(steps as u16));
elems.cur_pos -= steps;
}
}, },
Key::Alt(x) => { Key::Alt(x) => {
match x { press_alt(&mut elems, x);
'a'..='z' => {
let x = String::from(x);
if elems.conf.hotkeys.contains_key(&x) {
let hotcmd = &elems.conf.hotkeys[&x].clone();
for c in hotcmd.chars() {
if c != '\n' {
write_letter(&mut elems.command,
&mut elems.cur_pos,
&mut elems.max_pos,
c);
} else {
elems.cur_pos = 0;
elems.max_pos = 0;
elems.command = transform_alias(&elems.conf, elems.command);
run_cmd(&mut elems.command,
&mut elems.current_number,
&mut elems.conf,
&mut elems.stdout);
}
}
}
},
'.' => {
append_prev_arg(&mut elems.command, &mut elems.cur_pos, &mut elems.max_pos);
},
'!' => {
let mut cmd = format!("\r\n-- HOTKEYS --> \r\n{:?}\r\n", elems.conf.hotkeys);
cmd = format!("{}\r\n-- ALIASES -->\r\n{:?}\r\n", cmd, elems.conf.aliases);
cmd = cmd.replace(",", "\r\n");
cmd = cmd.replace('"', "");
write!(elems.stdout, "{}", cmd);
},
_ => (),
};
}, },
_ => (), _ => (),