Skip to content

Commit

Permalink
fix: 修正对tty读入的字符的处理,使其与Linux一致 (#46)
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 cac674d commit 730e030
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 53 deletions.
43 changes: 37 additions & 6 deletions src/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ pub enum SpecialKeycode {
BackSpace = b'\x08',
Tab = b'\t',

FunctionKeyPrefix = 0xE0,
ESC = 0x1B,
PauseBreak = 0xE1,
}

#[repr(u8)]
#[derive(Debug, FromPrimitive, TryFromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
impl Into<u8> for SpecialKeycode {
fn into(self) -> u8 {
self as u8
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[allow(dead_code)]
pub enum FunctionKeySuffix {
Up = 0x48,
Expand All @@ -27,8 +32,34 @@ pub enum FunctionKeySuffix {
End = 0x4F,
}

impl Into<u8> for SpecialKeycode {
fn into(self) -> u8 {
self as u8
impl FunctionKeySuffix {
pub const SUFFIX_0: u8 = 0x5b;
pub fn bytes(self) -> &'static [u8] {
match self {
FunctionKeySuffix::Up => &[0x5b, 0x41],
FunctionKeySuffix::Down => &[0x5b, 0x42],
FunctionKeySuffix::Left => &[0x5b, 0x44],
FunctionKeySuffix::Right => &[0x5b, 0x43],
FunctionKeySuffix::Home => &[0x5b, 0x48],
FunctionKeySuffix::End => &[0x5b, 0x46],
}
}

pub fn try_from(value: &[u8]) -> Option<Self> {
match value {
[0x5b, 0x41] => Some(FunctionKeySuffix::Up),
[0x5b, 0x42] => Some(FunctionKeySuffix::Down),
[0x5b, 0x44] => Some(FunctionKeySuffix::Left),
[0x5b, 0x43] => Some(FunctionKeySuffix::Right),
[0x5b, 0x48] => Some(FunctionKeySuffix::Home),
[0x5b, 0x46] => Some(FunctionKeySuffix::End),
_ => None,
}
}
}

impl Into<&[u8]> for FunctionKeySuffix {
fn into(self) -> &'static [u8] {
self.bytes()
}
}
103 changes: 56 additions & 47 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,59 @@ impl Shell {
}
}

fn handle_funckey(&mut self, command_index: &mut usize) {
let key = Self::read_char();
if key != FunctionKeySuffix::SUFFIX_0 {
return;
}
let key1 = Self::read_char();
let suffix = &[key, key1];
// println!("suffix: {:?}", suffix);
let function_key = FunctionKeySuffix::try_from(suffix);
if function_key.is_none() {
return;
}
let function_key = function_key.unwrap();

match function_key {
FunctionKeySuffix::Up => {
if *command_index > 0 {
*command_index -= 1;
self.printer
.change_line(self.history_commands.get(*command_index).unwrap());
}
}

FunctionKeySuffix::Down => {
if *command_index < self.history_commands.len() - 1 {
*command_index += 1;
self.printer
.change_line(self.history_commands.get(*command_index).unwrap());
}
}

FunctionKeySuffix::Left => {
if self.printer.cursor > 0 {
self.printer.cursor_left(1);
}
}

FunctionKeySuffix::Right => {
if self.printer.cursor < self.printer.buf.borrow().len() {
self.printer.cursor_right(1);
}
}

FunctionKeySuffix::Home => {
self.printer.home();
}

FunctionKeySuffix::End => {
self.printer.end();
}
}
}

fn readline(&mut self) -> usize {
let mut stdout = std::io::stdout();
self.history_commands.push(Rc::clone(&self.printer.buf));
Expand All @@ -152,48 +205,8 @@ impl Shell {
let key = Self::read_char();
if let Ok(special_key) = SpecialKeycode::try_from(key) {
match special_key {
SpecialKeycode::FunctionKeyPrefix => {
let key = Self::read_char();
let function_key = FunctionKeySuffix::try_from(key).unwrap();
match function_key {
FunctionKeySuffix::Up => {
if command_index > 0 {
command_index -= 1;
self.printer.change_line(
self.history_commands.get(command_index).unwrap(),
);
}
}

FunctionKeySuffix::Down => {
if command_index < self.history_commands.len() - 1 {
command_index += 1;
self.printer.change_line(
self.history_commands.get(command_index).unwrap(),
);
}
}

FunctionKeySuffix::Left => {
if self.printer.cursor > 0 {
self.printer.cursor_left(1);
}
}

FunctionKeySuffix::Right => {
if self.printer.cursor < self.printer.buf.borrow().len() {
self.printer.cursor_right(1);
}
}

FunctionKeySuffix::Home => {
self.printer.home();
}

FunctionKeySuffix::End => {
self.printer.end();
}
}
SpecialKeycode::ESC => {
self.handle_funckey(&mut command_index);
}

SpecialKeycode::LF | SpecialKeycode::CR => {
Expand All @@ -202,14 +215,10 @@ impl Shell {
return 1;
}

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

SpecialKeycode::Delete => {
self.printer.delete(1);
}

SpecialKeycode::Tab => {
let mut buf = self.printer.buf.deref().borrow().clone();
buf.truncate(self.printer.cursor);
Expand Down

0 comments on commit 730e030

Please sign in to comment.