datafusion_tui/events/
key.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17//
18// MIT License
19
20// Copyright (c) 2021 Alexander Keliris
21
22// Permission is hereby granted, free of charge, to any person obtaining a copy
23// of this software and associated documentation files (the "Software"), to deal
24// in the Software without restriction, including without limitation the rights
25// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26// copies of the Software, and to permit persons to whom the Software is
27// furnished to do so, subject to the following conditions:
28
29// The above copyright notice and this permission notice shall be included in all
30// copies or substantial portions of the Software.
31
32// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38// SOFTWARE.
39//
40// Link: https://github.com/Rigellute/spotify-tui/blob/master/src/event/key.rs
41
42use crossterm::event;
43use std::fmt;
44
45/// Represents an key.
46#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
47pub enum Key {
48    /// Both Enter (or Return) and numpad Enter
49    Enter,
50    /// Tabulation key
51    Tab,
52    /// Backspace key
53    Backspace,
54    /// Escape key
55    Esc,
56
57    /// Left arrow
58    Left,
59    /// Right arrow
60    Right,
61    /// Up arrow
62    Up,
63    /// Down arrow
64    Down,
65
66    /// Insert key
67    Ins,
68    /// Delete key
69    Delete,
70    /// Home key
71    Home,
72    /// End key
73    End,
74    /// Page Up key
75    PageUp,
76    /// Page Down key
77    PageDown,
78
79    /// F0 key
80    F0,
81    /// F1 key
82    F1,
83    /// F2 key
84    F2,
85    /// F3 key
86    F3,
87    /// F4 key
88    F4,
89    /// F5 key
90    F5,
91    /// F6 key
92    F6,
93    /// F7 key
94    F7,
95    /// F8 key
96    F8,
97    /// F9 key
98    F9,
99    /// F10 key
100    F10,
101    /// F11 key
102    F11,
103    /// F12 key
104    F12,
105    Char(char),
106    Ctrl(char),
107    Alt(char),
108    Unknown,
109}
110
111impl Key {
112    /// Returns the function key corresponding to the given number
113    ///
114    /// 1 -> F1, etc...
115    ///
116    /// # Panics
117    ///
118    /// If `n == 0 || n > 12`
119    pub fn from_f(n: u8) -> Key {
120        match n {
121            0 => Key::F0,
122            1 => Key::F1,
123            2 => Key::F2,
124            3 => Key::F3,
125            4 => Key::F4,
126            5 => Key::F5,
127            6 => Key::F6,
128            7 => Key::F7,
129            8 => Key::F8,
130            9 => Key::F9,
131            10 => Key::F10,
132            11 => Key::F11,
133            12 => Key::F12,
134            _ => panic!("unknown function key: F{}", n),
135        }
136    }
137}
138
139impl fmt::Display for Key {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        match *self {
142            Key::Alt(' ') => write!(f, "<Alt+Space>"),
143            Key::Ctrl(' ') => write!(f, "<Ctrl+Space>"),
144            Key::Char(' ') => write!(f, "<Space>"),
145            Key::Alt(c) => write!(f, "<Alt+{}>", c),
146            Key::Ctrl(c) => write!(f, "<Ctrl+{}>", c),
147            Key::Char(c) => write!(f, "{}", c),
148            Key::Left | Key::Right | Key::Up | Key::Down => write!(f, "<{:?} Arrow Key>", self),
149            Key::Enter
150            | Key::Tab
151            | Key::Backspace
152            | Key::Esc
153            | Key::Ins
154            | Key::Delete
155            | Key::Home
156            | Key::End
157            | Key::PageUp
158            | Key::PageDown => write!(f, "<{:?}>", self),
159            _ => write!(f, "{:?}", self),
160        }
161    }
162}
163
164impl From<event::KeyEvent> for Key {
165    fn from(key_event: event::KeyEvent) -> Self {
166        match key_event {
167            event::KeyEvent {
168                code: event::KeyCode::Esc,
169                ..
170            } => Key::Esc,
171            event::KeyEvent {
172                code: event::KeyCode::Backspace,
173                ..
174            } => Key::Backspace,
175            event::KeyEvent {
176                code: event::KeyCode::Left,
177                ..
178            } => Key::Left,
179            event::KeyEvent {
180                code: event::KeyCode::Right,
181                ..
182            } => Key::Right,
183            event::KeyEvent {
184                code: event::KeyCode::Up,
185                ..
186            } => Key::Up,
187            event::KeyEvent {
188                code: event::KeyCode::Down,
189                ..
190            } => Key::Down,
191            event::KeyEvent {
192                code: event::KeyCode::Home,
193                ..
194            } => Key::Home,
195            event::KeyEvent {
196                code: event::KeyCode::End,
197                ..
198            } => Key::End,
199            event::KeyEvent {
200                code: event::KeyCode::PageUp,
201                ..
202            } => Key::PageUp,
203            event::KeyEvent {
204                code: event::KeyCode::PageDown,
205                ..
206            } => Key::PageDown,
207            event::KeyEvent {
208                code: event::KeyCode::Delete,
209                ..
210            } => Key::Delete,
211            event::KeyEvent {
212                code: event::KeyCode::Insert,
213                ..
214            } => Key::Ins,
215            event::KeyEvent {
216                code: event::KeyCode::F(n),
217                ..
218            } => Key::from_f(n),
219            event::KeyEvent {
220                code: event::KeyCode::Tab,
221                ..
222            } => Key::Tab,
223
224            // First check for char + modifier
225            event::KeyEvent {
226                code: event::KeyCode::Char(c),
227                modifiers: event::KeyModifiers::ALT,
228            } => Key::Alt(c),
229            event::KeyEvent {
230                code: event::KeyCode::Char(c),
231                modifiers: event::KeyModifiers::CONTROL,
232            } => Key::Ctrl(c),
233
234            event::KeyEvent {
235                code: event::KeyCode::Char(c),
236                ..
237            } => Key::Char(c),
238            event::KeyEvent {
239                code: event::KeyCode::Enter,
240                ..
241            } => Key::Enter,
242            _ => Key::Unknown,
243        }
244    }
245}