From 357eb3cd32702116fdc44505656a889d42780dab Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Tue, 13 Aug 2024 16:01:00 +0200 Subject: [PATCH] fish_key_reader: use char_to_symbol for verbose output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit byte_to_symbol was broken because it didn't iterate by byte, it iterated by rust-char, which is a codepoint. So it failed for everything outside of ascii and, because of a mistaken bound, ascii chars from 0x21 to 0x2F ("!" to "/" - all the punctuation). char_to_symbol will print printable codepoints as-is and others escaped. This is okay - something like `decoded from: +` or `decoded from: ö` is entirely understandable, there is no need to tell you that "ö" is \xc3\xb6. This reverts commit 423e5f6c039212fdd5d6b6f46e644666a460d944. --- src/bin/fish_key_reader.rs | 4 ++-- src/key.rs | 21 +++++---------------- tests/pexpects/fkr.py | 3 +++ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/bin/fish_key_reader.rs b/src/bin/fish_key_reader.rs index d874941de..6d83da1a4 100644 --- a/src/bin/fish_key_reader.rs +++ b/src/bin/fish_key_reader.rs @@ -20,7 +20,7 @@ use fish::{ eprintf, fprintf, input::input_terminfo_get_name, input_common::{terminal_protocols_enable_ifn, CharEvent, InputEventQueue, InputEventQueuer}, - key::{self, byte_to_symbol, Key}, + key::{self, char_to_symbol, Key}, panic::panic_handler, print_help::print_help, printf, @@ -104,7 +104,7 @@ fn process_input(continuous_mode: bool, verbose: bool) -> i32 { if verbose { printf!("# decoded from: "); for byte in kevt.seq.chars() { - printf!("%s", &byte_to_symbol(byte)); + printf!("%s", &char_to_symbol(byte)); } printf!("\n"); } diff --git a/src/key.rs b/src/key.rs index 415798d17..086f5e869 100644 --- a/src/key.rs +++ b/src/key.rs @@ -418,7 +418,7 @@ fn ctrl_to_symbol(buf: &mut WString, c: char) { // 2. key names that are given as raw escape sequence (\e123); those we want to display // similar to how they are given. - let ctrl_symbolic_names: [&wstr; 28] = { + let ctrl_symbolic_names: [&wstr; 29] = { std::array::from_fn(|i| match i { 8 => L!("\\b"), 9 => L!("\\t"), @@ -453,26 +453,15 @@ fn ascii_printable_to_symbol(buf: &mut WString, c: char) { } } -pub fn byte_to_symbol(c: char) -> WString { - let mut buff = WString::new(); - let buf = &mut buff; - if c <= '\x1b' { - ctrl_to_symbol(buf, c); - } else if ('\u{30}'..'\u{7f}').contains(&c) { - // ASCII characters that are not control characters - ascii_printable_to_symbol(buf, c); - } else { - sprintf!(=> buf, "\\x%02x", 0x7f); - } - buff -} - /// Convert a wide-char to a symbol that can be used in our output. -pub(crate) fn char_to_symbol(c: char) -> WString { +pub fn char_to_symbol(c: char) -> WString { let mut buff = WString::new(); let buf = &mut buff; if c <= ' ' { ctrl_to_symbol(buf, c); + } else if c == '\u{7f}' { + // DEL is at the end of the ASCII range + sprintf!(=> buf, "\\x%02x", 0x7f); } else if c < '\u{80}' { // ASCII characters that are not control characters ascii_printable_to_symbol(buf, c); diff --git a/tests/pexpects/fkr.py b/tests/pexpects/fkr.py index 69b7248cb..8b6c052f7 100644 --- a/tests/pexpects/fkr.py +++ b/tests/pexpects/fkr.py @@ -37,6 +37,9 @@ sleep(0.020) send("\x1B") expect_str("# decoded from: \\e\r\n") expect_str("bind escape 'do something'\r\n") +send("ö") +expect_str("# decoded from: ö\r\n") +expect_str("bind ö 'do something'\r\n") send("\u1234") expect_str("bind ሴ 'do something'\r\n")