Flèche gauche fonctionne, en mode remplacement

This commit is contained in:
Justine 2022-12-25 17:45:46 +01:00
parent 19771dbd13
commit e9d9db1f08

View File

@ -8,6 +8,7 @@ pub mod shell {
use std::env;
use std::str;
use std::process::Stdio;
use std::num::Wrapping;
use users::{get_user_by_uid, get_current_uid};
use termion::event::Key;
use termion::input::TermRead;
@ -104,7 +105,11 @@ pub mod shell {
return resultat;
}
fn build_prompt(curr_number: &i32) -> String {
fn build_prompt(curr_number: &i32) -> (String, usize) {
let mut length: usize = 9;
length += curr_number.to_string().len();
let user = String::from(
get_user_by_uid(get_current_uid())
.unwrap()
@ -113,12 +118,16 @@ pub mod shell {
.unwrap()
);
length += user.chars().count();
let host = String::from(
gethostname::gethostname()
.to_str()
.unwrap()
);
length += host.chars().count();
let current_dir = String::from(
env::current_dir()
.unwrap()
@ -132,13 +141,15 @@ pub mod shell {
.unwrap()
);
length += dir_last.chars().count();
let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ",
color::Fg(color::LightBlack),
curr_number,
color::Fg(color::LightCyan),
user, host, dir_last,
color::Fg(color::Reset)));
return prompt;
return (prompt, length);
}
fn replace_signs(line: &String) -> String {
@ -205,7 +216,7 @@ pub mod shell {
//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);
let (mut prompt, mut pr_len) = build_prompt(&current_number);
//Initialize
write!(stdout, "\r\n SquiShell (sqish)--- \r\n{}", prompt);
@ -224,7 +235,7 @@ pub mod shell {
Key::Char('\n') => {
current_number = get_curr_history_number();
prompt = build_prompt(&current_number);
(prompt, pr_len) = build_prompt(&current_number);
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
let comm = replace_signs(&mycommand);
let res = handle_input(&comm);
@ -234,7 +245,7 @@ pub mod shell {
write!(stdout, "\r\n{}", line);
}
current_number = get_curr_history_number();
prompt = build_prompt(&current_number);
(prompt, pr_len) = build_prompt(&current_number);
write!(stdout, "{}", prompt).unwrap();
stdout.flush();
} else if mycommand == String::from("exit") {
@ -294,10 +305,9 @@ pub mod shell {
Key::Left => {
//La taille du prompt est incorrecte à cause des couleurs !
let pr_length = prompt.chars().count();
let curs_pos = stdout.cursor_pos().unwrap().0;
if usize::from(curs_pos) > pr_length {
write!(stdout, "1{:?}/2{}-{}", curs_pos, pr_length, cursor::Left(10));
if usize::from(curs_pos) > pr_len {
write!(stdout, "{}", cursor::Left(1));
stdout.flush();
}
},