Hotkeys babeeee

This commit is contained in:
Justine 2023-02-18 13:34:26 +01:00
parent 6abe6d886d
commit 506007e71b

View File

@ -209,17 +209,39 @@ pub mod shell {
//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 + 1).try_into().unwrap();
write!(stdout, "{}", cursor::Right(right_spaces));
let chars_left = max_pos - current_pos;
if current_pos < max_pos {
write!(stdout, "{}", cursor::Right(chars_left as u16));
}
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();
//write!(stdout, "{}", cursor::Left(1));
//write!(stdout, "\x1b[K").unwrap();
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() {
@ -260,8 +282,8 @@ pub mod shell {
stdout.flush();
for c in stdin.keys() {
match c.unwrap() {
let k = c.unwrap();
match k {
Key::Char('\t') => {
//Do NOT search on an empty command
if *&mycommand.len() == 0 {
@ -343,31 +365,18 @@ pub mod shell {
}
},
Key::Char(c) => {
//if at the end of the command...
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;
write_letter(&mut mycommand, &mut current_pos, &mut max_pos, c);
}
},
Key::Delete => {
if current_pos < max_pos {
if mycommand.chars().count() == 1
&& current_pos == 0 {
clear_line(&max_pos, &current_pos);
mycommand.clear();
max_pos = 0;
current_pos = 0;
} else if current_pos < max_pos {
mycommand.remove(current_pos);
if current_pos > 0 {
current_pos -= 1;
@ -383,7 +392,15 @@ pub mod shell {
},
Key::Backspace => {
if current_pos > 0 {
if current_pos == 0 {
continue;
}
if mycommand.chars().count() == 1 {
clear_line(&max_pos, &current_pos);
mycommand.clear();
current_pos = 0;
max_pos = 0;
} else {
current_pos -= 1;
max_pos -= 1;
mycommand.remove(current_pos);
@ -443,10 +460,32 @@ pub mod shell {
print!("{}", cursor::Right(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();
}