Fixed character insertion

This commit is contained in:
Justine
2023-02-02 11:32:01 +01:00
parent 681d96ba0f
commit 88a35d692c

View File

@ -3,7 +3,6 @@ pub mod shell {
use std::process::Command; use std::process::Command;
use std::io::stdin; use std::io::stdin;
use std::io::stdout; use std::io::stdout;
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;
@ -98,7 +97,7 @@ pub mod shell {
.stdin(stdin) .stdin(stdin)
.spawn() { .spawn() {
Ok(h) => h, Ok(h) => h,
Err(e) => { Err(_) => {
let err_string = format!("Command not found : {}\r\n", command); let err_string = format!("Command not found : {}\r\n", command);
return String::from(err_string); return String::from(err_string);
}, },
@ -205,14 +204,19 @@ pub mod shell {
return (max_pos, current_pos); return (max_pos, current_pos);
} }
fn clear_line(max_pos: &usize) { fn clear_line(max_pos: &usize, current_pos: &usize) {
//clears currently written command from the screen //clears currently written command from the screen
//...NOT from the variable ! //...NOT from the variable !
let mut stdout = stdout().into_raw_mode().unwrap(); let mut stdout = stdout().into_raw_mode().unwrap();
let right_spaces: u16 = (*max_pos - *current_pos).try_into().unwrap();
write!(stdout, "{}", cursor::Right(right_spaces));
for _i in 0..*max_pos { for _i in 0..*max_pos {
write!(stdout, "{}", cursor::Left(1)); write!(stdout, "{}", cursor::Left(1));
write!(stdout, "\x1b[K").unwrap(); write!(stdout, "\x1b[K").unwrap();
} }
write!(stdout, "{}", cursor::Left(1));
write!(stdout, "\x1b[K").unwrap();
stdout.flush();
} }
@ -296,7 +300,7 @@ pub mod shell {
}, },
Key::Ctrl('t') => { Key::Ctrl('t') => {
clear_line(&max_pos); clear_line(&max_pos, &current_pos);
max_pos = 0; max_pos = 0;
current_pos = 0; current_pos = 0;
mycommand.clear(); mycommand.clear();
@ -334,20 +338,19 @@ pub mod shell {
write!(stdout, "{}", cursor::Restore); write!(stdout, "{}", cursor::Restore);
write!(stdout, "{}", cursor::Right(1)); write!(stdout, "{}", cursor::Right(1));
max_pos += 1; max_pos += 1;
current_pos += 1;
} }
}, },
Key::Backspace => { Key::Backspace => {
if current_pos > 0 { if current_pos > 0 {
max_pos -= 1;
current_pos -= 1; current_pos -= 1;
max_pos -= 1;
mycommand.remove(current_pos); mycommand.remove(current_pos);
write!(stdout, "{}", cursor::Left(1)); write!(stdout, "{}", cursor::Left(1));
//Delete the end
write!(stdout, "\x1b[K").unwrap();
write!(stdout, "{}", cursor::Save); write!(stdout, "{}", cursor::Save);
clear_line(&max_pos); clear_line(&max_pos, &current_pos);
write!(stdout, "{}", mycommand); write!(stdout, "{}", mycommand);
write!(stdout, "{}", cursor::Restore); write!(stdout, "{}", cursor::Restore);
stdout.flush(); stdout.flush();
@ -380,11 +383,21 @@ pub mod shell {
write!(stdout, "{}", mycommand); write!(stdout, "{}", mycommand);
}, },
Key::Ctrl('p') => {
print!("{}", current_pos);
}
Key::Ctrl('x') => {
clear_line(&max_pos, &current_pos);
mycommand.clear();
current_pos = 0;
max_pos = 0;
}
Key::Left => { Key::Left => {
if current_pos > 0 { if current_pos > 0 {
current_pos -= 1; current_pos -= 1;
write!(stdout, "{}", cursor::Left(1)); write!(stdout, "{}", cursor::Left(1));
stdout.flush();
} }
}, },