keystroke.rs

 1use std::fmt::Write;
 2
 3use anyhow::anyhow;
 4use serde::Deserialize;
 5
 6#[derive(Clone, Debug, Eq, PartialEq, Default, Deserialize, Hash)]
 7pub struct Keystroke {
 8    pub ctrl: bool,
 9    pub alt: bool,
10    pub shift: bool,
11    pub cmd: bool,
12    pub function: bool,
13    pub key: String,
14}
15
16impl Keystroke {
17    pub fn parse(source: &str) -> anyhow::Result<Self> {
18        let mut ctrl = false;
19        let mut alt = false;
20        let mut shift = false;
21        let mut cmd = false;
22        let mut function = false;
23        let mut key = None;
24
25        let mut components = source.split('-').peekable();
26        while let Some(component) = components.next() {
27            match component {
28                "ctrl" => ctrl = true,
29                "alt" => alt = true,
30                "shift" => shift = true,
31                "cmd" => cmd = true,
32                "fn" => function = true,
33                _ => {
34                    if let Some(component) = components.peek() {
35                        if component.is_empty() && source.ends_with('-') {
36                            key = Some(String::from("-"));
37                            break;
38                        } else {
39                            return Err(anyhow!("Invalid keystroke `{}`", source));
40                        }
41                    } else {
42                        key = Some(String::from(component));
43                    }
44                }
45            }
46        }
47
48        let key = key.ok_or_else(|| anyhow!("Invalid keystroke `{}`", source))?;
49
50        Ok(Keystroke {
51            ctrl,
52            alt,
53            shift,
54            cmd,
55            function,
56            key,
57        })
58    }
59
60    pub fn modified(&self) -> bool {
61        self.ctrl || self.alt || self.shift || self.cmd
62    }
63}
64
65impl std::fmt::Display for Keystroke {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        if self.ctrl {
68            f.write_char('^')?;
69        }
70        if self.alt {
71            f.write_char('⎇')?;
72        }
73        if self.cmd {
74            f.write_char('⌘')?;
75        }
76        if self.shift {
77            f.write_char('⇧')?;
78        }
79        let key = match self.key.as_str() {
80            "backspace" => '⌫',
81            "up" => '↑',
82            "down" => '↓',
83            "left" => '←',
84            "right" => '→',
85            "tab" => '⇥',
86            "escape" => '⎋',
87            key => {
88                if key.len() == 1 {
89                    key.chars().next().unwrap().to_ascii_uppercase()
90                } else {
91                    return f.write_str(key);
92                }
93            }
94        };
95        f.write_char(key)
96    }
97}