2021-06-25 07:58:15 +04:00
|
|
|
use bitflags::bitflags;
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
/// Represents key modifiers (shift, control, alt).
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
|
|
pub struct KeyModifiers: u8 {
|
|
|
|
const SHIFT = 0b0000_0001;
|
|
|
|
const CONTROL = 0b0000_0010;
|
|
|
|
const ALT = 0b0000_0100;
|
|
|
|
const NONE = 0b0000_0000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "term")]
|
|
|
|
impl From<KeyModifiers> for crossterm::event::KeyModifiers {
|
|
|
|
fn from(key_modifiers: KeyModifiers) -> Self {
|
|
|
|
use crossterm::event::KeyModifiers as CKeyModifiers;
|
|
|
|
|
|
|
|
let mut result = CKeyModifiers::NONE;
|
|
|
|
|
2021-06-25 08:13:15 +04:00
|
|
|
if key_modifiers.contains(KeyModifiers::SHIFT) {
|
|
|
|
result.insert(CKeyModifiers::SHIFT);
|
2021-06-25 07:58:15 +04:00
|
|
|
}
|
2021-06-25 08:13:15 +04:00
|
|
|
if key_modifiers.contains(KeyModifiers::CONTROL) {
|
|
|
|
result.insert(CKeyModifiers::CONTROL);
|
2021-06-25 07:58:15 +04:00
|
|
|
}
|
2021-06-25 08:13:15 +04:00
|
|
|
if key_modifiers.contains(KeyModifiers::ALT) {
|
|
|
|
result.insert(CKeyModifiers::ALT);
|
2021-06-25 07:58:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "term")]
|
|
|
|
impl From<crossterm::event::KeyModifiers> for KeyModifiers {
|
|
|
|
fn from(val: crossterm::event::KeyModifiers) -> Self {
|
|
|
|
use crossterm::event::KeyModifiers as CKeyModifiers;
|
|
|
|
|
|
|
|
let mut result = KeyModifiers::NONE;
|
|
|
|
|
2021-06-25 08:13:15 +04:00
|
|
|
if val.contains(CKeyModifiers::SHIFT) {
|
|
|
|
result.insert(KeyModifiers::SHIFT);
|
2021-06-25 07:58:15 +04:00
|
|
|
}
|
2021-06-25 08:13:15 +04:00
|
|
|
if val.contains(CKeyModifiers::CONTROL) {
|
|
|
|
result.insert(KeyModifiers::CONTROL);
|
2021-06-25 07:58:15 +04:00
|
|
|
}
|
2021-06-25 08:13:15 +04:00
|
|
|
if val.contains(CKeyModifiers::ALT) {
|
|
|
|
result.insert(KeyModifiers::ALT);
|
2021-06-25 07:58:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a key.
|
2021-11-01 18:50:12 +04:00
|
|
|
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
|
2021-06-25 07:58:15 +04:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
|
|
pub enum KeyCode {
|
|
|
|
/// Backspace key.
|
|
|
|
Backspace,
|
|
|
|
/// Enter key.
|
|
|
|
Enter,
|
|
|
|
/// Left arrow key.
|
|
|
|
Left,
|
|
|
|
/// Right arrow key.
|
|
|
|
Right,
|
|
|
|
/// Up arrow key.
|
|
|
|
Up,
|
|
|
|
/// Down arrow key.
|
|
|
|
Down,
|
|
|
|
/// Home key.
|
|
|
|
Home,
|
|
|
|
/// End key.
|
|
|
|
End,
|
|
|
|
/// Page up key.
|
|
|
|
PageUp,
|
2021-07-27 11:32:05 +04:00
|
|
|
/// Page down key.
|
2021-06-25 07:58:15 +04:00
|
|
|
PageDown,
|
|
|
|
/// Tab key.
|
|
|
|
Tab,
|
|
|
|
/// Delete key.
|
|
|
|
Delete,
|
|
|
|
/// Insert key.
|
|
|
|
Insert,
|
|
|
|
/// F key.
|
|
|
|
///
|
|
|
|
/// `KeyCode::F(1)` represents F1 key, etc.
|
|
|
|
F(u8),
|
|
|
|
/// A character.
|
|
|
|
///
|
|
|
|
/// `KeyCode::Char('c')` represents `c` character, etc.
|
|
|
|
Char(char),
|
|
|
|
/// Null.
|
|
|
|
Null,
|
|
|
|
/// Escape key.
|
|
|
|
Esc,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "term")]
|
|
|
|
impl From<KeyCode> for crossterm::event::KeyCode {
|
|
|
|
fn from(key_code: KeyCode) -> Self {
|
|
|
|
use crossterm::event::KeyCode as CKeyCode;
|
|
|
|
|
|
|
|
match key_code {
|
|
|
|
KeyCode::Backspace => CKeyCode::Backspace,
|
|
|
|
KeyCode::Enter => CKeyCode::Enter,
|
|
|
|
KeyCode::Left => CKeyCode::Left,
|
|
|
|
KeyCode::Right => CKeyCode::Right,
|
|
|
|
KeyCode::Up => CKeyCode::Up,
|
|
|
|
KeyCode::Down => CKeyCode::Down,
|
|
|
|
KeyCode::Home => CKeyCode::Home,
|
|
|
|
KeyCode::End => CKeyCode::End,
|
|
|
|
KeyCode::PageUp => CKeyCode::PageUp,
|
|
|
|
KeyCode::PageDown => CKeyCode::PageDown,
|
|
|
|
KeyCode::Tab => CKeyCode::Tab,
|
|
|
|
KeyCode::Delete => CKeyCode::Delete,
|
|
|
|
KeyCode::Insert => CKeyCode::Insert,
|
|
|
|
KeyCode::F(f_number) => CKeyCode::F(f_number),
|
|
|
|
KeyCode::Char(character) => CKeyCode::Char(character),
|
|
|
|
KeyCode::Null => CKeyCode::Null,
|
|
|
|
KeyCode::Esc => CKeyCode::Esc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "term")]
|
|
|
|
impl From<crossterm::event::KeyCode> for KeyCode {
|
|
|
|
fn from(val: crossterm::event::KeyCode) -> Self {
|
|
|
|
use crossterm::event::KeyCode as CKeyCode;
|
|
|
|
|
|
|
|
match val {
|
|
|
|
CKeyCode::Backspace => KeyCode::Backspace,
|
|
|
|
CKeyCode::Enter => KeyCode::Enter,
|
|
|
|
CKeyCode::Left => KeyCode::Left,
|
|
|
|
CKeyCode::Right => KeyCode::Right,
|
|
|
|
CKeyCode::Up => KeyCode::Up,
|
|
|
|
CKeyCode::Down => KeyCode::Down,
|
|
|
|
CKeyCode::Home => KeyCode::Home,
|
|
|
|
CKeyCode::End => KeyCode::End,
|
|
|
|
CKeyCode::PageUp => KeyCode::PageUp,
|
|
|
|
CKeyCode::PageDown => KeyCode::PageDown,
|
|
|
|
CKeyCode::Tab => KeyCode::Tab,
|
2021-12-06 07:24:25 +04:00
|
|
|
CKeyCode::BackTab => unreachable!("BackTab should have been handled on KeyEvent level"),
|
2021-06-25 07:58:15 +04:00
|
|
|
CKeyCode::Delete => KeyCode::Delete,
|
|
|
|
CKeyCode::Insert => KeyCode::Insert,
|
|
|
|
CKeyCode::F(f_number) => KeyCode::F(f_number),
|
|
|
|
CKeyCode::Char(character) => KeyCode::Char(character),
|
|
|
|
CKeyCode::Null => KeyCode::Null,
|
|
|
|
CKeyCode::Esc => KeyCode::Esc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|