Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修正对tty读入的字符的处理,使其与Linux一致 #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading