Hotkeys babeeee
This commit is contained in:
parent
6abe6d886d
commit
506007e71b
103
src/lib.rs
103
src/lib.rs
@ -209,17 +209,39 @@ pub mod shell {
|
|||||||
//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 + 1).try_into().unwrap();
|
let chars_left = max_pos - current_pos;
|
||||||
write!(stdout, "{}", cursor::Right(right_spaces));
|
if current_pos < max_pos {
|
||||||
|
write!(stdout, "{}", cursor::Right(chars_left as u16));
|
||||||
|
}
|
||||||
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, "{}", cursor::Left(1));
|
||||||
write!(stdout, "\x1b[K").unwrap();
|
//write!(stdout, "\x1b[K").unwrap();
|
||||||
stdout.flush();
|
stdout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_letter(mycommand: &mut String, current_pos: &mut usize, max_pos: &mut usize, c: char) {
|
||||||
|
let mut stdout = stdout().into_raw_mode().unwrap();
|
||||||
|
if *current_pos == *max_pos {
|
||||||
|
mycommand.push(c);
|
||||||
|
*current_pos += 1;
|
||||||
|
*max_pos += 1;
|
||||||
|
write!(stdout, "{}", c).unwrap();
|
||||||
|
} else if *current_pos < *max_pos {
|
||||||
|
//Inserting a char inside the command...
|
||||||
|
write!(stdout, "{}", cursor::Save);
|
||||||
|
mycommand.insert(*current_pos, c);
|
||||||
|
let command_piece = &mycommand.as_str()[*current_pos..];
|
||||||
|
write!(stdout, "{}", command_piece);
|
||||||
|
stdout.flush();
|
||||||
|
write!(stdout, "{}", cursor::Restore);
|
||||||
|
write!(stdout, "{}", cursor::Right(1));
|
||||||
|
*max_pos += 1;
|
||||||
|
*current_pos += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn run_raw() {
|
pub fn run_raw() {
|
||||||
@ -260,8 +282,8 @@ pub mod shell {
|
|||||||
stdout.flush();
|
stdout.flush();
|
||||||
|
|
||||||
for c in stdin.keys() {
|
for c in stdin.keys() {
|
||||||
|
let k = c.unwrap();
|
||||||
match c.unwrap() {
|
match k {
|
||||||
Key::Char('\t') => {
|
Key::Char('\t') => {
|
||||||
//Do NOT search on an empty command
|
//Do NOT search on an empty command
|
||||||
if *&mycommand.len() == 0 {
|
if *&mycommand.len() == 0 {
|
||||||
@ -343,31 +365,18 @@ pub mod shell {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
Key::Char(c) => {
|
Key::Char(c) => {
|
||||||
//if at the end of the command...
|
write_letter(&mut mycommand, &mut current_pos, &mut max_pos, c);
|
||||||
if current_pos == max_pos {
|
}
|
||||||
mycommand.push(c);
|
|
||||||
current_pos += 1;
|
|
||||||
max_pos += 1;
|
|
||||||
write!(stdout, "{}", c).unwrap();
|
|
||||||
} else if current_pos < max_pos {
|
|
||||||
//Inserting a char inside the command...
|
|
||||||
write!(stdout, "{}", cursor::Save);
|
|
||||||
mycommand.insert(current_pos, c);
|
|
||||||
let command_piece = &mycommand[current_pos..];
|
|
||||||
write!(stdout, "{}", command_piece);
|
|
||||||
stdout.flush();
|
|
||||||
write!(stdout, "{}", cursor::Restore);
|
|
||||||
write!(stdout, "{}", cursor::Right(1));
|
|
||||||
max_pos += 1;
|
|
||||||
current_pos += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
Key::Delete => {
|
Key::Delete => {
|
||||||
if current_pos < max_pos {
|
if mycommand.chars().count() == 1
|
||||||
|
&& current_pos == 0 {
|
||||||
|
clear_line(&max_pos, ¤t_pos);
|
||||||
|
mycommand.clear();
|
||||||
|
max_pos = 0;
|
||||||
|
current_pos = 0;
|
||||||
|
} else if current_pos < max_pos {
|
||||||
mycommand.remove(current_pos);
|
mycommand.remove(current_pos);
|
||||||
if current_pos > 0 {
|
if current_pos > 0 {
|
||||||
current_pos -= 1;
|
current_pos -= 1;
|
||||||
@ -383,7 +392,15 @@ pub mod shell {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Key::Backspace => {
|
Key::Backspace => {
|
||||||
if current_pos > 0 {
|
if current_pos == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if mycommand.chars().count() == 1 {
|
||||||
|
clear_line(&max_pos, ¤t_pos);
|
||||||
|
mycommand.clear();
|
||||||
|
current_pos = 0;
|
||||||
|
max_pos = 0;
|
||||||
|
} else {
|
||||||
current_pos -= 1;
|
current_pos -= 1;
|
||||||
max_pos -= 1;
|
max_pos -= 1;
|
||||||
mycommand.remove(current_pos);
|
mycommand.remove(current_pos);
|
||||||
@ -393,7 +410,7 @@ pub mod shell {
|
|||||||
write!(stdout, "{}", mycommand);
|
write!(stdout, "{}", mycommand);
|
||||||
write!(stdout, "{}", cursor::Restore);
|
write!(stdout, "{}", cursor::Restore);
|
||||||
stdout.flush();
|
stdout.flush();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Key::Up => {
|
Key::Up => {
|
||||||
@ -443,10 +460,32 @@ pub mod shell {
|
|||||||
print!("{}", cursor::Right(1));
|
print!("{}", cursor::Right(1));
|
||||||
current_pos += 1;
|
current_pos += 1;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
|
//Pattern matching here !
|
||||||
|
Key::Alt(x) => {
|
||||||
|
match x {
|
||||||
|
'a'..='y' => {
|
||||||
|
let x = String::from(x);
|
||||||
|
if conf.hotkeys.contains_key(&x) {
|
||||||
|
let hotcmd = &conf.hotkeys[&x];
|
||||||
|
for c in hotcmd.chars() {
|
||||||
|
write_letter(&mut mycommand,
|
||||||
|
&mut current_pos,
|
||||||
|
&mut max_pos,
|
||||||
|
c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'z' => {
|
||||||
|
print!("Z");
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
};
|
||||||
|
|
||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user