Fixed character insertion
This commit is contained in:
31
src/lib.rs
31
src/lib.rs
@ -3,7 +3,6 @@ pub mod shell {
|
||||
use std::process::Command;
|
||||
use std::io::stdin;
|
||||
use std::io::stdout;
|
||||
use std::io::Stdout;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::env;
|
||||
@ -98,7 +97,7 @@ pub mod shell {
|
||||
.stdin(stdin)
|
||||
.spawn() {
|
||||
Ok(h) => h,
|
||||
Err(e) => {
|
||||
Err(_) => {
|
||||
let err_string = format!("Command not found : {}\r\n", command);
|
||||
return String::from(err_string);
|
||||
},
|
||||
@ -205,14 +204,19 @@ pub mod shell {
|
||||
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
|
||||
//...NOT from the variable !
|
||||
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 {
|
||||
write!(stdout, "{}", cursor::Left(1));
|
||||
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') => {
|
||||
clear_line(&max_pos);
|
||||
clear_line(&max_pos, ¤t_pos);
|
||||
max_pos = 0;
|
||||
current_pos = 0;
|
||||
mycommand.clear();
|
||||
@ -334,20 +338,19 @@ pub mod shell {
|
||||
write!(stdout, "{}", cursor::Restore);
|
||||
write!(stdout, "{}", cursor::Right(1));
|
||||
max_pos += 1;
|
||||
current_pos += 1;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Key::Backspace => {
|
||||
if current_pos > 0 {
|
||||
max_pos -= 1;
|
||||
current_pos -= 1;
|
||||
max_pos -= 1;
|
||||
mycommand.remove(current_pos);
|
||||
write!(stdout, "{}", cursor::Left(1));
|
||||
//Delete the end
|
||||
write!(stdout, "\x1b[K").unwrap();
|
||||
write!(stdout, "{}", cursor::Save);
|
||||
clear_line(&max_pos);
|
||||
clear_line(&max_pos, ¤t_pos);
|
||||
write!(stdout, "{}", mycommand);
|
||||
write!(stdout, "{}", cursor::Restore);
|
||||
stdout.flush();
|
||||
@ -380,11 +383,21 @@ pub mod shell {
|
||||
write!(stdout, "{}", mycommand);
|
||||
},
|
||||
|
||||
Key::Ctrl('p') => {
|
||||
print!("{}", current_pos);
|
||||
}
|
||||
|
||||
Key::Ctrl('x') => {
|
||||
clear_line(&max_pos, ¤t_pos);
|
||||
mycommand.clear();
|
||||
current_pos = 0;
|
||||
max_pos = 0;
|
||||
}
|
||||
|
||||
Key::Left => {
|
||||
if current_pos > 0 {
|
||||
current_pos -= 1;
|
||||
write!(stdout, "{}", cursor::Left(1));
|
||||
stdout.flush();
|
||||
}
|
||||
},
|
||||
|
||||
|
Reference in New Issue
Block a user