ratatui_toolkit/primitives/termtui/keybindings/methods/
key_to_display_string.rs1use crate::primitives::termtui::keybindings::TermTuiKeyBindings;
4use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
5
6impl TermTuiKeyBindings {
7 pub fn key_to_display_string(key: &KeyEvent) -> String {
9 let mut parts = Vec::new();
10
11 if key.modifiers.contains(KeyModifiers::CONTROL) {
12 parts.push("^");
13 }
14 if key.modifiers.contains(KeyModifiers::ALT) {
15 parts.push("Alt+");
16 }
17 if key.modifiers.contains(KeyModifiers::SHIFT) {
18 parts.push("\u{21e7}"); }
20
21 let key_str = match key.code {
22 KeyCode::Char(c) => c.to_uppercase().to_string(),
23 KeyCode::Enter => "Enter".to_string(),
24 KeyCode::Esc => "Esc".to_string(),
25 KeyCode::Backspace => "Bksp".to_string(),
26 KeyCode::Tab => "Tab".to_string(),
27 KeyCode::Up => "\u{2191}".to_string(),
28 KeyCode::Down => "\u{2193}".to_string(),
29 KeyCode::Left => "\u{2190}".to_string(),
30 KeyCode::Right => "\u{2192}".to_string(),
31 KeyCode::Home => "Home".to_string(),
32 KeyCode::End => "End".to_string(),
33 KeyCode::PageUp => "PgUp".to_string(),
34 KeyCode::PageDown => "PgDn".to_string(),
35 KeyCode::Delete => "Del".to_string(),
36 KeyCode::Insert => "Ins".to_string(),
37 KeyCode::F(n) => format!("F{}", n),
38 _ => "?".to_string(),
39 };
40
41 parts.push(&key_str);
42 parts.concat()
43 }
44}