Added hist number to prompt, Arrows working
This commit is contained in:
51
src/lib.rs
51
src/lib.rs
@ -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(¤t_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(¤t_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(¤t_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(¤t_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(¤t_number) {
|
||||
Some(c) => c,
|
||||
None => continue
|
||||
};
|
||||
mycommand = lastcmd.trim().to_string();
|
||||
//let prompt = build_prompt();
|
||||
write!(stdout, "{}", mycommand);
|
||||
mycommand = lastcmd;
|
||||
},
|
||||
|
||||
_ => (),
|
||||
|
@ -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)?;
|
||||
|
Reference in New Issue
Block a user