Skip to content

Commit

Permalink
fix: 修复delete键 (#47)
Browse files Browse the repository at this point in the history
Signed-off-by: longjin <[email protected]>
  • Loading branch information
fslongjin authored Sep 28, 2024
1 parent 730e030 commit 6c1ca14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
27 changes: 19 additions & 8 deletions src/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use num_enum::TryFromPrimitive;
pub enum SpecialKeycode {
LF = b'\n',
CR = b'\r',
Delete = b'\x7f',
BackSpace = b'\x08',
BackSpace = b'\x7f',
Tab = b'\t',

ESC = 0x1B,
Expand All @@ -23,13 +22,14 @@ impl Into<u8> for SpecialKeycode {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[allow(dead_code)]
pub enum FunctionKeySuffix {
Up = 0x48,
Down = 0x50,
Left = 0x4B,
Right = 0x4D,
Up,
Down,
Left,
Right,

Home = 0x47,
End = 0x4F,
Home,
End,
Delete,
}

impl FunctionKeySuffix {
Expand All @@ -42,6 +42,7 @@ impl FunctionKeySuffix {
FunctionKeySuffix::Right => &[0x5b, 0x43],
FunctionKeySuffix::Home => &[0x5b, 0x48],
FunctionKeySuffix::End => &[0x5b, 0x46],
FunctionKeySuffix::Delete => &[0x5b, 0x33, 0x7e],
}
}

Expand All @@ -53,9 +54,19 @@ impl FunctionKeySuffix {
[0x5b, 0x43] => Some(FunctionKeySuffix::Right),
[0x5b, 0x48] => Some(FunctionKeySuffix::Home),
[0x5b, 0x46] => Some(FunctionKeySuffix::End),
[0x5b, 0x33, 0x7e] => Some(FunctionKeySuffix::Delete),
_ => None,
}
}

pub fn should_read_more(value: &[u8]) -> bool {
match value.len() {
0 => true,
1 => value[0] == Self::SUFFIX_0,
2 => value[0] == Self::SUFFIX_0 && value[1] == 0x33,
_ => false,
}
}
}

impl Into<&[u8]> for FunctionKeySuffix {
Expand Down
16 changes: 8 additions & 8 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,16 @@ impl Shell {
}

fn handle_funckey(&mut self, command_index: &mut usize) {
let key = Self::read_char();
if key != FunctionKeySuffix::SUFFIX_0 {
return;
let mut keys = Vec::new();

while FunctionKeySuffix::should_read_more(&keys) {
keys.push(Self::read_char());
}
let key1 = Self::read_char();
let suffix = &[key, key1];
// println!("suffix: {:?}", suffix);
let function_key = FunctionKeySuffix::try_from(suffix);
let function_key = FunctionKeySuffix::try_from(&keys);
if function_key.is_none() {
return;
}

let function_key = function_key.unwrap();

match function_key {
Expand Down Expand Up @@ -194,6 +193,7 @@ impl Shell {
FunctionKeySuffix::End => {
self.printer.end();
}
FunctionKeySuffix::Delete => self.printer.delete(1),
}
}

Expand All @@ -215,7 +215,7 @@ impl Shell {
return 1;
}

SpecialKeycode::BackSpace | SpecialKeycode::Delete => {
SpecialKeycode::BackSpace => {
self.printer.backspace();
}

Expand Down

0 comments on commit 6c1ca14

Please sign in to comment.