ai-usagebar 1.19.0

Omarchy/Waybar widgets + TUI for tracking multi-provider AI plan usage
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Global toggle shortcut for the tray popover.
//!
//! [`normalize`] is pure and compiles everywhere so Linux CI can test it: it
//! turns whatever the user typed ("shift + ctrl + u") into the one canonical
//! spelling the Settings page echoes back ("Ctrl+Shift+U") and the dialect
//! `global_hotkey::hotkey::HotKey::from_str` parses ("control+shift+KeyU").
//! [`HotkeyBinding`] is the Windows-only registration on top of it.

use std::fmt::Write as _;

/// Canonical spelling shown to the user ("Ctrl+Shift+U") plus the dialect
/// `global_hotkey::hotkey::HotKey::from_str` parses ("control+shift+KeyU").
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Normalized {
    pub canonical: String,
    pub crate_form: String,
}

/// Longest slice of an unrecognised token echoed back in an error. The text
/// came from a config file or a text box, so it is data, not a message.
const MAX_ECHOED_TOKEN_CHARS: usize = 16;

/// Parse a user-typed shortcut into its canonical and crate spellings.
///
/// Tokens are split on `+`, trimmed, and matched case-insensitively. At least
/// one of Ctrl/Alt/Win is required (Shift alone would shadow typing) and
/// exactly one non-modifier key. Escape is refused because it already closes
/// the popover. Errors are user-facing sentences.
pub fn normalize(text: &str) -> Result<Normalized, String> {
    if text.trim().is_empty() {
        return Err("Enter a shortcut".to_string());
    }

    let mut ctrl = false;
    let mut alt = false;
    let mut shift = false;
    let mut win = false;
    let mut key: Option<Key> = None;

    for raw in text.split('+') {
        let token = raw.trim();
        if token.is_empty() {
            return Err("Put a key between each '+'".to_string());
        }
        match token.to_ascii_lowercase().as_str() {
            "ctrl" | "control" => ctrl = true,
            "alt" | "option" => alt = true,
            "shift" => shift = true,
            "win" | "super" | "meta" | "cmd" | "command" => win = true,
            _ => {
                if key.is_some() {
                    return Err("Use exactly one key, for example Ctrl+Shift+U".to_string());
                }
                key = Some(parse_key(token)?);
            }
        }
    }

    let Some(key) = key else {
        return Err("Add a key, for example Ctrl+Shift+U".to_string());
    };
    if !(ctrl || alt || win) {
        return Err("Add Ctrl, Alt or Win".to_string());
    }

    let mut canonical = String::new();
    let mut crate_form = String::new();
    for (on, shown, parsed) in [
        (ctrl, "Ctrl", "control"),
        (alt, "Alt", "alt"),
        (shift, "Shift", "shift"),
        (win, "Win", "super"),
    ] {
        if on {
            let _ = write!(canonical, "{shown}+");
            let _ = write!(crate_form, "{parsed}+");
        }
    }
    canonical.push_str(&key.canonical);
    crate_form.push_str(&key.crate_form);

    Ok(Normalized {
        canonical,
        crate_form,
    })
}

struct Key {
    canonical: String,
    crate_form: String,
}

impl Key {
    fn fixed(canonical: &str, crate_form: &str) -> Self {
        Self {
            canonical: canonical.to_string(),
            crate_form: crate_form.to_string(),
        }
    }
}

fn parse_key(token: &str) -> Result<Key, String> {
    let mut chars = token.chars();
    if let (Some(ch), None) = (chars.next(), chars.next()) {
        if ch.is_ascii_alphabetic() {
            let upper = ch.to_ascii_uppercase();
            return Ok(Key {
                canonical: upper.to_string(),
                crate_form: format!("Key{upper}"),
            });
        }
        if ch.is_ascii_digit() {
            return Ok(Key {
                canonical: ch.to_string(),
                crate_form: format!("Digit{ch}"),
            });
        }
        let punctuation = match ch {
            '-' => Some("Minus"),
            '=' => Some("Equal"),
            '[' => Some("BracketLeft"),
            ']' => Some("BracketRight"),
            '\\' => Some("Backslash"),
            ';' => Some("Semicolon"),
            '\'' => Some("Quote"),
            ',' => Some("Comma"),
            '.' => Some("Period"),
            '/' => Some("Slash"),
            '`' => Some("Backquote"),
            _ => None,
        };
        if let Some(crate_form) = punctuation {
            return Ok(Key::fixed(&ch.to_string(), crate_form));
        }
    }

    let lower = token.to_ascii_lowercase();
    if let Some(number) = lower.strip_prefix('f')
        && number.bytes().all(|b| b.is_ascii_digit())
        && let Ok(n) = number.parse::<u8>()
        && (1..=24).contains(&n)
    {
        let name = format!("F{n}");
        return Ok(Key::fixed(&name, &name));
    }

    let named = match lower.as_str() {
        "space" => Some(("Space", "Space")),
        "enter" | "return" => Some(("Enter", "Enter")),
        "tab" => Some(("Tab", "Tab")),
        "backspace" => Some(("Backspace", "Backspace")),
        "delete" | "del" => Some(("Delete", "Delete")),
        "insert" | "ins" => Some(("Insert", "Insert")),
        "home" => Some(("Home", "Home")),
        "end" => Some(("End", "End")),
        "pageup" | "pgup" => Some(("PageUp", "PageUp")),
        "pagedown" | "pgdn" => Some(("PageDown", "PageDown")),
        "up" | "arrowup" => Some(("Up", "ArrowUp")),
        "down" | "arrowdown" => Some(("Down", "ArrowDown")),
        "left" | "arrowleft" => Some(("Left", "ArrowLeft")),
        "right" | "arrowright" => Some(("Right", "ArrowRight")),
        "escape" | "esc" => return Err("Escape closes the popover".to_string()),
        _ => None,
    };
    if let Some((canonical, crate_form)) = named {
        return Ok(Key::fixed(canonical, crate_form));
    }

    Err(format!("Unsupported key: {}", echo_token(token)))
}

/// Bound and ASCII-only: the token is echoed into a sentence a UI renders.
fn echo_token(token: &str) -> String {
    let echoed: String = token
        .chars()
        .filter(char::is_ascii_graphic)
        .take(MAX_ECHOED_TOKEN_CHARS)
        .collect();
    if echoed.is_empty() {
        "?".to_string()
    } else {
        echoed
    }
}

#[cfg(windows)]
mod binding {
    use global_hotkey::hotkey::HotKey;
    use global_hotkey::{GlobalHotKeyEvent, GlobalHotKeyManager, HotKeyState};

    use crate::display::sanitize_untrusted_line;

    use super::normalize;

    /// The one registered global shortcut, or none.
    ///
    /// The manager owns a message-only window that receives `WM_HOTKEY`, so
    /// the binding lives on the thread that pumps messages: the tao
    /// event-loop thread, the same one the tray icon lives on.
    pub struct HotkeyBinding {
        manager: GlobalHotKeyManager,
        current: Option<HotKey>,
    }

    impl HotkeyBinding {
        /// Must be created on the thread that runs the win32 message loop
        /// (the tao event-loop thread).
        pub fn new() -> Result<Self, String> {
            let manager = GlobalHotKeyManager::new().map_err(|err| {
                format!(
                    "Could not set up the global shortcut: {}",
                    sanitize_untrusted_line(&err.to_string())
                )
            })?;
            Ok(Self {
                manager,
                current: None,
            })
        }

        /// Replace the registered shortcut. `None` unregisters. On failure
        /// nothing stays registered and the error is a user-facing sentence
        /// ("Ctrl+Shift+U is already used by another app").
        pub fn apply(&mut self, canonical: Option<&str>) -> Result<(), String> {
            let wanted = match canonical {
                None => None,
                Some(text) => {
                    let normalized = normalize(text)?;
                    let hotkey: HotKey = normalized.crate_form.parse().map_err(|err| {
                        format!(
                            "{} is not a shortcut this build can register: {}",
                            normalized.canonical,
                            sanitize_untrusted_line(&format!("{err}"))
                        )
                    })?;
                    Some((normalized.canonical, hotkey))
                }
            };

            if let (Some(current), Some((_, hotkey))) = (self.current, &wanted)
                && current == *hotkey
            {
                return Ok(());
            }

            if let Some(previous) = self.current.take() {
                self.manager.unregister(previous).map_err(|err| {
                    format!(
                        "Could not release the previous shortcut: {}",
                        sanitize_untrusted_line(&err.to_string())
                    )
                })?;
            }

            let Some((canonical, hotkey)) = wanted else {
                return Ok(());
            };
            self.manager
                .register(hotkey)
                .map_err(|err| register_failure(&canonical, &err))?;
            self.current = Some(hotkey);
            Ok(())
        }

        /// Id the crate reports in press events for the registered shortcut.
        pub fn current_id(&self) -> Option<u32> {
            self.current.map(|hotkey| hotkey.id())
        }
    }

    /// Turn a registration failure into the sentence the Settings page shows.
    fn register_failure(canonical: &str, err: &global_hotkey::Error) -> String {
        match err {
            global_hotkey::Error::AlreadyRegistered(_) => {
                format!("{canonical} is already used by another app")
            }
            other => format!(
                "Could not register {canonical}: {}",
                sanitize_untrusted_line(&other.to_string())
            ),
        }
    }

    /// Route presses (`HotKeyState::Pressed` only) to the caller; replaces the
    /// crate's channel receiver. The crate keeps the first handler installed
    /// for the life of the process, so call this once.
    pub fn install_press_handler<F: Fn(u32) + Send + Sync + 'static>(handler: F) {
        GlobalHotKeyEvent::set_event_handler(Some(move |event: GlobalHotKeyEvent| {
            if event.state() == HotKeyState::Pressed {
                handler(event.id());
            }
        }));
    }

    #[cfg(test)]
    mod tests {
        use std::str::FromStr;

        use global_hotkey::hotkey::{Code, HotKey, Modifiers};

        use super::super::normalize;
        use super::register_failure;

        #[test]
        fn crate_parses_the_crate_form_we_emit() {
            let normalized = normalize("Ctrl+Shift+U").expect("valid shortcut");
            let hotkey = HotKey::from_str(&normalized.crate_form).expect("crate accepts it");
            assert_eq!(hotkey.mods, Modifiers::CONTROL | Modifiers::SHIFT);
            assert_eq!(hotkey.key, Code::KeyU);
        }

        #[test]
        fn crate_parses_every_key_family_we_emit() {
            for text in [
                "Win+Alt+F12",
                "Ctrl+1",
                "Alt+Up",
                "Ctrl+Alt+Space",
                "Ctrl+`",
                "Ctrl+Shift+\\",
            ] {
                let normalized = normalize(text).expect(text);
                HotKey::from_str(&normalized.crate_form).expect(text);
            }
        }

        #[test]
        fn already_registered_becomes_a_friendly_sentence() {
            let hotkey = HotKey::new(Some(Modifiers::CONTROL), Code::KeyU);
            let message =
                register_failure("Ctrl+U", &global_hotkey::Error::AlreadyRegistered(hotkey));
            assert_eq!(message, "Ctrl+U is already used by another app");
        }

        #[test]
        fn other_failures_name_the_shortcut_and_strip_controls() {
            let err = global_hotkey::Error::FailedToRegister("bad\u{1b}[31m vk".to_string());
            let message = register_failure("Ctrl+U", &err);
            assert!(
                message.starts_with("Could not register Ctrl+U: "),
                "{message}"
            );
            assert!(!message.contains('\u{1b}'), "{message}");
        }
    }
}

#[cfg(windows)]
pub use binding::{HotkeyBinding, install_press_handler};

#[cfg(test)]
mod tests {
    use super::*;

    fn canonical(text: &str) -> String {
        normalize(text)
            .unwrap_or_else(|err| panic!("{text}: {err}"))
            .canonical
    }

    fn crate_form(text: &str) -> String {
        normalize(text)
            .unwrap_or_else(|err| panic!("{text}: {err}"))
            .crate_form
    }

    fn refusal(text: &str) -> String {
        match normalize(text) {
            Ok(normalized) => panic!("{text} was accepted as {normalized:?}"),
            Err(err) => err,
        }
    }

    #[test]
    fn modifiers_come_out_in_canonical_order() {
        assert_eq!(canonical("shift+ctrl+u"), "Ctrl+Shift+U");
        assert_eq!(canonical("win+shift+alt+ctrl+k"), "Ctrl+Alt+Shift+Win+K");
    }

    #[test]
    fn whitespace_and_case_are_forgiven() {
        assert_eq!(canonical("  CTRL +  Shift + u "), "Ctrl+Shift+U");
    }

    #[test]
    fn modifier_aliases_collapse() {
        assert_eq!(canonical("control+u"), "Ctrl+U");
        assert_eq!(canonical("option+u"), "Alt+U");
        for win in ["win", "super", "meta", "cmd", "command"] {
            assert_eq!(canonical(&format!("{win}+u")), "Win+U", "{win}");
        }
    }

    #[test]
    fn repeated_modifiers_are_harmless() {
        assert_eq!(canonical("ctrl+control+u"), "Ctrl+U");
    }

    #[test]
    fn function_keys() {
        assert_eq!(canonical("alt+f1"), "Alt+F1");
        assert_eq!(canonical("ctrl+F24"), "Ctrl+F24");
        assert_eq!(crate_form("ctrl+F24"), "control+F24");
    }

    #[test]
    fn arrows_use_short_canonical_and_crate_names() {
        assert_eq!(canonical("ctrl+arrowup"), "Ctrl+Up");
        assert_eq!(crate_form("ctrl+up"), "control+ArrowUp");
        assert_eq!(crate_form("alt+left"), "alt+ArrowLeft");
    }

    #[test]
    fn punctuation_keys() {
        assert_eq!(canonical("ctrl+-"), "Ctrl+-");
        assert_eq!(crate_form("ctrl+-"), "control+Minus");
        assert_eq!(crate_form("ctrl+="), "control+Equal");
        assert_eq!(crate_form("ctrl+["), "control+BracketLeft");
        assert_eq!(crate_form("ctrl+]"), "control+BracketRight");
        assert_eq!(crate_form("ctrl+\\"), "control+Backslash");
        assert_eq!(crate_form("ctrl+;"), "control+Semicolon");
        assert_eq!(crate_form("ctrl+'"), "control+Quote");
        assert_eq!(crate_form("ctrl+,"), "control+Comma");
        assert_eq!(crate_form("ctrl+."), "control+Period");
        assert_eq!(crate_form("ctrl+/"), "control+Slash");
        assert_eq!(crate_form("ctrl+`"), "control+Backquote");
    }

    #[test]
    fn digits_and_named_keys() {
        assert_eq!(canonical("ctrl+1"), "Ctrl+1");
        assert_eq!(crate_form("ctrl+1"), "control+Digit1");
        assert_eq!(canonical("ctrl+space"), "Ctrl+Space");
        assert_eq!(canonical("ctrl+pgup"), "Ctrl+PageUp");
        assert_eq!(crate_form("win+enter"), "super+Enter");
    }

    #[test]
    fn crate_form_spells_letters_as_key_codes() {
        assert_eq!(crate_form("Ctrl+Shift+U"), "control+shift+KeyU");
        assert_eq!(crate_form("win+alt+z"), "alt+super+KeyZ");
    }

    #[test]
    fn shift_alone_is_not_enough() {
        assert_eq!(refusal("Shift+U"), "Add Ctrl, Alt or Win");
    }

    #[test]
    fn a_bare_key_is_refused() {
        assert_eq!(refusal("u"), "Add Ctrl, Alt or Win");
    }

    #[test]
    fn two_keys_are_refused() {
        assert_eq!(
            refusal("ctrl+u+i"),
            "Use exactly one key, for example Ctrl+Shift+U"
        );
    }

    #[test]
    fn only_modifiers_are_refused() {
        assert_eq!(refusal("ctrl+shift"), "Add a key, for example Ctrl+Shift+U");
    }

    #[test]
    fn escape_is_refused() {
        assert_eq!(refusal("ctrl+escape"), "Escape closes the popover");
        assert_eq!(refusal("ctrl+esc"), "Escape closes the popover");
    }

    #[test]
    fn blank_is_refused() {
        assert_eq!(refusal(""), "Enter a shortcut");
        assert_eq!(refusal("   "), "Enter a shortcut");
    }

    #[test]
    fn an_empty_token_is_refused() {
        assert_eq!(refusal("ctrl++u"), "Put a key between each '+'");
    }

    #[test]
    fn unknown_tokens_are_echoed() {
        assert_eq!(refusal("ctrl+numpad5"), "Unsupported key: numpad5");
        assert_eq!(refusal("ctrl+f25"), "Unsupported key: f25");
        assert_eq!(refusal("ctrl+f0"), "Unsupported key: f0");
        assert_eq!(refusal("ctrl+f1x"), "Unsupported key: f1x");
    }

    #[test]
    fn overlong_garbage_is_clipped_and_stripped() {
        let garbage = format!("ctrl+\u{1b}[31m{}\u{e9}", "x".repeat(40));
        let message = refusal(&garbage);
        assert_eq!(message, format!("Unsupported key: [31m{}", "x".repeat(12)));
        assert!(message.len() <= "Unsupported key: ".len() + MAX_ECHOED_TOKEN_CHARS);
    }

    #[test]
    fn a_token_with_nothing_printable_echoes_a_placeholder() {
        assert_eq!(refusal("ctrl+\u{1b}"), "Unsupported key: ?");
    }
}