read_char: Handle non-tty terminals explicitly#124
Merged
mitsuhiko merged 1 commit intoconsole-rs:masterfrom Jan 14, 2023
Merged
read_char: Handle non-tty terminals explicitly#124mitsuhiko merged 1 commit intoconsole-rs:masterfrom
mitsuhiko merged 1 commit intoconsole-rs:masterfrom
Conversation
`Key::Unknown` is produced only when: - A keycode is not recognized by the `key_from_key_code` function under Windows - `read_key` is invoked on a non-tty terminal The two places in the code which explicitly handled `Key::Unknown` were treating it as a proxy for an "unattended terminal" check. Since this is only 100% valid to do when running under *nix I've replaced these handlers with an explicit `!self.is_tty` check instead, which is a common pattern in the Term code. `Key::Unknown` is now assumed to represent an unprintable / control key and skipped when being evaluated.
Contributor
Author
|
Related PR on dialoguer: console-rs/dialoguer#192 |
|
Pressing |
benesch
pushed a commit
to benesch/console
that referenced
this pull request
Dec 4, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Key::Unknownis produced only when:key_from_key_codefunction under Windows:console/src/windows_term.rs
Line 337 in b3ecb7d
read_keyis invoked on a non-tty terminal:console/src/term.rs
Lines 274 to 276 in b3ecb7d
The two places in the code which explicitly handled
Key::Unknownwere treating it as a proxy for an "unattended terminal" check:console/src/term.rs
Lines 258 to 263 in b3ecb7d
Since this is only 100% valid to do when running under *nix I've replaced these handlers with an explicit
!self.is_ttycheck instead, which is a common pattern in the Term code.Key::Unknownis now assumed to represent an unprintable / control key and skipped when being evaluated.I found this issue when pressing the
Ctrlkey while using theread_keyfunction, which causes it to returnErr(Not a terminal)under Windows (but works fine on Linux). In this case, I was in the process of pressingCtrl + Cin order to terminate the program.It looks like this issue also occurred previously for the
ShiftandAltkeys under Windows - rather than extend theKeysenum further to support theCtrlkey I think the proposed fix is a little more future proof (though you may like to add support for VK_CONTROL for the sake of completeness anyway).