kimun-notes 0.16.1

A terminal-based notes application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::{fmt::Display, hash::Hash};

use serde::{Deserialize, Serialize};

use super::key_strike::KeyStrike;

#[derive(
    Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord,
)]
#[serde(try_from = "String", into = "String")]
pub struct KeyCombo {
    pub modifiers: KeyModifiers,
    pub key: KeyStrike,
}

impl Display for KeyCombo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let modif = self.modifiers.to_string();
        let key = self.key.to_string();
        if modif.is_empty() {
            write!(f, "{}", key)
        } else {
            write!(f, "{}&{}", modif, key)
        }
    }
}

impl TryFrom<String> for KeyCombo {
    type Error = String;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        let splits = value.split("&").collect::<Vec<_>>();
        match splits.len() {
            0 => Err("No Keys found here".to_string()),
            1 => match KeyStrike::try_from(splits.first().unwrap().trim().to_string()) {
                Ok(ks) => Ok(KeyCombo {
                    modifiers: KeyModifiers::default(),
                    key: ks,
                }),
                Err(e) => Err(e),
            },
            2 => {
                let m = splits.first().unwrap().trim().to_string();
                let k = splits.last().unwrap().trim().to_string();

                match (KeyModifiers::try_from(m), KeyStrike::try_from(k)) {
                    (Ok(modifiers), Ok(key)) => Ok(KeyCombo { modifiers, key }),
                    (Ok(_), Err(e)) => Err(e),
                    (Err(e), Ok(_)) => Err(e),
                    (Err(em), Err(ek)) => Err(format!("{} - {}", em, ek)),
                }
            }
            _ => Err(format!(
                "This is a non valid combination, only one key and a modifier combination is allowed: {}",
                value
            )),
        }
    }
}

impl From<KeyCombo> for String {
    fn from(value: KeyCombo) -> Self {
        value.to_string()
    }
}

// impl TryFrom<KeyboardData> for KeyCombo {
//     type Error = String;

//     fn try_from(value: KeyboardData) -> Result<Self, Self::Error> {
//         let key: KeyStrike = value.key().into();
//         let modifiers: KeyModifiers = value.modifiers().into();

//         if key == KeyStrike::Unknown {
//             Err(format!("Unknown Key: {}", value.key()))
//         } else {
//             Ok(KeyCombo { modifiers, key })
//         }
//     }
// }

// impl From<Rc<KeyboardData>> for KeyCombo {
//     fn from(value: Rc<KeyboardData>) -> Self {
//         let key: KeyStrike = value.key().into();
//         let modifiers: KeyModifiers = value.modifiers().into();

//         if key == KeyStrike::Unknown {
//             error!("Unknown Key: {}", value.key());
//             KeyCombo::default()
//         } else {
//             KeyCombo { modifiers, key }
//         }
//     }
// }

impl KeyCombo {
    pub fn new(modifiers: KeyModifiers, key: KeyStrike) -> Self {
        Self { modifiers, key }
    }

    /// Returns `true` for combinations accepted in the config file:
    /// - ctrl/alt (with optional shift) + a letter key (a–z), **or**
    /// - a bare F-key (F1–F12, no modifier required)
    pub fn is_valid_binding(&self) -> bool {
        let is_letter_combo = (self.modifiers.is_ctrl() || self.modifiers.is_alt())
            && (self.key >= KeyStrike::KeyA && self.key <= KeyStrike::KeyZ
                || self.key >= KeyStrike::Digit0 && self.key <= KeyStrike::Digit9
                || matches!(
                    self.key,
                    KeyStrike::Comma
                        | KeyStrike::Period
                        | KeyStrike::Slash
                        | KeyStrike::Semicolon
                        | KeyStrike::Quote
                        | KeyStrike::BracketLeft
                        | KeyStrike::BracketRight
                        | KeyStrike::Backslash
                        | KeyStrike::Backquote
                        | KeyStrike::Minus
                        | KeyStrike::Equal
                ));
        let is_fkey = matches!(
            self.key,
            KeyStrike::F1
                | KeyStrike::F2
                | KeyStrike::F3
                | KeyStrike::F4
                | KeyStrike::F5
                | KeyStrike::F6
                | KeyStrike::F7
                | KeyStrike::F8
                | KeyStrike::F9
                | KeyStrike::F10
                | KeyStrike::F11
                | KeyStrike::F12
        );
        is_letter_combo || is_fkey
    }
}

/// Pressed modifier keys.
///
/// Specification:
/// <https://w3c.github.io/uievents-key/#keys-modifier>
#[derive(
    Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord,
)]
#[serde(try_from = "String", into = "String")]
pub struct KeyModifiers {
    alt: bool,
    ctrl: bool,
    cmd: bool,
    shift: bool,
}

// For compatibility
const META: &str = "meta";
const CMD: &str = "cmd";

const ALT: &str = "alt";
const CONTROL: &str = "ctrl";
const SHIFT: &str = "shift";

// For compatibility
#[cfg(target_os = "macos")]
const META_CMD: &str = CMD;
#[cfg(not(target_os = "macos"))]
const META_CMD: &str = META;

impl TryFrom<String> for KeyModifiers {
    type Error = String;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        let splits = value.split("+");
        let mut modifiers = KeyModifiers::default();
        for modif in splits {
            match modif {
                "" => {}
                CONTROL => modifiers.with_ctrl(),
                SHIFT => modifiers.with_shift(),
                ALT => modifiers.with_alt(),
                META => modifiers.with_meta_cmd(),
                CMD => modifiers.with_meta_cmd(),
                _ => return Err(format!("Non valid modifier value: {}", modif)),
            }
        }
        Ok(modifiers)
    }
}

impl From<KeyModifiers> for String {
    fn from(value: KeyModifiers) -> Self {
        value.to_string()
    }
}

// impl From<Modifiers> for KeyModifiers {
//     fn from(value: Modifiers) -> Self {
//         let mut km = KeyModifiers::default();
//         if value.shift() {
//             km.with_shift();
//         }
//         if value.ctrl() {
//             km.with_ctrl();
//         }
//         if value.alt() {
//             km.with_alt();
//         }
//         if value.meta() {
//             km.with_meta_cmd();
//         }
//         km
//     }
// }

impl Display for KeyModifiers {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut modifiers = vec![];
        if self.is_ctrl() {
            modifiers.push(CONTROL);
        }
        if self.is_alt() {
            modifiers.push(ALT);
        }
        if self.is_meta_cmd() {
            modifiers.push(META_CMD);
        }
        if self.is_shift() {
            modifiers.push(SHIFT);
        }
        let string = modifiers.join("+");
        write!(f, "{}", string)
    }
}

impl KeyModifiers {
    pub fn new() -> Self {
        KeyModifiers::default()
    }

    pub fn is_empty(&self) -> bool {
        !self.alt && !self.ctrl && !self.cmd && !self.shift
    }

    pub fn with_shift(&mut self) {
        self.shift = true;
    }
    pub fn with_ctrl(&mut self) {
        self.ctrl = true;
    }
    pub fn with_alt(&mut self) {
        self.alt = true;
    }
    pub fn with_meta_cmd(&mut self) {
        self.cmd = true;
    }

    pub fn and_shift(mut self) -> Self {
        self.with_shift();
        self
    }
    pub fn and_ctrl(mut self) -> Self {
        self.with_ctrl();
        self
    }
    pub fn and_alt(mut self) -> Self {
        self.with_alt();
        self
    }
    pub fn and_meta_cmd(mut self) -> Self {
        self.with_meta_cmd();
        self
    }
    /// Return `true` if a shift key is pressed.
    pub fn is_shift(&self) -> bool {
        self.shift
    }

    /// Return `true` if a control key is pressed.
    pub fn is_ctrl(&self) -> bool {
        self.ctrl
    }

    /// Return `true` if an alt key is pressed.
    pub fn is_alt(&self) -> bool {
        self.alt
    }

    /// Return `true` if a meta key is pressed.
    pub fn is_meta_cmd(&self) -> bool {
        self.cmd
    }
}

#[cfg(test)]
mod tests {
    use color_eyre::eyre;

    use crate::keys::{key_combo::KeyCombo, key_strike::KeyStrike};

    use super::KeyModifiers;

    #[test]
    fn serialize_keymodifier() -> eyre::Result<()> {
        let mut km = KeyModifiers::default();
        km.with_shift();

        let km_ser = km.to_string();
        assert_eq!("shift", km_ser);

        km.with_ctrl();
        let km_ser = km.to_string();
        assert_eq!("ctrl+shift", km_ser);
        Ok(())
    }

    #[test]
    fn deserialize_keymodifier() -> eyre::Result<()> {
        let text = "meta+shift";
        let km = KeyModifiers::try_from(text.to_string());

        assert!(km.is_ok());

        let km = km.unwrap();
        assert!(km.cmd);
        assert!(km.shift);
        assert!(!km.ctrl);
        assert!(!km.alt);

        Ok(())
    }

    #[test]
    fn serialize_keycombo() {
        let kc = KeyCombo::new(
            KeyModifiers::new().and_meta_cmd().and_ctrl(),
            crate::keys::key_strike::KeyStrike::KeyN,
        );

        let kc_ser = kc.to_string();

        #[cfg(target_os = "macos")]
        assert_eq!("ctrl+cmd&N", kc_ser);
        #[cfg(not(target_os = "macos"))]
        assert_eq!("ctrl+meta&N", kc_ser);
    }

    #[test]
    fn deserialize_keycombo_meta() {
        let string = "shift+meta & H".to_string();

        let kc = KeyCombo::try_from(string).unwrap();

        assert!(kc.modifiers.shift);
        assert!(kc.modifiers.cmd);
        assert!(!kc.modifiers.ctrl);
        assert!(!kc.modifiers.alt);
        assert_eq!(kc.key, KeyStrike::KeyH);
    }

    #[test]
    fn deserialize_keycombo_cmd() {
        let string = "shift+cmd & H".to_string();

        let kc = KeyCombo::try_from(string).unwrap();

        assert!(kc.modifiers.shift);
        assert!(kc.modifiers.cmd);
        assert!(!kc.modifiers.ctrl);
        assert!(!kc.modifiers.alt);
        assert_eq!(kc.key, KeyStrike::KeyH);
    }

    #[test]
    fn deserialize_keycombo_no_mod() {
        let string = "L".to_string();

        let kc = KeyCombo::try_from(string).unwrap();

        assert!(!kc.modifiers.shift);
        assert!(!kc.modifiers.cmd);
        assert!(!kc.modifiers.ctrl);
        assert!(!kc.modifiers.alt);
        assert_eq!(kc.key, KeyStrike::KeyL);
    }

    #[test]
    fn roundtrip_keycombo_no_modifier() {
        // A combo with no modifiers must serialize without " & " prefix
        // and deserialize back correctly.
        let kc = KeyCombo::new(KeyModifiers::default(), KeyStrike::Tab);
        let serialized = kc.to_string();
        assert_eq!(serialized, "<Tab>");

        let parsed = KeyCombo::try_from(serialized).unwrap();
        assert_eq!(parsed, kc);
    }

    #[test]
    fn deserialize_legacy_no_modifier_with_ampersand() {
        // Old config files wrote " & <Tab>" for no-modifier Tab — must still parse.
        let kc = KeyCombo::try_from(" & <Tab>".to_string()).unwrap();
        assert!(!kc.modifiers.is_ctrl());
        assert!(!kc.modifiers.is_shift());
        assert_eq!(kc.key, KeyStrike::Tab);
    }
}