1use std::fmt;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum KeyCode {
16 Char(char),
21 Enter,
23 Tab,
25 Escape,
27 Backspace,
29 Delete,
31 Insert,
33 Up,
35 Down,
37 Left,
39 Right,
41 Home,
43 End,
45 PageUp,
47 PageDown,
49 F(u8),
51 Unidentified,
54}
55
56impl KeyCode {
57 pub fn display(&self) -> String {
61 match self {
62 KeyCode::Char(c) => c.to_string(),
63 KeyCode::Enter => "Enter".to_string(),
64 KeyCode::Tab => "Tab".to_string(),
65 KeyCode::Escape => "Esc".to_string(),
66 KeyCode::Backspace => "Backspace".to_string(),
67 KeyCode::Delete => "Delete".to_string(),
68 KeyCode::Insert => "Insert".to_string(),
69 KeyCode::Up => "↑".to_string(),
70 KeyCode::Down => "↓".to_string(),
71 KeyCode::Left => "←".to_string(),
72 KeyCode::Right => "→".to_string(),
73 KeyCode::Home => "Home".to_string(),
74 KeyCode::End => "End".to_string(),
75 KeyCode::PageUp => "PgUp".to_string(),
76 KeyCode::PageDown => "PgDn".to_string(),
77 KeyCode::F(n) => format!("F{n}"),
78 KeyCode::Unidentified => "?".to_string(),
79 }
80 }
81}
82
83impl fmt::Display for KeyCode {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 write!(f, "{}", self.display())
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn display_covers_named_keys() {
95 assert_eq!(KeyCode::Enter.to_string(), "Enter");
96 assert_eq!(KeyCode::Escape.to_string(), "Esc");
97 assert_eq!(KeyCode::Backspace.to_string(), "Backspace");
98 assert_eq!(KeyCode::Delete.to_string(), "Delete");
99 assert_eq!(KeyCode::Up.to_string(), "↑");
100 assert_eq!(KeyCode::Left.to_string(), "←");
101 assert_eq!(KeyCode::F(5).to_string(), "F5");
102 assert_eq!(KeyCode::F(12).to_string(), "F12");
103 }
104
105 #[test]
106 fn display_chars_verbatim() {
107 assert_eq!(KeyCode::Char('a').to_string(), "a");
108 assert_eq!(KeyCode::Char(' ').to_string(), " ");
109 assert_eq!(KeyCode::Char('?').to_string(), "?");
110 }
111
112 #[test]
113 fn display_unidentified_is_placeholder() {
114 assert_eq!(KeyCode::Unidentified.to_string(), "?");
115 }
116}