rpi-tui 0.3.1

Terminal User Interface for pi-rust
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
//! Keybinding system for TUI.
//!
//! Provides configurable keyboard shortcuts for TUI actions.
//! This is a 1:1 port of the TypeScript keybindings.ts

use std::collections::HashMap;

use crossterm::event::{KeyCode, KeyModifiers};

/// A keybinding identifier.
pub type KeybindingId = &'static str;

/// A key combination.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyCombo {
    pub code: KeyCode,
    pub modifiers: KeyModifiers,
}

impl KeyCombo {
    /// Create a new key combination.
    pub fn new(code: KeyCode, modifiers: KeyModifiers) -> Self {
        Self { code, modifiers }
    }

    /// Create from just a key code (no modifiers).
    pub fn from_code(code: KeyCode) -> Self {
        Self {
            code,
            modifiers: KeyModifiers::NONE,
        }
    }

    /// Add Ctrl modifier.
    pub fn ctrl(self) -> Self {
        Self {
            modifiers: self.modifiers | KeyModifiers::CONTROL,
            ..self
        }
    }

    /// Add Shift modifier.
    pub fn shift(self) -> Self {
        Self {
            modifiers: self.modifiers | KeyModifiers::SHIFT,
            ..self
        }
    }

    /// Add Alt modifier.
    pub fn alt(self) -> Self {
        Self {
            modifiers: self.modifiers | KeyModifiers::ALT,
            ..self
        }
    }

    /// Add Super modifier.
    pub fn super_key(self) -> Self {
        Self {
            modifiers: self.modifiers | KeyModifiers::SUPER,
            ..self
        }
    }

    /// Human-readable key text for hints (mirrors native pi's `formatKeyText`,
    /// e.g. `Alt+Q`, `Ctrl+Shift+F`, `Enter`). Modifiers are ordered
    /// Ctrl, Alt, Shift, Super.
    pub fn display(&self) -> String {
        let mut parts: Vec<String> = Vec::new();
        if self.modifiers.contains(KeyModifiers::CONTROL) {
            parts.push("Ctrl".into());
        }
        if self.modifiers.contains(KeyModifiers::ALT) {
            parts.push("Alt".into());
        }
        // `BackTab` already carries the shift; don't render it twice.
        if self.modifiers.contains(KeyModifiers::SHIFT) && self.code != KeyCode::BackTab {
            parts.push("Shift".into());
        }
        if self.modifiers.contains(KeyModifiers::SUPER) {
            parts.push("Super".into());
        }
        let key = match self.code {
            KeyCode::Char(c) => c.to_uppercase().to_string(),
            KeyCode::Enter => "Enter".into(),
            KeyCode::Esc => "Esc".into(),
            KeyCode::Tab | KeyCode::BackTab => "Tab".into(),
            KeyCode::Backspace => "Backspace".into(),
            KeyCode::Delete => "Delete".into(),
            KeyCode::Insert => "Insert".into(),
            KeyCode::Up => "Up".into(),
            KeyCode::Down => "Down".into(),
            KeyCode::Left => "Left".into(),
            KeyCode::Right => "Right".into(),
            KeyCode::Home => "Home".into(),
            KeyCode::End => "End".into(),
            KeyCode::PageUp => "PageUp".into(),
            KeyCode::PageDown => "PageDown".into(),
            other => format!("{other:?}"),
        };
        parts.push(key);
        parts.join("+")
    }
}

/// Keybinding definition.
#[derive(Debug, Clone)]
pub struct KeybindingDefinition {
    pub default_keys: Vec<KeyCombo>,
    pub description: Option<&'static str>,
}

/// Keybinding conflict.
#[derive(Debug, Clone)]
pub struct KeybindingConflict {
    pub key: KeyCombo,
    pub keybindings: Vec<KeybindingId>,
}

/// Keybindings manager.
#[derive(Clone)]
pub struct Keybindings {
    definitions: HashMap<KeybindingId, KeybindingDefinition>,
    user_bindings: HashMap<KeybindingId, Vec<KeyCombo>>,
    keys_by_id: HashMap<KeybindingId, Vec<KeyCombo>>,
    conflicts: Vec<KeybindingConflict>,
}

impl Keybindings {
    /// Create a new keybindings manager with default bindings.
    pub fn new() -> Self {
        let definitions = Self::default_definitions();
        let keys_by_id = definitions
            .iter()
            .map(|(id, def)| (*id, def.default_keys.clone()))
            .collect();
        Self {
            definitions,
            user_bindings: HashMap::new(),
            keys_by_id,
            conflicts: Vec::new(),
        }
    }

    /// Get default keybinding definitions.
    fn default_definitions() -> HashMap<KeybindingId, KeybindingDefinition> {
        use KeyCode::*;
        use KeyModifiers as M;

        let mut map = HashMap::new();

        // Application-level actions. These IDs intentionally match native Pi
        // so a copied settings.json can override them; rpi keeps its existing
        // Ctrl+M model-cycle default for backward compatibility.
        map.insert(
            "app.interrupt",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Esc, M::NONE)],
                description: Some("Cancel or abort"),
            },
        );
        map.insert(
            "app.clear",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('c'), M::CONTROL)],
                description: Some("Clear editor"),
            },
        );
        map.insert(
            "app.exit",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('d'), M::CONTROL)],
                description: Some("Exit when editor is empty"),
            },
        );
        map.insert(
            "app.model.select",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('l'), M::CONTROL)],
                description: Some("Open model selector"),
            },
        );
        map.insert(
            "app.model.cycleForward",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('m'), M::CONTROL)],
                description: Some("Cycle to next model"),
            },
        );
        map.insert(
            "app.tools.expand",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('o'), M::CONTROL)],
                description: Some("Toggle tool output"),
            },
        );
        map.insert(
            "app.thinking.toggle",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('t'), M::CONTROL)],
                description: Some("Toggle thinking blocks"),
            },
        );
        map.insert(
            "app.editor.external",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('g'), M::CONTROL)],
                description: Some("Open external editor"),
            },
        );
        map.insert(
            "app.thinking.cycle",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Tab, M::SHIFT)],
                description: Some("Cycle thinking level"),
            },
        );
        map.insert(
            "app.clipboard.pasteImage",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('v'), M::CONTROL)],
                description: Some("Paste image from clipboard"),
            },
        );
        map.insert(
            "app.message.dequeue",
            KeybindingDefinition {
                // Native pi: `alt+q` on Windows, `alt+up` elsewhere.
                default_keys: vec![if cfg!(windows) {
                    KeyCombo::new(Char('q'), M::ALT)
                } else {
                    KeyCombo::new(Up, M::ALT)
                }],
                description: Some("Restore queued messages"),
            },
        );

        // Editor navigation and editing
        map.insert(
            "tui.editor.cursorUp",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Up, M::NONE)],
                description: Some("Move cursor up"),
            },
        );
        map.insert(
            "tui.editor.cursorDown",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Down, M::NONE)],
                description: Some("Move cursor down"),
            },
        );
        map.insert(
            "tui.editor.historyPrevious",
            KeybindingDefinition {
                default_keys: vec![],
                description: Some("Select previous prompt history entry"),
            },
        );
        map.insert(
            "tui.editor.historyNext",
            KeybindingDefinition {
                default_keys: vec![],
                description: Some("Select next prompt history entry"),
            },
        );
        map.insert(
            "tui.editor.cursorLeft",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Left, M::NONE),
                    KeyCombo::new(Char('b'), M::CONTROL),
                ],
                description: Some("Move cursor left"),
            },
        );
        map.insert(
            "tui.editor.cursorRight",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Right, M::NONE),
                    KeyCombo::new(Char('f'), M::CONTROL),
                ],
                description: Some("Move cursor right"),
            },
        );
        map.insert(
            "tui.editor.cursorWordLeft",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Left, M::ALT),
                    KeyCombo::new(Left, M::CONTROL),
                    KeyCombo::new(Char('b'), M::ALT),
                ],
                description: Some("Move cursor word left"),
            },
        );
        map.insert(
            "tui.editor.cursorWordRight",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Right, M::ALT),
                    KeyCombo::new(Right, M::CONTROL),
                    KeyCombo::new(Char('f'), M::ALT),
                ],
                description: Some("Move cursor word right"),
            },
        );
        map.insert(
            "tui.editor.cursorLineStart",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Home, M::NONE),
                    KeyCombo::new(Home, M::CONTROL),
                    KeyCombo::new(Char('a'), M::CONTROL),
                ],
                description: Some("Move to line start"),
            },
        );
        map.insert(
            "tui.editor.cursorLineEnd",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(End, M::NONE),
                    KeyCombo::new(End, M::CONTROL),
                    KeyCombo::new(Char('e'), M::CONTROL),
                ],
                description: Some("Move to line end"),
            },
        );
        map.insert(
            "tui.editor.jumpForward",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char(']'), M::CONTROL)],
                description: Some("Jump forward to character"),
            },
        );
        map.insert(
            "tui.editor.jumpBackward",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char(']'), M::CONTROL | M::ALT)],
                description: Some("Jump backward to character"),
            },
        );
        map.insert(
            "tui.editor.pageUp",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(PageUp, M::NONE),
                    KeyCombo::new(PageUp, M::CONTROL),
                ],
                description: Some("Page up"),
            },
        );
        map.insert(
            "tui.editor.pageDown",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(PageDown, M::NONE),
                    KeyCombo::new(PageDown, M::CONTROL),
                ],
                description: Some("Page down"),
            },
        );
        map.insert(
            "tui.editor.deleteCharBackward",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Backspace, M::NONE)],
                description: Some("Delete character backward"),
            },
        );
        map.insert(
            "tui.editor.deleteCharForward",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Delete, M::NONE),
                    KeyCombo::new(Char('d'), M::CONTROL),
                ],
                description: Some("Delete character forward"),
            },
        );
        map.insert(
            "tui.editor.deleteWordBackward",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Char('w'), M::CONTROL),
                    KeyCombo::new(Backspace, M::ALT),
                ],
                description: Some("Delete word backward"),
            },
        );
        map.insert(
            "tui.editor.deleteWordForward",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Char('d'), M::ALT),
                    KeyCombo::new(Delete, M::ALT),
                ],
                description: Some("Delete word forward"),
            },
        );
        map.insert(
            "tui.editor.deleteToLineStart",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('u'), M::CONTROL)],
                description: Some("Delete to line start"),
            },
        );
        map.insert(
            "tui.editor.deleteToLineEnd",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('k'), M::CONTROL)],
                description: Some("Delete to line end"),
            },
        );
        map.insert(
            "tui.editor.yank",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('y'), M::CONTROL)],
                description: Some("Yank"),
            },
        );
        map.insert(
            "tui.editor.yankPop",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('y'), M::ALT)],
                description: Some("Yank pop"),
            },
        );
        map.insert(
            "tui.editor.undo",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('-'), M::CONTROL)],
                description: Some("Undo"),
            },
        );

        // Generic input actions
        map.insert(
            "tui.input.newLine",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Enter, M::SHIFT),
                    KeyCombo::new(Char('j'), M::CONTROL),
                ],
                description: Some("Insert newline"),
            },
        );
        map.insert(
            "tui.input.submit",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Enter, M::NONE)],
                description: Some("Submit input"),
            },
        );
        map.insert(
            "tui.input.tab",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Tab, M::NONE)],
                description: Some("Tab / autocomplete"),
            },
        );
        map.insert(
            "tui.input.copy",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('c'), M::CONTROL)],
                description: Some("Copy selection"),
            },
        );

        // Generic selection actions
        map.insert(
            "tui.select.up",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Up, M::NONE)],
                description: Some("Move selection up"),
            },
        );
        map.insert(
            "tui.select.down",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Down, M::NONE)],
                description: Some("Move selection down"),
            },
        );
        map.insert(
            "tui.select.pageUp",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(PageUp, M::NONE)],
                description: Some("Selection page up"),
            },
        );
        map.insert(
            "tui.select.pageDown",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(PageDown, M::NONE)],
                description: Some("Selection page down"),
            },
        );
        map.insert(
            "tui.select.confirm",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Enter, M::NONE)],
                description: Some("Confirm selection"),
            },
        );
        map.insert(
            "tui.select.cancel",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Esc, M::NONE),
                    KeyCombo::new(Char('c'), M::CONTROL),
                ],
                description: Some("Cancel selection"),
            },
        );

        // Alternate-screen viewport navigation
        map.insert(
            "tui.altScreen.pageUp",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(PageUp, M::NONE)],
                description: Some("Scroll viewport up one page"),
            },
        );
        map.insert(
            "tui.altScreen.pageDown",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(PageDown, M::NONE)],
                description: Some("Scroll viewport down one page"),
            },
        );
        map.insert(
            "tui.altScreen.halfPageUp",
            KeybindingDefinition {
                default_keys: vec![],
                description: Some("Scroll viewport up half a page"),
            },
        );
        map.insert(
            "tui.altScreen.halfPageDown",
            KeybindingDefinition {
                default_keys: vec![],
                description: Some("Scroll viewport down half a page"),
            },
        );
        map.insert(
            "tui.altScreen.lineUp",
            KeybindingDefinition {
                default_keys: vec![],
                description: Some("Scroll viewport up one line"),
            },
        );
        map.insert(
            "tui.altScreen.lineDown",
            KeybindingDefinition {
                default_keys: vec![],
                description: Some("Scroll viewport down one line"),
            },
        );
        map.insert(
            "tui.altScreen.previousPrompt",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Up, M::CONTROL | M::SHIFT)],
                description: Some("Jump to previous semantic prompt"),
            },
        );
        map.insert(
            "tui.altScreen.nextPrompt",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Down, M::CONTROL | M::SHIFT)],
                description: Some("Jump to next semantic prompt"),
            },
        );
        map.insert(
            "tui.altScreen.search",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Char('f'), M::CONTROL | M::SHIFT)],
                description: Some("Search the primary scroll view"),
            },
        );
        map.insert(
            "tui.altScreen.searchNext",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Enter, M::NONE),
                    KeyCombo::new(Char('g'), M::CONTROL),
                ],
                description: Some("Select the next search match"),
            },
        );
        map.insert(
            "tui.altScreen.searchPrevious",
            KeybindingDefinition {
                default_keys: vec![
                    KeyCombo::new(Enter, M::SHIFT),
                    KeyCombo::new(Char('g'), M::CONTROL | M::SHIFT),
                ],
                description: Some("Select the previous search match"),
            },
        );
        map.insert(
            "tui.altScreen.searchClose",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Esc, M::NONE)],
                description: Some("Close transcript search"),
            },
        );
        map.insert(
            "tui.altScreen.top",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(Home, M::NONE)],
                description: Some("Scroll viewport to top"),
            },
        );
        map.insert(
            "tui.altScreen.bottom",
            KeybindingDefinition {
                default_keys: vec![KeyCombo::new(End, M::NONE)],
                description: Some("Scroll viewport to bottom"),
            },
        );

        map
    }

    /// Check if a key event matches a keybinding.
    pub fn matches(&self, event: &crossterm::event::KeyEvent, binding_id: KeybindingId) -> bool {
        if let Some(combos) = self.keys_by_id.get(binding_id) {
            let combo = KeyCombo::new(event.code, event.modifiers);
            return combos.contains(&combo);
        }
        false
    }

    /// Get the keybinding ID for a key event.
    pub fn get_binding(&self, event: &crossterm::event::KeyEvent) -> Option<KeybindingId> {
        let combo = KeyCombo::new(event.code, event.modifiers);
        for (id, combos) in &self.keys_by_id {
            if combos.contains(&combo) {
                return Some(*id);
            }
        }
        None
    }

    /// Get keys for a keybinding.
    pub fn get_keys(&self, binding_id: KeybindingId) -> Vec<KeyCombo> {
        self.keys_by_id.get(binding_id).cloned().unwrap_or_default()
    }

    /// Get definition for a keybinding.
    pub fn get_definition(&self, binding_id: KeybindingId) -> Option<&KeybindingDefinition> {
        self.definitions.get(binding_id)
    }

    /// Get conflicts.
    pub fn get_conflicts(&self) -> &[KeybindingConflict] {
        &self.conflicts
    }

    /// Set user bindings.
    pub fn set_user_bindings(&mut self, user_bindings: HashMap<KeybindingId, Vec<KeyCombo>>) {
        self.user_bindings = user_bindings;
        self.rebuild();
    }

    /// Rebuild the keybindings after user bindings change.
    fn rebuild(&mut self) {
        self.keys_by_id.clear();
        self.conflicts.clear();

        // Track which keys are claimed by which keybindings
        let mut key_claims: HashMap<KeyCombo, Vec<KeybindingId>> = HashMap::new();

        for (id, def) in &self.definitions {
            let keys = if let Some(user_keys) = self.user_bindings.get(id) {
                user_keys.clone()
            } else {
                def.default_keys.clone()
            };

            // Track claims
            for key in &keys {
                key_claims.entry(*key).or_default().push(*id);
            }

            self.keys_by_id.insert(*id, keys);
        }

        // Find conflicts
        for (key, claimants) in key_claims {
            if claimants.len() > 1 {
                self.conflicts.push(KeybindingConflict {
                    key,
                    keybindings: claimants,
                });
            }
        }
    }

    /// Set a single keybinding.
    pub fn set(&mut self, binding_id: KeybindingId, combos: Vec<KeyCombo>) {
        self.user_bindings.insert(binding_id, combos);
        self.rebuild();
    }
}

impl Default for Keybindings {
    fn default() -> Self {
        Self::new()
    }
}

/// Global keybindings manager.
static GLOBAL_KEYBINDINGS: std::sync::OnceLock<std::sync::Mutex<Keybindings>> =
    std::sync::OnceLock::new();

/// Set global keybindings.
pub fn set_keybindings(keybindings: Keybindings) {
    let guard = GLOBAL_KEYBINDINGS.get_or_init(|| std::sync::Mutex::new(Keybindings::new()));
    if let Ok(mut kb) = guard.lock() {
        *kb = keybindings;
    }
}

/// Get global keybindings.
pub fn get_keybindings() -> std::sync::MutexGuard<'static, Keybindings> {
    let guard = GLOBAL_KEYBINDINGS.get_or_init(|| std::sync::Mutex::new(Keybindings::new()));
    guard.lock().expect("Failed to lock global keybindings")
}

/// Predefined keybinding IDs.
pub mod keys {
    use super::KeybindingId;

    pub const INTERRUPT: KeybindingId = "app.interrupt";
    pub const CLEAR: KeybindingId = "app.clear";
    pub const EXIT: KeybindingId = "app.exit";
    pub const MODEL_SELECT: KeybindingId = "app.model.select";
    pub const MODEL_CYCLE_FORWARD: KeybindingId = "app.model.cycleForward";
    pub const TOOLS_EXPAND: KeybindingId = "app.tools.expand";
    pub const THINKING_TOGGLE: KeybindingId = "app.thinking.toggle";
    pub const EXTERNAL_EDITOR: KeybindingId = "app.editor.external";
    pub const THINKING_CYCLE: KeybindingId = "app.thinking.cycle";
    pub const PASTE_IMAGE: KeybindingId = "app.clipboard.pasteImage";
    pub const DEQUEUE: KeybindingId = "app.message.dequeue";

    // Editor
    pub const CURSOR_UP: KeybindingId = "tui.editor.cursorUp";
    pub const CURSOR_DOWN: KeybindingId = "tui.editor.cursorDown";
    pub const CURSOR_LEFT: KeybindingId = "tui.editor.cursorLeft";
    pub const CURSOR_RIGHT: KeybindingId = "tui.editor.cursorRight";
    pub const CURSOR_WORD_LEFT: KeybindingId = "tui.editor.cursorWordLeft";
    pub const CURSOR_WORD_RIGHT: KeybindingId = "tui.editor.cursorWordRight";
    pub const CURSOR_LINE_START: KeybindingId = "tui.editor.cursorLineStart";
    pub const CURSOR_LINE_END: KeybindingId = "tui.editor.cursorLineEnd";
    pub const DELETE_CHAR_BACKWARD: KeybindingId = "tui.editor.deleteCharBackward";
    pub const DELETE_CHAR_FORWARD: KeybindingId = "tui.editor.deleteCharForward";
    pub const DELETE_WORD_BACKWARD: KeybindingId = "tui.editor.deleteWordBackward";
    pub const DELETE_WORD_FORWARD: KeybindingId = "tui.editor.deleteWordForward";
    pub const DELETE_TO_LINE_START: KeybindingId = "tui.editor.deleteToLineStart";
    pub const DELETE_TO_LINE_END: KeybindingId = "tui.editor.deleteToLineEnd";
    pub const YANK: KeybindingId = "tui.editor.yank";
    pub const YANK_POP: KeybindingId = "tui.editor.yankPop";
    pub const UNDO: KeybindingId = "tui.editor.undo";

    // Input
    pub const INPUT_SUBMIT: KeybindingId = "tui.input.submit";
    pub const INPUT_NEW_LINE: KeybindingId = "tui.input.newLine";
    pub const INPUT_TAB: KeybindingId = "tui.input.tab";
    pub const INPUT_COPY: KeybindingId = "tui.input.copy";

    // Select
    pub const SELECT_UP: KeybindingId = "tui.select.up";
    pub const SELECT_DOWN: KeybindingId = "tui.select.down";
    pub const SELECT_PAGE_UP: KeybindingId = "tui.select.pageUp";
    pub const SELECT_PAGE_DOWN: KeybindingId = "tui.select.pageDown";
    pub const SELECT_CONFIRM: KeybindingId = "tui.select.confirm";
    pub const SELECT_CANCEL: KeybindingId = "tui.select.cancel";

    // Alt screen
    pub const PAGE_UP: KeybindingId = "tui.altScreen.pageUp";
    pub const PAGE_DOWN: KeybindingId = "tui.altScreen.pageDown";
    pub const HALF_PAGE_UP: KeybindingId = "tui.altScreen.halfPageUp";
    pub const HALF_PAGE_DOWN: KeybindingId = "tui.altScreen.halfPageDown";
    pub const LINE_UP: KeybindingId = "tui.altScreen.lineUp";
    pub const LINE_DOWN: KeybindingId = "tui.altScreen.lineDown";
    pub const TOP: KeybindingId = "tui.altScreen.top";
    pub const BOTTOM: KeybindingId = "tui.altScreen.bottom";
    pub const SEARCH: KeybindingId = "tui.altScreen.search";
    pub const SEARCH_NEXT: KeybindingId = "tui.altScreen.searchNext";
    pub const SEARCH_PREVIOUS: KeybindingId = "tui.altScreen.searchPrevious";
    pub const SEARCH_CLOSE: KeybindingId = "tui.altScreen.searchClose";
    pub const PREVIOUS_PROMPT: KeybindingId = "tui.altScreen.previousPrompt";
    pub const NEXT_PROMPT: KeybindingId = "tui.altScreen.nextPrompt";
}

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

    #[test]
    fn test_keybindings_new() {
        let kb = Keybindings::new();
        assert!(kb.definitions.len() > 0);
    }

    #[test]
    fn test_matches() {
        let kb = Keybindings::new();
        let event = crossterm::event::KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
        assert!(kb.matches(&event, keys::INPUT_SUBMIT));
    }

    #[test]
    fn test_get_keys() {
        let kb = Keybindings::new();
        let keys = kb.get_keys(keys::CURSOR_LEFT);
        assert!(!keys.is_empty());
    }

    #[test]
    fn key_combo_display_formats_hints() {
        assert_eq!(
            KeyCombo::new(KeyCode::Char('q'), KeyModifiers::ALT).display(),
            "Alt+Q"
        );
        assert_eq!(
            KeyCombo::new(
                KeyCode::Char('f'),
                KeyModifiers::CONTROL | KeyModifiers::SHIFT
            )
            .display(),
            "Ctrl+Shift+F"
        );
        assert_eq!(
            KeyCombo::new(KeyCode::Enter, KeyModifiers::NONE).display(),
            "Enter"
        );
        assert_eq!(
            KeyCombo::new(KeyCode::Up, KeyModifiers::ALT).display(),
            "Alt+Up"
        );
    }

    #[test]
    fn dequeue_binding_has_a_platform_default() {
        let kb = Keybindings::new();
        let keys = kb.get_keys(keys::DEQUEUE);
        assert_eq!(keys.len(), 1);
        if cfg!(windows) {
            assert_eq!(keys[0].display(), "Alt+Q");
        } else {
            assert_eq!(keys[0].display(), "Alt+Up");
        }
    }

    #[test]
    fn dequeue_matches_its_platform_default_event() {
        // Guards the hotkey itself: a real Alt+Q (Windows) / Alt+Up (elsewhere)
        // event must resolve to `app.message.dequeue`, since the TUI key loop
        // routes the "edit queued messages" action solely through this match.
        let kb = Keybindings::new();
        let event = if cfg!(windows) {
            crossterm::event::KeyEvent::new(KeyCode::Char('q'), KeyModifiers::ALT)
        } else {
            crossterm::event::KeyEvent::new(KeyCode::Up, KeyModifiers::ALT)
        };
        assert!(kb.matches(&event, keys::DEQUEUE));
    }
}