crossterm
crossterm copied to clipboard
Implement From<KeyEvent> for String
Is your feature request related to a problem? Please describe. I’m implementing a system to automatically build help panes from a contextual action structure. I have everything I need already except the ability to turn the key event into a string for displaying in a nice way.
Describe the solution you'd like I would love to see the from keyevent trait for string,so it can be used for this purposes. It could be behind a feature flag so only those needing it get it.
Describe alternatives you've considered if any I can build a wrapper type in my own codebase and implement it there.
Additional context Anything else? Nope
Does the fmt::Debug implementation suit your needs? E.g.
use crossterm::event::{self, Event};
fn main() {
loop {
if let Ok(Event::Key(ev)) = event::read() {
println!("{ev:?}");
break;
}
}
}
KeyEvent { code: Char('a'), modifiers: NONE, kind: Press, state: NONE }
The idea is to pretty print the key as part of the help message, so no, the debug does not fit my needs.
On Sat, Jul 29, 2023 at 1:23 PM Peter Hebden @.***> wrote:
Does the fmt::Debug implementation suit your needs? E.g.
use crossterm::event::{self, Event}; fn main() { loop { if let Ok(Event::Key(ev)) = event::read() { println!("{ev:?}"); break; } }}
KeyEvent { code: Char('a'), modifiers: NONE, kind: Press, state: NONE }
— Reply to this email directly, view it on GitHub https://github.com/crossterm-rs/crossterm/issues/792#issuecomment-1656708789, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKJWMZO5HFR4CXEU5AFELXSTXDLANCNFSM6AAAAAA2CXXQHY . You are receiving this because you authored the thread.Message ID: @.***>
--
https://danielorodriguez.com
I think this probably should be impl Display for KeyEvent and I want it for similar reasons (help text in TUI apps). That said, the fact that the event has kindand state fields makes any implementation a bit weird - they would either show too much information for help text, or have to throw away information and be ambiguous when displaying a key repeat event or disambiguating whether the event came from a key on the keypad / whether caps / num lock are on.
I think it's probably reasonable to implement Display for KeyCode and KeyModifiier, but I'd consider not using the event for help text and instead using custom struct that just worries about the code and modifier in your app.