Skip to content

Commit

Permalink
fish_key_reader: Humanize key descriptions
Browse files Browse the repository at this point in the history
This used to print all codepoints outside of the ASCII range (i.e.
above 0x80) in \uXXXX or \UYYYYYYYY notation.

That's quite awkward, considering that this is about keys that are
being pressed, and many keyboards have actual symbols for these on
them - I have an "ö" key, so I would like to use `bind ö` and not
`bind \u00F6`. So we go by iswgraph.

On a slightly different note, `\e` was written as `\c[ (or \e)`. I do
not believe anyone really uses `\c[` (the `[` would need to
be escaped!), and it's confusing and unnecessary to even mention that.
  • Loading branch information
faho committed Aug 26, 2023
1 parent d803ebb commit 55c425a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc_src/cmds/fish_key_reader.rst
Expand Up @@ -56,7 +56,7 @@ Example
> fish_key_reader --verbose
Press a key:
# press alt+enter
hex: 1B char: \c[ (or \e)
hex: 1B char: \e
( 0.027 ms) hex: D char: \cM (or \r)
bind \e\r 'do something'

7 changes: 6 additions & 1 deletion src/fish_key_reader.cpp
Expand Up @@ -138,14 +138,19 @@ static void ascii_printable_to_symbol(wchar_t *buf, int buf_len, wchar_t wc, boo
static wchar_t *char_to_symbol(wchar_t wc, bool bind_friendly) {
static wchar_t buf[64];

if (wc < L' ') { // ASCII control character
if (wc == '\x1b') {
// Escape - this is *technically* also \c[
std::swprintf(buf, sizeof(buf) / sizeof(*buf), L"\\e");
} else if (wc < L' ') { // ASCII control character
ctrl_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
} else if (wc == L' ') { // the "space" character
space_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
} else if (wc == 0x7F) { // the "del" character
del_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
} else if (wc < 0x80) { // ASCII characters that are not control characters
ascii_printable_to_symbol(buf, sizeof(buf) / sizeof(*buf), wc, bind_friendly);
} else if (std::iswgraph(wc)) {
std::swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lc", wc);
}
// Conditional handling of BMP Unicode characters depends on the encoding. Assume width of wchar_t
// corresponds to the encoding, i.e. WCHAR_T_BITS == 16 implies UTF-16 and WCHAR_T_BITS == 32
Expand Down
2 changes: 1 addition & 1 deletion tests/pexpects/fkr.py
Expand Up @@ -31,7 +31,7 @@
sleep(0.020)
# send "\x1B\xE1\x88\xB4"
send("\x1B\u1234")
expect_str("char: \\u1234\r\nbind \\e\\u1234 'do something'\r\n")
expect_str("char: \r\nbind \\eሴ 'do something'\r\n")

# Is a NULL char echoed correctly?
sleep(0.020)
Expand Down

0 comments on commit 55c425a

Please sign in to comment.