Added hist number to prompt, Arrows working

This commit is contained in:
Justine
2022-12-23 17:35:20 +01:00
parent 8c92b82656
commit c65a6aa4d0
4 changed files with 53 additions and 119 deletions

View File

@ -104,7 +104,7 @@ pub mod shell {
return resultat;
}
fn build_prompt() -> String {
fn build_prompt(curr_number: &i32) -> String {
let user = String::from(
get_user_by_uid(get_current_uid())
.unwrap()
@ -132,7 +132,9 @@ pub mod shell {
.unwrap()
);
let prompt = String::from(format!("{}[{}@{} in {}]{} ",
let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ",
color::Fg(color::LightBlack),
curr_number,
color::Fg(color::LightCyan),
user, host, dir_last,
color::Fg(color::Reset)));
@ -175,19 +177,21 @@ pub mod shell {
//Nothing
} else {
//Command was empty, new line
return String::from(" ");
return String::from("");
}
}
fn rewrite(current_cmd: String) -> String {
fn delete_current_cmd(current_cmd: String) -> String {
//Deletes currently written command
let mut stdout = stdout().into_raw_mode().unwrap();
let nbr_of_chars = current_cmd.graphemes(true).count();
while nbr_of_chars > 0 {
//write!(stdout, "{}", cursor::Save);
for c in current_cmd.graphemes(true) {
write!(stdout, "\x1b[D").unwrap();
write!(stdout, "\x1b[K").unwrap();
}
stdout.flush();
//write!(stdout, "{}", cursor::Restore);
return String::new();
}
@ -196,9 +200,13 @@ pub mod shell {
pub fn run_raw() {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
let mut prompt = build_prompt();
let mut mycommand = String::new();
//Used in conjunction with Up arrow to go back in history
//Resetted by pressing Enter
let mut current_number = get_curr_history_number();
let mut prompt = build_prompt(&current_number);
//Initialize
write!(stdout, "\r\n SquiShell (sqish)--- \r\n{}", prompt);
stdout.flush();
@ -215,6 +223,8 @@ pub mod shell {
}
Key::Char('\n') => {
current_number = get_curr_history_number();
prompt = build_prompt(&current_number);
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
let comm = replace_signs(&mycommand);
let res = handle_input(&comm);
@ -223,7 +233,8 @@ pub mod shell {
for line in res.split('\n') {
write!(stdout, "\r\n{}", line);
}
prompt = build_prompt();
current_number = get_curr_history_number();
prompt = build_prompt(&current_number);
write!(stdout, "{}", prompt).unwrap();
stdout.flush();
} else if mycommand == String::from("exit") {
@ -258,19 +269,27 @@ pub mod shell {
},
Key::Up => {
mycommand = rewrite(mycommand);
let lastnumber = match get_history_number() {
Ok(e) => e,
Err(_) => continue,
};
let lastcmd = match get_hist_from_number(&lastnumber) {
mycommand = delete_current_cmd(mycommand);
current_number -= 1;
let lastcmd = match get_hist_from_number(&current_number) {
Some(c) => c,
None => continue
};
mycommand = lastcmd.trim().to_string();
//let prompt = build_prompt();
write!(stdout, "{}", mycommand);
},
Key::Down => {
mycommand = delete_current_cmd(mycommand);
current_number += 1;
let lastcmd = match get_hist_from_number(&current_number) {
Some(c) => c,
None => continue
};
mycommand = lastcmd.trim().to_string();
//let prompt = build_prompt();
write!(stdout, "{}", mycommand);
mycommand = lastcmd;
},
_ => (),

View File

@ -34,6 +34,16 @@ pub fn get_history_number() -> Result<i32, std::io::Error> {
return Ok(number);
}
pub fn get_curr_history_number() -> i32 {
//Returns CURRENT history number, AKA the one for the command we are typing
let mynumber = match get_history_number() {
Ok(n) => n + 1,
Err(e) => 1,
};
return mynumber;
}
pub fn get_history() -> Result<String, std::io::Error> {
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::open(filepath)?;