| 1 |
|
| 2 |
use anyhow::{anyhow, Error}; |
| 3 |
use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidthStr}; |
| 4 |
use serde::de::{self, Deserialize, Deserializer}; |
| 5 |
use std::fmt; |
| 6 |
|
| 7 |
pub use crate::keyboard::{KeyCode, KeyModifiers, MediaKeyCode, ModifierKeyCode}; |
| 8 |
|
| 9 |
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)] |
| 10 |
pub enum Event { |
| 11 |
FocusGained, |
| 12 |
FocusLost, |
| 13 |
Key(KeyEvent), |
| 14 |
Mouse(MouseEvent), |
| 15 |
Paste(String), |
| 16 |
Resize(u16, u16), |
| 17 |
IdleTimeout, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)] |
| 21 |
pub struct MouseEvent { |
| 22 |
|
| 23 |
pub kind: MouseEventKind, |
| 24 |
|
| 25 |
pub column: u16, |
| 26 |
|
| 27 |
pub row: u16, |
| 28 |
|
| 29 |
pub modifiers: KeyModifiers, |
| 30 |
} |
| 31 |
|
| 32 |
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)] |
| 33 |
pub enum MouseEventKind { |
| 34 |
|
| 35 |
Down(MouseButton), |
| 36 |
|
| 37 |
Up(MouseButton), |
| 38 |
|
| 39 |
Drag(MouseButton), |
| 40 |
|
| 41 |
Moved, |
| 42 |
|
| 43 |
ScrollDown, |
| 44 |
|
| 45 |
ScrollUp, |
| 46 |
|
| 47 |
ScrollLeft, |
| 48 |
|
| 49 |
ScrollRight, |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)] |
| 54 |
pub enum MouseButton { |
| 55 |
|
| 56 |
Left, |
| 57 |
|
| 58 |
Right, |
| 59 |
|
| 60 |
Middle, |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] |
| 65 |
pub struct KeyEvent { |
| 66 |
pub code: KeyCode, |
| 67 |
pub modifiers: KeyModifiers, |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
impl KeyEvent { |
| 72 |
|
| 73 |
pub fn char(&self) -> Option<char> { |
| 74 |
match self.code { |
| 75 |
KeyCode::Char(ch) => Some(ch), |
| 76 |
_ => None, |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub fn key_sequence_format(&self) -> String { |
| 97 |
let s = self.to_string(); |
| 98 |
if s.graphemes(true).count() > 1 { |
| 99 |
format!("<{}>", s) |
| 100 |
} else { |
| 101 |
s |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
pub(crate) mod keys { |
| 107 |
pub(crate) const BACKSPACE: &str = "backspace"; |
| 108 |
pub(crate) const ENTER: &str = "ret"; |
| 109 |
pub(crate) const LEFT: &str = "left"; |
| 110 |
pub(crate) const RIGHT: &str = "right"; |
| 111 |
pub(crate) const UP: &str = "up"; |
| 112 |
pub(crate) const DOWN: &str = "down"; |
| 113 |
pub(crate) const HOME: &str = "home"; |
| 114 |
pub(crate) const END: &str = "end"; |
| 115 |
pub(crate) const PAGEUP: &str = "pageup"; |
| 116 |
pub(crate) const PAGEDOWN: &str = "pagedown"; |
| 117 |
pub(crate) const TAB: &str = "tab"; |
| 118 |
pub(crate) const DELETE: &str = "del"; |
| 119 |
pub(crate) const INSERT: &str = "ins"; |
| 120 |
pub(crate) const NULL: &str = "null"; |
| 121 |
pub(crate) const ESC: &str = "esc"; |
| 122 |
pub(crate) const SPACE: &str = "space"; |
| 123 |
pub(crate) const MINUS: &str = "minus"; |
| 124 |
pub(crate) const LESS_THAN: &str = "lt"; |
| 125 |
pub(crate) const GREATER_THAN: &str = "gt"; |
| 126 |
pub(crate) const CAPS_LOCK: &str = "capslock"; |
| 127 |
pub(crate) const SCROLL_LOCK: &str = "scrolllock"; |
| 128 |
pub(crate) const NUM_LOCK: &str = "numlock"; |
| 129 |
pub(crate) const PRINT_SCREEN: &str = "printscreen"; |
| 130 |
pub(crate) const PAUSE: &str = "pause"; |
| 131 |
pub(crate) const MENU: &str = "menu"; |
| 132 |
pub(crate) const KEYPAD_BEGIN: &str = "keypadbegin"; |
| 133 |
pub(crate) const PLAY: &str = "play"; |
| 134 |
pub(crate) const PAUSE_MEDIA: &str = "pausemedia"; |
| 135 |
pub(crate) const PLAY_PAUSE: &str = "playpause"; |
| 136 |
pub(crate) const REVERSE: &str = "reverse"; |
| 137 |
pub(crate) const STOP: &str = "stop"; |
| 138 |
pub(crate) const FAST_FORWARD: &str = "fastforward"; |
| 139 |
pub(crate) const REWIND: &str = "rewind"; |
| 140 |
pub(crate) const TRACK_NEXT: &str = "tracknext"; |
| 141 |
pub(crate) const TRACK_PREVIOUS: &str = "trackprevious"; |
| 142 |
pub(crate) const RECORD: &str = "record"; |
| 143 |
pub(crate) const LOWER_VOLUME: &str = "lowervolume"; |
| 144 |
pub(crate) const RAISE_VOLUME: &str = "raisevolume"; |
| 145 |
pub(crate) const MUTE_VOLUME: &str = "mutevolume"; |
| 146 |
pub(crate) const LEFT_SHIFT: &str = "leftshift"; |
| 147 |
pub(crate) const LEFT_CONTROL: &str = "leftcontrol"; |
| 148 |
pub(crate) const LEFT_ALT: &str = "leftalt"; |
| 149 |
pub(crate) const LEFT_SUPER: &str = "leftsuper"; |
| 150 |
pub(crate) const LEFT_HYPER: &str = "lefthyper"; |
| 151 |
pub(crate) const LEFT_META: &str = "leftmeta"; |
| 152 |
pub(crate) const RIGHT_SHIFT: &str = "rightshift"; |
| 153 |
pub(crate) const RIGHT_CONTROL: &str = "rightcontrol"; |
| 154 |
pub(crate) const RIGHT_ALT: &str = "rightalt"; |
| 155 |
pub(crate) const RIGHT_SUPER: &str = "rightsuper"; |
| 156 |
pub(crate) const RIGHT_HYPER: &str = "righthyper"; |
| 157 |
pub(crate) const RIGHT_META: &str = "rightmeta"; |
| 158 |
pub(crate) const ISO_LEVEL_3_SHIFT: &str = "isolevel3shift"; |
| 159 |
pub(crate) const ISO_LEVEL_5_SHIFT: &str = "isolevel5shift"; |
| 160 |
} |
| 161 |
|
| 162 |
impl fmt::Display for KeyEvent { |
| 163 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { |
| 164 |
f.write_fmt(format_args!( |
| 165 |
"{}{}{}{}", |
| 166 |
if self.modifiers.contains(KeyModifiers::SUPER) { |
| 167 |
"Meta-" |
| 168 |
} else { |
| 169 |
"" |
| 170 |
}, |
| 171 |
if self.modifiers.contains(KeyModifiers::SHIFT) { |
| 172 |
"S-" |
| 173 |
} else { |
| 174 |
"" |
| 175 |
}, |
| 176 |
if self.modifiers.contains(KeyModifiers::ALT) { |
| 177 |
"A-" |
| 178 |
} else { |
| 179 |
"" |
| 180 |
}, |
| 181 |
if self.modifiers.contains(KeyModifiers::CONTROL) { |
| 182 |
"C-" |
| 183 |
} else { |
| 184 |
"" |
| 185 |
}, |
| 186 |
))?; |
| 187 |
match self.code { |
| 188 |
KeyCode::Backspace => f.write_str(keys::BACKSPACE)?, |
| 189 |
KeyCode::Enter => f.write_str(keys::ENTER)?, |
| 190 |
KeyCode::Left => f.write_str(keys::LEFT)?, |
| 191 |
KeyCode::Right => f.write_str(keys::RIGHT)?, |
| 192 |
KeyCode::Up => f.write_str(keys::UP)?, |
| 193 |
KeyCode::Down => f.write_str(keys::DOWN)?, |
| 194 |
KeyCode::Home => f.write_str(keys::HOME)?, |
| 195 |
KeyCode::End => f.write_str(keys::END)?, |
| 196 |
KeyCode::PageUp => f.write_str(keys::PAGEUP)?, |
| 197 |
KeyCode::PageDown => f.write_str(keys::PAGEDOWN)?, |
| 198 |
KeyCode::Tab => f.write_str(keys::TAB)?, |
| 199 |
KeyCode::Delete => f.write_str(keys::DELETE)?, |
| 200 |
KeyCode::Insert => f.write_str(keys::INSERT)?, |
| 201 |
KeyCode::Null => f.write_str(keys::NULL)?, |
| 202 |
KeyCode::Esc => f.write_str(keys::ESC)?, |
| 203 |
KeyCode::Char(' ') => f.write_str(keys::SPACE)?, |
| 204 |
KeyCode::Char('-') => f.write_str(keys::MINUS)?, |
| 205 |
KeyCode::Char('<') => f.write_str(keys::LESS_THAN)?, |
| 206 |
KeyCode::Char('>') => f.write_str(keys::GREATER_THAN)?, |
| 207 |
KeyCode::F(i) => f.write_fmt(format_args!("F{}", i))?, |
| 208 |
KeyCode::Char(c) => f.write_fmt(format_args!("{}", c))?, |
| 209 |
KeyCode::CapsLock => f.write_str(keys::CAPS_LOCK)?, |
| 210 |
KeyCode::ScrollLock => f.write_str(keys::SCROLL_LOCK)?, |
| 211 |
KeyCode::NumLock => f.write_str(keys::NUM_LOCK)?, |
| 212 |
KeyCode::PrintScreen => f.write_str(keys::PRINT_SCREEN)?, |
| 213 |
KeyCode::Pause => f.write_str(keys::PAUSE)?, |
| 214 |
KeyCode::Menu => f.write_str(keys::MENU)?, |
| 215 |
KeyCode::KeypadBegin => f.write_str(keys::KEYPAD_BEGIN)?, |
| 216 |
KeyCode::Media(MediaKeyCode::Play) => f.write_str(keys::PLAY)?, |
| 217 |
KeyCode::Media(MediaKeyCode::Pause) => f.write_str(keys::PAUSE_MEDIA)?, |
| 218 |
KeyCode::Media(MediaKeyCode::PlayPause) => f.write_str(keys::PLAY_PAUSE)?, |
| 219 |
KeyCode::Media(MediaKeyCode::Stop) => f.write_str(keys::STOP)?, |
| 220 |
KeyCode::Media(MediaKeyCode::Reverse) => f.write_str(keys::REVERSE)?, |
| 221 |
KeyCode::Media(MediaKeyCode::FastForward) => f.write_str(keys::FAST_FORWARD)?, |
| 222 |
KeyCode::Media(MediaKeyCode::Rewind) => f.write_str(keys::REWIND)?, |
| 223 |
KeyCode::Media(MediaKeyCode::TrackNext) => f.write_str(keys::TRACK_NEXT)?, |
| 224 |
KeyCode::Media(MediaKeyCode::TrackPrevious) => f.write_str(keys::TRACK_PREVIOUS)?, |
| 225 |
KeyCode::Media(MediaKeyCode::Record) => f.write_str(keys::RECORD)?, |
| 226 |
KeyCode::Media(MediaKeyCode::LowerVolume) => f.write_str(keys::LOWER_VOLUME)?, |
| 227 |
KeyCode::Media(MediaKeyCode::RaiseVolume) => f.write_str(keys::RAISE_VOLUME)?, |
| 228 |
KeyCode::Media(MediaKeyCode::MuteVolume) => f.write_str(keys::MUTE_VOLUME)?, |
| 229 |
KeyCode::Modifier(ModifierKeyCode::LeftShift) => f.write_str(keys::LEFT_SHIFT)?, |
| 230 |
KeyCode::Modifier(ModifierKeyCode::LeftControl) => f.write_str(keys::LEFT_CONTROL)?, |
| 231 |
KeyCode::Modifier(ModifierKeyCode::LeftAlt) => f.write_str(keys::LEFT_ALT)?, |
| 232 |
KeyCode::Modifier(ModifierKeyCode::LeftSuper) => f.write_str(keys::LEFT_SUPER)?, |
| 233 |
KeyCode::Modifier(ModifierKeyCode::LeftHyper) => f.write_str(keys::LEFT_HYPER)?, |
| 234 |
KeyCode::Modifier(ModifierKeyCode::LeftMeta) => f.write_str(keys::LEFT_META)?, |
| 235 |
KeyCode::Modifier(ModifierKeyCode::RightShift) => f.write_str(keys::RIGHT_SHIFT)?, |
| 236 |
KeyCode::Modifier(ModifierKeyCode::RightControl) => f.write_str(keys::RIGHT_CONTROL)?, |
| 237 |
KeyCode::Modifier(ModifierKeyCode::RightAlt) => f.write_str(keys::RIGHT_ALT)?, |
| 238 |
KeyCode::Modifier(ModifierKeyCode::RightSuper) => f.write_str(keys::RIGHT_SUPER)?, |
| 239 |
KeyCode::Modifier(ModifierKeyCode::RightHyper) => f.write_str(keys::RIGHT_HYPER)?, |
| 240 |
KeyCode::Modifier(ModifierKeyCode::RightMeta) => f.write_str(keys::RIGHT_META)?, |
| 241 |
KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift) => { |
| 242 |
f.write_str(keys::ISO_LEVEL_3_SHIFT)? |
| 243 |
} |
| 244 |
KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift) => { |
| 245 |
f.write_str(keys::ISO_LEVEL_5_SHIFT)? |
| 246 |
} |
| 247 |
}; |
| 248 |
Ok(()) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
impl UnicodeWidthStr for KeyEvent { |
| 253 |
fn width(&self) -> usize { |
| 254 |
use helix_core::unicode::width::UnicodeWidthChar; |
| 255 |
let mut width = match self.code { |
| 256 |
KeyCode::Backspace => keys::BACKSPACE.len(), |
| 257 |
KeyCode::Enter => keys::ENTER.len(), |
| 258 |
KeyCode::Left => keys::LEFT.len(), |
| 259 |
KeyCode::Right => keys::RIGHT.len(), |
| 260 |
KeyCode::Up => keys::UP.len(), |
| 261 |
KeyCode::Down => keys::DOWN.len(), |
| 262 |
KeyCode::Home => keys::HOME.len(), |
| 263 |
KeyCode::End => keys::END.len(), |
| 264 |
KeyCode::PageUp => keys::PAGEUP.len(), |
| 265 |
KeyCode::PageDown => keys::PAGEDOWN.len(), |
| 266 |
KeyCode::Tab => keys::TAB.len(), |
| 267 |
KeyCode::Delete => keys::DELETE.len(), |
| 268 |
KeyCode::Insert => keys::INSERT.len(), |
| 269 |
KeyCode::Null => keys::NULL.len(), |
| 270 |
KeyCode::Esc => keys::ESC.len(), |
| 271 |
KeyCode::Char(' ') => keys::SPACE.len(), |
| 272 |
KeyCode::Char('-') => keys::MINUS.len(), |
| 273 |
KeyCode::F(1..=9) => 2, |
| 274 |
KeyCode::F(_) => 3, |
| 275 |
KeyCode::Char(c) => c.width().unwrap_or(0), |
| 276 |
KeyCode::CapsLock => keys::CAPS_LOCK.len(), |
| 277 |
KeyCode::ScrollLock => keys::SCROLL_LOCK.len(), |
| 278 |
KeyCode::NumLock => keys::NUM_LOCK.len(), |
| 279 |
KeyCode::PrintScreen => keys::PRINT_SCREEN.len(), |
| 280 |
KeyCode::Pause => keys::PAUSE.len(), |
| 281 |
KeyCode::Menu => keys::MENU.len(), |
| 282 |
KeyCode::KeypadBegin => keys::KEYPAD_BEGIN.len(), |
| 283 |
KeyCode::Media(MediaKeyCode::Play) => keys::PLAY.len(), |
| 284 |
KeyCode::Media(MediaKeyCode::Pause) => keys::PAUSE_MEDIA.len(), |
| 285 |
KeyCode::Media(MediaKeyCode::PlayPause) => keys::PLAY_PAUSE.len(), |
| 286 |
KeyCode::Media(MediaKeyCode::Stop) => keys::STOP.len(), |
| 287 |
KeyCode::Media(MediaKeyCode::Reverse) => keys::REVERSE.len(), |
| 288 |
KeyCode::Media(MediaKeyCode::FastForward) => keys::FAST_FORWARD.len(), |
| 289 |
KeyCode::Media(MediaKeyCode::Rewind) => keys::REWIND.len(), |
| 290 |
KeyCode::Media(MediaKeyCode::TrackNext) => keys::TRACK_NEXT.len(), |
| 291 |
KeyCode::Media(MediaKeyCode::TrackPrevious) => keys::TRACK_PREVIOUS.len(), |
| 292 |
KeyCode::Media(MediaKeyCode::Record) => keys::RECORD.len(), |
| 293 |
KeyCode::Media(MediaKeyCode::LowerVolume) => keys::LOWER_VOLUME.len(), |
| 294 |
KeyCode::Media(MediaKeyCode::RaiseVolume) => keys::RAISE_VOLUME.len(), |
| 295 |
KeyCode::Media(MediaKeyCode::MuteVolume) => keys::MUTE_VOLUME.len(), |
| 296 |
KeyCode::Modifier(ModifierKeyCode::LeftShift) => keys::LEFT_SHIFT.len(), |
| 297 |
KeyCode::Modifier(ModifierKeyCode::LeftControl) => keys::LEFT_CONTROL.len(), |
| 298 |
KeyCode::Modifier(ModifierKeyCode::LeftAlt) => keys::LEFT_ALT.len(), |
| 299 |
KeyCode::Modifier(ModifierKeyCode::LeftSuper) => keys::LEFT_SUPER.len(), |
| 300 |
KeyCode::Modifier(ModifierKeyCode::LeftHyper) => keys::LEFT_HYPER.len(), |
| 301 |
KeyCode::Modifier(ModifierKeyCode::LeftMeta) => keys::LEFT_META.len(), |
| 302 |
KeyCode::Modifier(ModifierKeyCode::RightShift) => keys::RIGHT_SHIFT.len(), |
| 303 |
KeyCode::Modifier(ModifierKeyCode::RightControl) => keys::RIGHT_CONTROL.len(), |
| 304 |
KeyCode::Modifier(ModifierKeyCode::RightAlt) => keys::RIGHT_ALT.len(), |
| 305 |
KeyCode::Modifier(ModifierKeyCode::RightSuper) => keys::RIGHT_SUPER.len(), |
| 306 |
KeyCode::Modifier(ModifierKeyCode::RightHyper) => keys::RIGHT_HYPER.len(), |
| 307 |
KeyCode::Modifier(ModifierKeyCode::RightMeta) => keys::RIGHT_META.len(), |
| 308 |
KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift) => keys::ISO_LEVEL_3_SHIFT.len(), |
| 309 |
KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift) => keys::ISO_LEVEL_5_SHIFT.len(), |
| 310 |
}; |
| 311 |
if self.modifiers.contains(KeyModifiers::SHIFT) { |
| 312 |
width += 2; |
| 313 |
} |
| 314 |
if self.modifiers.contains(KeyModifiers::ALT) { |
| 315 |
width += 2; |
| 316 |
} |
| 317 |
if self.modifiers.contains(KeyModifiers::CONTROL) { |
| 318 |
width += 2; |
| 319 |
} |
| 320 |
if self.modifiers.contains(KeyModifiers::SUPER) { |
| 321 |
|
| 322 |
width += 5; |
| 323 |
} |
| 324 |
width |
| 325 |
} |
| 326 |
|
| 327 |
fn width_cjk(&self) -> usize { |
| 328 |
self.width() |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
impl std::str::FromStr for KeyEvent { |
| 333 |
type Err = Error; |
| 334 |
|
| 335 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 336 |
let mut tokens: Vec<_> = s.split('-').collect(); |
| 337 |
let mut code = match tokens.pop().ok_or_else(|| anyhow!("Missing key code"))? { |
| 338 |
keys::BACKSPACE => KeyCode::Backspace, |
| 339 |
keys::ENTER => KeyCode::Enter, |
| 340 |
keys::LEFT => KeyCode::Left, |
| 341 |
keys::RIGHT => KeyCode::Right, |
| 342 |
keys::UP => KeyCode::Up, |
| 343 |
keys::DOWN => KeyCode::Down, |
| 344 |
keys::HOME => KeyCode::Home, |
| 345 |
keys::END => KeyCode::End, |
| 346 |
keys::PAGEUP => KeyCode::PageUp, |
| 347 |
keys::PAGEDOWN => KeyCode::PageDown, |
| 348 |
keys::TAB => KeyCode::Tab, |
| 349 |
keys::DELETE => KeyCode::Delete, |
| 350 |
keys::INSERT => KeyCode::Insert, |
| 351 |
keys::NULL => KeyCode::Null, |
| 352 |
keys::ESC => KeyCode::Esc, |
| 353 |
keys::SPACE => KeyCode::Char(' '), |
| 354 |
keys::MINUS => KeyCode::Char('-'), |
| 355 |
keys::LESS_THAN => KeyCode::Char('<'), |
| 356 |
keys::GREATER_THAN => KeyCode::Char('>'), |
| 357 |
keys::CAPS_LOCK => KeyCode::CapsLock, |
| 358 |
keys::SCROLL_LOCK => KeyCode::ScrollLock, |
| 359 |
keys::NUM_LOCK => KeyCode::NumLock, |
| 360 |
keys::PRINT_SCREEN => KeyCode::PrintScreen, |
| 361 |
keys::PAUSE => KeyCode::Pause, |
| 362 |
keys::MENU => KeyCode::Menu, |
| 363 |
keys::KEYPAD_BEGIN => KeyCode::KeypadBegin, |
| 364 |
keys::PLAY => KeyCode::Media(MediaKeyCode::Play), |
| 365 |
keys::PAUSE_MEDIA => KeyCode::Media(MediaKeyCode::Pause), |
| 366 |
keys::PLAY_PAUSE => KeyCode::Media(MediaKeyCode::PlayPause), |
| 367 |
keys::STOP => KeyCode::Media(MediaKeyCode::Stop), |
| 368 |
keys::REVERSE => KeyCode::Media(MediaKeyCode::Reverse), |
| 369 |
keys::FAST_FORWARD => KeyCode::Media(MediaKeyCode::FastForward), |
| 370 |
keys::REWIND => KeyCode::Media(MediaKeyCode::Rewind), |
| 371 |
keys::TRACK_NEXT => KeyCode::Media(MediaKeyCode::TrackNext), |
| 372 |
keys::TRACK_PREVIOUS => KeyCode::Media(MediaKeyCode::TrackPrevious), |
| 373 |
keys::RECORD => KeyCode::Media(MediaKeyCode::Record), |
| 374 |
keys::LOWER_VOLUME => KeyCode::Media(MediaKeyCode::LowerVolume), |
| 375 |
keys::RAISE_VOLUME => KeyCode::Media(MediaKeyCode::RaiseVolume), |
| 376 |
keys::MUTE_VOLUME => KeyCode::Media(MediaKeyCode::MuteVolume), |
| 377 |
keys::LEFT_SHIFT => KeyCode::Modifier(ModifierKeyCode::LeftShift), |
| 378 |
keys::LEFT_CONTROL => KeyCode::Modifier(ModifierKeyCode::LeftControl), |
| 379 |
keys::LEFT_ALT => KeyCode::Modifier(ModifierKeyCode::LeftAlt), |
| 380 |
keys::LEFT_SUPER => KeyCode::Modifier(ModifierKeyCode::LeftSuper), |
| 381 |
keys::LEFT_HYPER => KeyCode::Modifier(ModifierKeyCode::LeftHyper), |
| 382 |
keys::LEFT_META => KeyCode::Modifier(ModifierKeyCode::LeftMeta), |
| 383 |
keys::RIGHT_SHIFT => KeyCode::Modifier(ModifierKeyCode::RightShift), |
| 384 |
keys::RIGHT_CONTROL => KeyCode::Modifier(ModifierKeyCode::RightControl), |
| 385 |
keys::RIGHT_ALT => KeyCode::Modifier(ModifierKeyCode::RightAlt), |
| 386 |
keys::RIGHT_SUPER => KeyCode::Modifier(ModifierKeyCode::RightSuper), |
| 387 |
keys::RIGHT_HYPER => KeyCode::Modifier(ModifierKeyCode::RightHyper), |
| 388 |
keys::RIGHT_META => KeyCode::Modifier(ModifierKeyCode::RightMeta), |
| 389 |
keys::ISO_LEVEL_3_SHIFT => KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift), |
| 390 |
keys::ISO_LEVEL_5_SHIFT => KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift), |
| 391 |
single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()), |
| 392 |
function if function.len() > 1 && function.starts_with('F') => { |
| 393 |
let function: String = function.chars().skip(1).collect(); |
| 394 |
let function = str::parse::<u8>(&function)?; |
| 395 |
(function > 0 && function < 25) |
| 396 |
.then_some(KeyCode::F(function)) |
| 397 |
.ok_or_else(|| anyhow!("Invalid function key '{}'", function))? |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
_ if s.ends_with('-') && tokens.last().is_some_and(|t| t.is_empty()) => { |
| 403 |
if s == "-" { |
| 404 |
return Ok(KeyEvent { |
| 405 |
code: KeyCode::Char('-'), |
| 406 |
modifiers: KeyModifiers::empty(), |
| 407 |
}); |
| 408 |
} else { |
| 409 |
let suggestion = format!("{}-{}", s.trim_end_matches('-'), keys::MINUS); |
| 410 |
return Err(anyhow!( |
| 411 |
"Key '-' cannot be used with modifiers, use '{}' instead", |
| 412 |
suggestion |
| 413 |
)); |
| 414 |
} |
| 415 |
} |
| 416 |
invalid => return Err(anyhow!("Invalid key code '{}'", invalid)), |
| 417 |
}; |
| 418 |
|
| 419 |
let mut modifiers = KeyModifiers::empty(); |
| 420 |
for token in tokens { |
| 421 |
let flag = match token { |
| 422 |
"S" => KeyModifiers::SHIFT, |
| 423 |
"A" => KeyModifiers::ALT, |
| 424 |
"C" => KeyModifiers::CONTROL, |
| 425 |
"Meta" | "Cmd" | "Win" => KeyModifiers::SUPER, |
| 426 |
_ => return Err(anyhow!("Invalid key modifier '{}-'", token)), |
| 427 |
}; |
| 428 |
|
| 429 |
if modifiers.contains(flag) { |
| 430 |
return Err(anyhow!("Repeated key modifier '{}-'", token)); |
| 431 |
} |
| 432 |
modifiers.insert(flag); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
match code { |
| 438 |
KeyCode::Char(ch) |
| 439 |
if ch.is_ascii_lowercase() && modifiers.contains(KeyModifiers::SHIFT) => |
| 440 |
{ |
| 441 |
code = KeyCode::Char(ch.to_ascii_uppercase()); |
| 442 |
modifiers.remove(KeyModifiers::SHIFT); |
| 443 |
} |
| 444 |
_ => (), |
| 445 |
} |
| 446 |
|
| 447 |
Ok(KeyEvent { code, modifiers }) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
impl<'de> Deserialize<'de> for KeyEvent { |
| 452 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 453 |
where |
| 454 |
D: Deserializer<'de>, |
| 455 |
{ |
| 456 |
let s = String::deserialize(deserializer)?; |
| 457 |
s.parse().map_err(de::Error::custom) |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
#[cfg(feature = "term")] |
| 462 |
impl From<termina::event::Event> for Event { |
| 463 |
fn from(event: termina::event::Event) -> Self { |
| 464 |
match event { |
| 465 |
termina::event::Event::Key(key) => Self::Key(key.into()), |
| 466 |
termina::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()), |
| 467 |
termina::event::Event::WindowResized(termina::WindowSize { rows, cols, .. }) => { |
| 468 |
Self::Resize(cols, rows) |
| 469 |
} |
| 470 |
termina::event::Event::FocusIn => Self::FocusGained, |
| 471 |
termina::event::Event::FocusOut => Self::FocusLost, |
| 472 |
termina::event::Event::Paste(s) => Self::Paste(s), |
| 473 |
_ => unreachable!(), |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
#[cfg(feature = "term")] |
| 479 |
impl From<termina::event::MouseEvent> for MouseEvent { |
| 480 |
fn from( |
| 481 |
termina::event::MouseEvent { |
| 482 |
kind, |
| 483 |
column, |
| 484 |
row, |
| 485 |
modifiers, |
| 486 |
}: termina::event::MouseEvent, |
| 487 |
) -> Self { |
| 488 |
Self { |
| 489 |
kind: kind.into(), |
| 490 |
column, |
| 491 |
row, |
| 492 |
modifiers: modifiers.into(), |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
#[cfg(feature = "term")] |
| 498 |
impl From<termina::event::MouseEventKind> for MouseEventKind { |
| 499 |
fn from(kind: termina::event::MouseEventKind) -> Self { |
| 500 |
match kind { |
| 501 |
termina::event::MouseEventKind::Down(button) => Self::Down(button.into()), |
| 502 |
termina::event::MouseEventKind::Up(button) => Self::Up(button.into()), |
| 503 |
termina::event::MouseEventKind::Drag(button) => Self::Drag(button.into()), |
| 504 |
termina::event::MouseEventKind::Moved => Self::Moved, |
| 505 |
termina::event::MouseEventKind::ScrollDown => Self::ScrollDown, |
| 506 |
termina::event::MouseEventKind::ScrollUp => Self::ScrollUp, |
| 507 |
termina::event::MouseEventKind::ScrollLeft => Self::ScrollLeft, |
| 508 |
termina::event::MouseEventKind::ScrollRight => Self::ScrollRight, |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
#[cfg(feature = "term")] |
| 514 |
impl From<termina::event::MouseButton> for MouseButton { |
| 515 |
fn from(button: termina::event::MouseButton) -> Self { |
| 516 |
match button { |
| 517 |
termina::event::MouseButton::Left => MouseButton::Left, |
| 518 |
termina::event::MouseButton::Right => MouseButton::Right, |
| 519 |
termina::event::MouseButton::Middle => MouseButton::Middle, |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
#[cfg(feature = "term")] |
| 525 |
impl From<termina::event::KeyEvent> for KeyEvent { |
| 526 |
fn from( |
| 527 |
termina::event::KeyEvent { |
| 528 |
code, modifiers, .. |
| 529 |
}: termina::event::KeyEvent, |
| 530 |
) -> Self { |
| 531 |
if code == termina::event::KeyCode::BackTab { |
| 532 |
|
| 533 |
let mut modifiers: KeyModifiers = modifiers.into(); |
| 534 |
modifiers.insert(KeyModifiers::SHIFT); |
| 535 |
Self { |
| 536 |
code: KeyCode::Tab, |
| 537 |
modifiers, |
| 538 |
} |
| 539 |
} else { |
| 540 |
Self { |
| 541 |
code: code.into(), |
| 542 |
modifiers: modifiers.into(), |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
#[cfg(feature = "term")] |
| 549 |
impl From<KeyEvent> for termina::event::KeyEvent { |
| 550 |
fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self { |
| 551 |
if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) { |
| 552 |
|
| 553 |
let mut modifiers = modifiers; |
| 554 |
modifiers.remove(KeyModifiers::SHIFT); |
| 555 |
termina::event::KeyEvent { |
| 556 |
code: termina::event::KeyCode::BackTab, |
| 557 |
modifiers: modifiers.into(), |
| 558 |
kind: termina::event::KeyEventKind::Press, |
| 559 |
state: termina::event::KeyEventState::NONE, |
| 560 |
} |
| 561 |
} else { |
| 562 |
termina::event::KeyEvent { |
| 563 |
code: code.into(), |
| 564 |
modifiers: modifiers.into(), |
| 565 |
kind: termina::event::KeyEventKind::Press, |
| 566 |
state: termina::event::KeyEventState::NONE, |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
#[cfg(all(feature = "term", windows))] |
| 573 |
impl From<crossterm::event::Event> for Event { |
| 574 |
fn from(event: crossterm::event::Event) -> Self { |
| 575 |
match event { |
| 576 |
crossterm::event::Event::Key(key) => Self::Key(key.into()), |
| 577 |
crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()), |
| 578 |
crossterm::event::Event::Resize(w, h) => Self::Resize(w, h), |
| 579 |
crossterm::event::Event::FocusGained => Self::FocusGained, |
| 580 |
crossterm::event::Event::FocusLost => Self::FocusLost, |
| 581 |
crossterm::event::Event::Paste(s) => Self::Paste(s), |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
#[cfg(all(feature = "term", windows))] |
| 587 |
impl From<crossterm::event::MouseEvent> for MouseEvent { |
| 588 |
fn from( |
| 589 |
crossterm::event::MouseEvent { |
| 590 |
kind, |
| 591 |
column, |
| 592 |
row, |
| 593 |
modifiers, |
| 594 |
}: crossterm::event::MouseEvent, |
| 595 |
) -> Self { |
| 596 |
Self { |
| 597 |
kind: kind.into(), |
| 598 |
column, |
| 599 |
row, |
| 600 |
modifiers: modifiers.into(), |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
#[cfg(all(feature = "term", windows))] |
| 606 |
impl From<crossterm::event::MouseEventKind> for MouseEventKind { |
| 607 |
fn from(kind: crossterm::event::MouseEventKind) -> Self { |
| 608 |
match kind { |
| 609 |
crossterm::event::MouseEventKind::Down(button) => Self::Down(button.into()), |
| 610 |
crossterm::event::MouseEventKind::Up(button) => Self::Up(button.into()), |
| 611 |
crossterm::event::MouseEventKind::Drag(button) => Self::Drag(button.into()), |
| 612 |
crossterm::event::MouseEventKind::Moved => Self::Moved, |
| 613 |
crossterm::event::MouseEventKind::ScrollDown => Self::ScrollDown, |
| 614 |
crossterm::event::MouseEventKind::ScrollUp => Self::ScrollUp, |
| 615 |
crossterm::event::MouseEventKind::ScrollLeft => Self::ScrollLeft, |
| 616 |
crossterm::event::MouseEventKind::ScrollRight => Self::ScrollRight, |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
#[cfg(all(feature = "term", windows))] |
| 622 |
impl From<crossterm::event::MouseButton> for MouseButton { |
| 623 |
fn from(button: crossterm::event::MouseButton) -> Self { |
| 624 |
match button { |
| 625 |
crossterm::event::MouseButton::Left => MouseButton::Left, |
| 626 |
crossterm::event::MouseButton::Right => MouseButton::Right, |
| 627 |
crossterm::event::MouseButton::Middle => MouseButton::Middle, |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
#[cfg(all(feature = "term", windows))] |
| 633 |
impl From<crossterm::event::KeyEvent> for KeyEvent { |
| 634 |
fn from( |
| 635 |
crossterm::event::KeyEvent { |
| 636 |
code, modifiers, .. |
| 637 |
}: crossterm::event::KeyEvent, |
| 638 |
) -> Self { |
| 639 |
if code == crossterm::event::KeyCode::BackTab { |
| 640 |
|
| 641 |
let mut modifiers: KeyModifiers = modifiers.into(); |
| 642 |
modifiers.insert(KeyModifiers::SHIFT); |
| 643 |
Self { |
| 644 |
code: KeyCode::Tab, |
| 645 |
modifiers, |
| 646 |
} |
| 647 |
} else { |
| 648 |
Self { |
| 649 |
code: code.into(), |
| 650 |
modifiers: modifiers.into(), |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
#[cfg(all(feature = "term", windows))] |
| 657 |
impl From<KeyEvent> for crossterm::event::KeyEvent { |
| 658 |
fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self { |
| 659 |
if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) { |
| 660 |
|
| 661 |
let mut modifiers = modifiers; |
| 662 |
modifiers.remove(KeyModifiers::SHIFT); |
| 663 |
crossterm::event::KeyEvent { |
| 664 |
code: crossterm::event::KeyCode::BackTab, |
| 665 |
modifiers: modifiers.into(), |
| 666 |
kind: crossterm::event::KeyEventKind::Press, |
| 667 |
state: crossterm::event::KeyEventState::NONE, |
| 668 |
} |
| 669 |
} else { |
| 670 |
crossterm::event::KeyEvent { |
| 671 |
code: code.into(), |
| 672 |
modifiers: modifiers.into(), |
| 673 |
kind: crossterm::event::KeyEventKind::Press, |
| 674 |
state: crossterm::event::KeyEventState::NONE, |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
pub fn parse_macro(keys_str: &str) -> anyhow::Result<Vec<KeyEvent>> { |
| 680 |
use anyhow::Context; |
| 681 |
let mut keys_res: anyhow::Result<_> = Ok(Vec::new()); |
| 682 |
let mut i = 0; |
| 683 |
while let Ok(keys) = &mut keys_res { |
| 684 |
if i >= keys_str.len() { |
| 685 |
break; |
| 686 |
} |
| 687 |
if !keys_str.is_char_boundary(i) { |
| 688 |
i += 1; |
| 689 |
continue; |
| 690 |
} |
| 691 |
|
| 692 |
let s = &keys_str[i..]; |
| 693 |
let mut end_i = 1; |
| 694 |
while !s.is_char_boundary(end_i) { |
| 695 |
end_i += 1; |
| 696 |
} |
| 697 |
let c = &s[..end_i]; |
| 698 |
if c == ">" { |
| 699 |
keys_res = Err(anyhow!("Unmatched '>'")); |
| 700 |
} else if c != "<" { |
| 701 |
keys.push(if c == "-" { keys::MINUS } else { c }); |
| 702 |
i += end_i; |
| 703 |
} else { |
| 704 |
match s.find('>').context("'>' expected") { |
| 705 |
Ok(end_i) => { |
| 706 |
keys.push(&s[1..end_i]); |
| 707 |
i += end_i + 1; |
| 708 |
} |
| 709 |
Err(err) => keys_res = Err(err), |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
keys_res.and_then(|keys| keys.into_iter().map(str::parse).collect()) |
| 714 |
} |
| 715 |
|
| 716 |
#[cfg(test)] |
| 717 |
mod test { |
| 718 |
use super::*; |
| 719 |
|
| 720 |
#[test] |
| 721 |
fn parsing_unmodified_keys() { |
| 722 |
assert_eq!( |
| 723 |
str::parse::<KeyEvent>("backspace").unwrap(), |
| 724 |
KeyEvent { |
| 725 |
code: KeyCode::Backspace, |
| 726 |
modifiers: KeyModifiers::NONE |
| 727 |
} |
| 728 |
); |
| 729 |
|
| 730 |
assert_eq!( |
| 731 |
str::parse::<KeyEvent>("left").unwrap(), |
| 732 |
KeyEvent { |
| 733 |
code: KeyCode::Left, |
| 734 |
modifiers: KeyModifiers::NONE |
| 735 |
} |
| 736 |
); |
| 737 |
|
| 738 |
assert_eq!( |
| 739 |
str::parse::<KeyEvent>(",").unwrap(), |
| 740 |
KeyEvent { |
| 741 |
code: KeyCode::Char(','), |
| 742 |
modifiers: KeyModifiers::NONE |
| 743 |
} |
| 744 |
); |
| 745 |
|
| 746 |
assert_eq!( |
| 747 |
str::parse::<KeyEvent>("w").unwrap(), |
| 748 |
KeyEvent { |
| 749 |
code: KeyCode::Char('w'), |
| 750 |
modifiers: KeyModifiers::NONE |
| 751 |
} |
| 752 |
); |
| 753 |
|
| 754 |
assert_eq!( |
| 755 |
str::parse::<KeyEvent>("F12").unwrap(), |
| 756 |
KeyEvent { |
| 757 |
code: KeyCode::F(12), |
| 758 |
modifiers: KeyModifiers::NONE |
| 759 |
} |
| 760 |
); |
| 761 |
|
| 762 |
assert_eq!( |
| 763 |
str::parse::<KeyEvent>("%").unwrap(), |
| 764 |
KeyEvent { |
| 765 |
code: KeyCode::Char('%'), |
| 766 |
modifiers: KeyModifiers::NONE |
| 767 |
} |
| 768 |
); |
| 769 |
|
| 770 |
assert_eq!( |
| 771 |
str::parse::<KeyEvent>(";").unwrap(), |
| 772 |
KeyEvent { |
| 773 |
code: KeyCode::Char(';'), |
| 774 |
modifiers: KeyModifiers::NONE |
| 775 |
} |
| 776 |
); |
| 777 |
|
| 778 |
assert_eq!( |
| 779 |
str::parse::<KeyEvent>(">").unwrap(), |
| 780 |
KeyEvent { |
| 781 |
code: KeyCode::Char('>'), |
| 782 |
modifiers: KeyModifiers::NONE |
| 783 |
} |
| 784 |
); |
| 785 |
|
| 786 |
assert_eq!( |
| 787 |
str::parse::<KeyEvent>("<").unwrap(), |
| 788 |
KeyEvent { |
| 789 |
code: KeyCode::Char('<'), |
| 790 |
modifiers: KeyModifiers::NONE |
| 791 |
} |
| 792 |
); |
| 793 |
|
| 794 |
assert_eq!( |
| 795 |
str::parse::<KeyEvent>("+").unwrap(), |
| 796 |
KeyEvent { |
| 797 |
code: KeyCode::Char('+'), |
| 798 |
modifiers: KeyModifiers::NONE |
| 799 |
} |
| 800 |
); |
| 801 |
assert_eq!( |
| 802 |
str::parse::<KeyEvent>("-").unwrap(), |
| 803 |
KeyEvent { |
| 804 |
code: KeyCode::Char('-'), |
| 805 |
modifiers: KeyModifiers::NONE, |
| 806 |
} |
| 807 |
); |
| 808 |
} |
| 809 |
|
| 810 |
#[test] |
| 811 |
fn parsing_modified_keys() { |
| 812 |
assert_eq!( |
| 813 |
str::parse::<KeyEvent>("S-minus").unwrap(), |
| 814 |
KeyEvent { |
| 815 |
code: KeyCode::Char('-'), |
| 816 |
modifiers: KeyModifiers::SHIFT |
| 817 |
} |
| 818 |
); |
| 819 |
|
| 820 |
assert_eq!( |
| 821 |
str::parse::<KeyEvent>("C-A-S-F12").unwrap(), |
| 822 |
KeyEvent { |
| 823 |
code: KeyCode::F(12), |
| 824 |
modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL | KeyModifiers::ALT |
| 825 |
} |
| 826 |
); |
| 827 |
|
| 828 |
assert_eq!( |
| 829 |
str::parse::<KeyEvent>("S-C-2").unwrap(), |
| 830 |
KeyEvent { |
| 831 |
code: KeyCode::Char('2'), |
| 832 |
modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL |
| 833 |
} |
| 834 |
); |
| 835 |
|
| 836 |
assert_eq!( |
| 837 |
str::parse::<KeyEvent>("A-C-+").unwrap(), |
| 838 |
KeyEvent { |
| 839 |
code: KeyCode::Char('+'), |
| 840 |
modifiers: KeyModifiers::ALT | KeyModifiers::CONTROL |
| 841 |
} |
| 842 |
); |
| 843 |
|
| 844 |
assert_eq!( |
| 845 |
str::parse::<KeyEvent>("C-S-r").unwrap(), |
| 846 |
str::parse::<KeyEvent>("C-R").unwrap(), |
| 847 |
); |
| 848 |
|
| 849 |
assert_eq!( |
| 850 |
str::parse::<KeyEvent>("S-w").unwrap(), |
| 851 |
KeyEvent { |
| 852 |
code: KeyCode::Char('W'), |
| 853 |
modifiers: KeyModifiers::NONE |
| 854 |
} |
| 855 |
); |
| 856 |
|
| 857 |
assert_eq!( |
| 858 |
str::parse::<KeyEvent>("Meta-c").unwrap(), |
| 859 |
KeyEvent { |
| 860 |
code: KeyCode::Char('c'), |
| 861 |
modifiers: KeyModifiers::SUPER |
| 862 |
} |
| 863 |
); |
| 864 |
assert_eq!( |
| 865 |
str::parse::<KeyEvent>("Win-s").unwrap(), |
| 866 |
KeyEvent { |
| 867 |
code: KeyCode::Char('s'), |
| 868 |
modifiers: KeyModifiers::SUPER |
| 869 |
} |
| 870 |
); |
| 871 |
assert_eq!( |
| 872 |
str::parse::<KeyEvent>("Cmd-d").unwrap(), |
| 873 |
KeyEvent { |
| 874 |
code: KeyCode::Char('d'), |
| 875 |
modifiers: KeyModifiers::SUPER |
| 876 |
} |
| 877 |
); |
| 878 |
} |
| 879 |
|
| 880 |
#[test] |
| 881 |
fn parsing_nonsensical_keys_fails() { |
| 882 |
assert!(str::parse::<KeyEvent>("F25").is_err()); |
| 883 |
assert!(str::parse::<KeyEvent>("F0").is_err()); |
| 884 |
assert!(str::parse::<KeyEvent>("aaa").is_err()); |
| 885 |
assert!(str::parse::<KeyEvent>("S-S-a").is_err()); |
| 886 |
assert!(str::parse::<KeyEvent>("C-A-S-C-1").is_err()); |
| 887 |
assert!(str::parse::<KeyEvent>("FU").is_err()); |
| 888 |
assert!(str::parse::<KeyEvent>("123").is_err()); |
| 889 |
assert!(str::parse::<KeyEvent>("S--").is_err()); |
| 890 |
assert!(str::parse::<KeyEvent>("S-").is_err()); |
| 891 |
assert!(str::parse::<KeyEvent>("S-percent").is_err()); |
| 892 |
} |
| 893 |
|
| 894 |
#[test] |
| 895 |
fn parsing_unsupported_named_keys() { |
| 896 |
assert!(str::parse::<KeyEvent>("plus").is_err()); |
| 897 |
assert!(str::parse::<KeyEvent>("percent").is_err()); |
| 898 |
assert!(str::parse::<KeyEvent>("semicolon").is_err()); |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn parsing_valid_macros() { |
| 903 |
assert_eq!( |
| 904 |
parse_macro("xdo").ok(), |
| 905 |
Some(vec![ |
| 906 |
KeyEvent { |
| 907 |
code: KeyCode::Char('x'), |
| 908 |
modifiers: KeyModifiers::NONE, |
| 909 |
}, |
| 910 |
KeyEvent { |
| 911 |
code: KeyCode::Char('d'), |
| 912 |
modifiers: KeyModifiers::NONE, |
| 913 |
}, |
| 914 |
KeyEvent { |
| 915 |
code: KeyCode::Char('o'), |
| 916 |
modifiers: KeyModifiers::NONE, |
| 917 |
}, |
| 918 |
]), |
| 919 |
); |
| 920 |
|
| 921 |
assert_eq!( |
| 922 |
parse_macro("<C-w>v<C-w>h<C-o>xx<A-s>").ok(), |
| 923 |
Some(vec![ |
| 924 |
KeyEvent { |
| 925 |
code: KeyCode::Char('w'), |
| 926 |
modifiers: KeyModifiers::CONTROL, |
| 927 |
}, |
| 928 |
KeyEvent { |
| 929 |
code: KeyCode::Char('v'), |
| 930 |
modifiers: KeyModifiers::NONE, |
| 931 |
}, |
| 932 |
KeyEvent { |
| 933 |
code: KeyCode::Char('w'), |
| 934 |
modifiers: KeyModifiers::CONTROL, |
| 935 |
}, |
| 936 |
KeyEvent { |
| 937 |
code: KeyCode::Char('h'), |
| 938 |
modifiers: KeyModifiers::NONE, |
| 939 |
}, |
| 940 |
KeyEvent { |
| 941 |
code: KeyCode::Char('o'), |
| 942 |
modifiers: KeyModifiers::CONTROL, |
| 943 |
}, |
| 944 |
KeyEvent { |
| 945 |
code: KeyCode::Char('x'), |
| 946 |
modifiers: KeyModifiers::NONE, |
| 947 |
}, |
| 948 |
KeyEvent { |
| 949 |
code: KeyCode::Char('x'), |
| 950 |
modifiers: KeyModifiers::NONE, |
| 951 |
}, |
| 952 |
KeyEvent { |
| 953 |
code: KeyCode::Char('s'), |
| 954 |
modifiers: KeyModifiers::ALT, |
| 955 |
}, |
| 956 |
]) |
| 957 |
); |
| 958 |
|
| 959 |
assert_eq!( |
| 960 |
parse_macro(":o foo.bar<ret>").ok(), |
| 961 |
Some(vec![ |
| 962 |
KeyEvent { |
| 963 |
code: KeyCode::Char(':'), |
| 964 |
modifiers: KeyModifiers::NONE, |
| 965 |
}, |
| 966 |
KeyEvent { |
| 967 |
code: KeyCode::Char('o'), |
| 968 |
modifiers: KeyModifiers::NONE, |
| 969 |
}, |
| 970 |
KeyEvent { |
| 971 |
code: KeyCode::Char(' '), |
| 972 |
modifiers: KeyModifiers::NONE, |
| 973 |
}, |
| 974 |
KeyEvent { |
| 975 |
code: KeyCode::Char('f'), |
| 976 |
modifiers: KeyModifiers::NONE, |
| 977 |
}, |
| 978 |
KeyEvent { |
| 979 |
code: KeyCode::Char('o'), |
| 980 |
modifiers: KeyModifiers::NONE, |
| 981 |
}, |
| 982 |
KeyEvent { |
| 983 |
code: KeyCode::Char('o'), |
| 984 |
modifiers: KeyModifiers::NONE, |
| 985 |
}, |
| 986 |
KeyEvent { |
| 987 |
code: KeyCode::Char('.'), |
| 988 |
modifiers: KeyModifiers::NONE, |
| 989 |
}, |
| 990 |
KeyEvent { |
| 991 |
code: KeyCode::Char('b'), |
| 992 |
modifiers: KeyModifiers::NONE, |
| 993 |
}, |
| 994 |
KeyEvent { |
| 995 |
code: KeyCode::Char('a'), |
| 996 |
modifiers: KeyModifiers::NONE, |
| 997 |
}, |
| 998 |
KeyEvent { |
| 999 |
code: KeyCode::Char('r'), |
| 1000 |
modifiers: KeyModifiers::NONE, |
| 1001 |
}, |
| 1002 |
KeyEvent { |
| 1003 |
code: KeyCode::Enter, |
| 1004 |
modifiers: KeyModifiers::NONE, |
| 1005 |
}, |
| 1006 |
]) |
| 1007 |
); |
| 1008 |
|
| 1009 |
assert_eq!( |
| 1010 |
parse_macro(":w aa-bb.txt<ret>").ok(), |
| 1011 |
Some(vec![ |
| 1012 |
KeyEvent { |
| 1013 |
code: KeyCode::Char(':'), |
| 1014 |
modifiers: KeyModifiers::NONE, |
| 1015 |
}, |
| 1016 |
KeyEvent { |
| 1017 |
code: KeyCode::Char('w'), |
| 1018 |
modifiers: KeyModifiers::NONE, |
| 1019 |
}, |
| 1020 |
KeyEvent { |
| 1021 |
code: KeyCode::Char(' '), |
| 1022 |
modifiers: KeyModifiers::NONE, |
| 1023 |
}, |
| 1024 |
KeyEvent { |
| 1025 |
code: KeyCode::Char('a'), |
| 1026 |
modifiers: KeyModifiers::NONE, |
| 1027 |
}, |
| 1028 |
KeyEvent { |
| 1029 |
code: KeyCode::Char('a'), |
| 1030 |
modifiers: KeyModifiers::NONE, |
| 1031 |
}, |
| 1032 |
KeyEvent { |
| 1033 |
code: KeyCode::Char('-'), |
| 1034 |
modifiers: KeyModifiers::NONE, |
| 1035 |
}, |
| 1036 |
KeyEvent { |
| 1037 |
code: KeyCode::Char('b'), |
| 1038 |
modifiers: KeyModifiers::NONE, |
| 1039 |
}, |
| 1040 |
KeyEvent { |
| 1041 |
code: KeyCode::Char('b'), |
| 1042 |
modifiers: KeyModifiers::NONE, |
| 1043 |
}, |
| 1044 |
KeyEvent { |
| 1045 |
code: KeyCode::Char('.'), |
| 1046 |
modifiers: KeyModifiers::NONE, |
| 1047 |
}, |
| 1048 |
KeyEvent { |
| 1049 |
code: KeyCode::Char('t'), |
| 1050 |
modifiers: KeyModifiers::NONE, |
| 1051 |
}, |
| 1052 |
KeyEvent { |
| 1053 |
code: KeyCode::Char('x'), |
| 1054 |
modifiers: KeyModifiers::NONE, |
| 1055 |
}, |
| 1056 |
KeyEvent { |
| 1057 |
code: KeyCode::Char('t'), |
| 1058 |
modifiers: KeyModifiers::NONE, |
| 1059 |
}, |
| 1060 |
KeyEvent { |
| 1061 |
code: KeyCode::Enter, |
| 1062 |
modifiers: KeyModifiers::NONE, |
| 1063 |
}, |
| 1064 |
]) |
| 1065 |
); |
| 1066 |
} |
| 1067 |
|
| 1068 |
#[test] |
| 1069 |
fn parsing_invalid_macros_fails() { |
| 1070 |
assert!(parse_macro("abc<C-").is_err()); |
| 1071 |
assert!(parse_macro("abc>123").is_err()); |
| 1072 |
assert!(parse_macro("wd<foo>").is_err()); |
| 1073 |
} |
| 1074 |
} |