revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Extended keymap utilities
//!
//! Provides configurable key binding management for TUI applications.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::utils::keymap::{KeymapConfig, Mode, bind};
//!
//! let mut keymap = KeymapConfig::new();
//!
//! // Add mode-specific bindings
//! keymap.bind(Mode::Normal, "j", "move_down");
//! keymap.bind(Mode::Normal, "k", "move_up");
//! keymap.bind(Mode::Insert, "Escape", "exit_insert");
//!
//! // Parse and execute
//! keymap.set_mode(Mode::Normal);
//! let action = keymap.lookup("j");
//! ```

use crate::event::{Key, KeyBinding};
use std::collections::HashMap;

/// Input mode
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Mode {
    /// Normal mode (navigation)
    #[default]
    Normal,
    /// Insert mode (text input)
    Insert,
    /// Visual mode (selection)
    Visual,
    /// Command mode (ex commands)
    Command,
    /// Search mode
    Search,
    /// Custom mode
    Custom(u8),
}

impl Mode {
    /// Get mode name
    pub fn name(&self) -> &'static str {
        match self {
            Mode::Normal => "NORMAL",
            Mode::Insert => "INSERT",
            Mode::Visual => "VISUAL",
            Mode::Command => "COMMAND",
            Mode::Search => "SEARCH",
            Mode::Custom(_) => "CUSTOM",
        }
    }
}

/// Key chord (multiple keys)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct KeyChord {
    /// Keys in the chord
    pub keys: Vec<KeyBinding>,
}

impl KeyChord {
    /// Create single key chord
    pub fn single(key: KeyBinding) -> Self {
        Self { keys: vec![key] }
    }

    /// Create multi-key chord
    pub fn multi(keys: Vec<KeyBinding>) -> Self {
        Self { keys }
    }

    /// Parse from string (e.g., "Ctrl-x Ctrl-s")
    pub fn parse(s: &str) -> Option<Self> {
        let parts: Vec<&str> = s.split_whitespace().collect();
        if parts.is_empty() {
            return None;
        }

        let keys: Option<Vec<KeyBinding>> = parts.iter().map(|p| parse_key_binding(p)).collect();
        keys.map(|k| Self { keys: k })
    }
}

/// Parse a single key binding string
pub fn parse_key_binding(s: &str) -> Option<KeyBinding> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }

    let mut ctrl = false;
    let mut alt = false;
    let mut shift = false;
    let mut key_part = s;

    // Parse modifiers
    loop {
        let lower = key_part.to_lowercase();
        if lower.starts_with("ctrl-") || lower.starts_with("c-") {
            ctrl = true;
            key_part = if lower.starts_with("ctrl-") {
                &key_part[5..]
            } else {
                &key_part[2..]
            };
        } else if lower.starts_with("alt-") || lower.starts_with("m-") {
            alt = true;
            key_part = if lower.starts_with("alt-") {
                &key_part[4..]
            } else {
                &key_part[2..]
            };
        } else if lower.starts_with("shift-") || lower.starts_with("s-") {
            shift = true;
            key_part = if lower.starts_with("shift-") {
                &key_part[6..]
            } else {
                &key_part[2..]
            };
        } else {
            break;
        }
    }

    let key = parse_key(key_part)?;

    Some(KeyBinding {
        key,
        ctrl,
        alt,
        shift,
    })
}

/// Parse key name to Key enum
fn parse_key(s: &str) -> Option<Key> {
    let lower = s.to_lowercase();
    match lower.as_str() {
        "enter" | "return" | "cr" => Some(Key::Enter),
        "escape" | "esc" => Some(Key::Escape),
        "tab" => Some(Key::Tab),
        "backtab" | "s-tab" => Some(Key::BackTab),
        "backspace" | "bs" => Some(Key::Backspace),
        "delete" | "del" => Some(Key::Delete),
        "up" => Some(Key::Up),
        "down" => Some(Key::Down),
        "left" => Some(Key::Left),
        "right" => Some(Key::Right),
        "home" => Some(Key::Home),
        "end" => Some(Key::End),
        "pageup" | "pgup" => Some(Key::PageUp),
        "pagedown" | "pgdn" => Some(Key::PageDown),
        "insert" | "ins" => Some(Key::Insert),
        "space" => Some(Key::Char(' ')),
        "f1" => Some(Key::F(1)),
        "f2" => Some(Key::F(2)),
        "f3" => Some(Key::F(3)),
        "f4" => Some(Key::F(4)),
        "f5" => Some(Key::F(5)),
        "f6" => Some(Key::F(6)),
        "f7" => Some(Key::F(7)),
        "f8" => Some(Key::F(8)),
        "f9" => Some(Key::F(9)),
        "f10" => Some(Key::F(10)),
        "f11" => Some(Key::F(11)),
        "f12" => Some(Key::F(12)),
        _ => {
            // Single character
            let chars: Vec<char> = s.chars().collect();
            if chars.len() == 1 {
                Some(Key::Char(chars[0]))
            } else {
                None
            }
        }
    }
}

/// Format a key binding for display
pub fn format_key_binding(binding: &KeyBinding) -> String {
    let mut parts = Vec::new();

    if binding.ctrl {
        parts.push("Ctrl");
    }
    if binding.alt {
        parts.push("Alt");
    }
    if binding.shift {
        parts.push("Shift");
    }

    let key_str = match binding.key {
        Key::Char(' ') => "Space".to_string(),
        Key::Char(c) => c.to_string(),
        Key::Enter => "Enter".to_string(),
        Key::Escape => "Esc".to_string(),
        Key::Tab => "Tab".to_string(),
        Key::BackTab => "BackTab".to_string(),
        Key::Backspace => "Backspace".to_string(),
        Key::Delete => "Del".to_string(),
        Key::Up => "".to_string(),
        Key::Down => "".to_string(),
        Key::Left => "".to_string(),
        Key::Right => "".to_string(),
        Key::Home => "Home".to_string(),
        Key::End => "End".to_string(),
        Key::PageUp => "PgUp".to_string(),
        Key::PageDown => "PgDn".to_string(),
        Key::Insert => "Ins".to_string(),
        Key::F(n) => format!("F{}", n),
        Key::Null => "Null".to_string(),
        Key::Unknown => "Unknown".to_string(),
    };

    parts.push(&key_str);
    parts.join("-")
}

/// Keymap configuration
#[derive(Clone, Debug)]
pub struct KeymapConfig {
    /// Mode-specific bindings
    bindings: HashMap<Mode, HashMap<KeyChord, String>>,
    /// Current mode
    current_mode: Mode,
    /// Pending keys for multi-key chords
    pending: Vec<KeyBinding>,
    /// Timeout for multi-key chords (ms)
    chord_timeout: u64,
    /// Global bindings (active in all modes)
    global_bindings: HashMap<KeyChord, String>,
}

impl KeymapConfig {
    /// Create new keymap config
    pub fn new() -> Self {
        Self {
            bindings: HashMap::new(),
            current_mode: Mode::Normal,
            pending: Vec::new(),
            chord_timeout: 1000,
            global_bindings: HashMap::new(),
        }
    }

    /// Set current mode
    pub fn set_mode(&mut self, mode: Mode) {
        self.current_mode = mode;
        self.pending.clear();
    }

    /// Get current mode
    pub fn mode(&self) -> Mode {
        self.current_mode
    }

    /// Add a binding to a specific mode
    pub fn bind(&mut self, mode: Mode, keys: &str, action: impl Into<String>) {
        if let Some(chord) = KeyChord::parse(keys) {
            self.bindings
                .entry(mode)
                .or_default()
                .insert(chord, action.into());
        }
    }

    /// Add a global binding (all modes)
    pub fn bind_global(&mut self, keys: &str, action: impl Into<String>) {
        if let Some(chord) = KeyChord::parse(keys) {
            self.global_bindings.insert(chord, action.into());
        }
    }

    /// Remove a binding
    pub fn unbind(&mut self, mode: Mode, keys: &str) {
        if let Some(chord) = KeyChord::parse(keys) {
            if let Some(mode_bindings) = self.bindings.get_mut(&mode) {
                mode_bindings.remove(&chord);
            }
        }
    }

    /// Look up action for a key
    pub fn lookup(&mut self, key: KeyBinding) -> LookupResult {
        self.pending.push(key);

        let chord = KeyChord {
            keys: self.pending.clone(),
        };

        // Check global bindings first
        if let Some(action) = self.global_bindings.get(&chord) {
            self.pending.clear();
            return LookupResult::Action(action.clone());
        }

        // Check mode-specific bindings
        if let Some(mode_bindings) = self.bindings.get(&self.current_mode) {
            if let Some(action) = mode_bindings.get(&chord) {
                self.pending.clear();
                return LookupResult::Action(action.clone());
            }

            // Check if this could be a prefix of a longer chord
            for existing_chord in mode_bindings.keys() {
                if existing_chord.keys.len() > self.pending.len()
                    && existing_chord.keys.starts_with(&self.pending)
                {
                    return LookupResult::Pending;
                }
            }
        }

        // No match and no prefix match
        self.pending.clear();
        LookupResult::None
    }

    /// Clear pending keys
    pub fn clear_pending(&mut self) {
        self.pending.clear();
    }

    /// Get pending keys
    pub fn pending_keys(&self) -> &[KeyBinding] {
        &self.pending
    }

    /// Check if there are pending keys
    pub fn has_pending(&self) -> bool {
        !self.pending.is_empty()
    }

    /// Set chord timeout
    pub fn chord_timeout(&mut self, ms: u64) {
        self.chord_timeout = ms;
    }

    /// Get all bindings for a mode
    pub fn bindings_for_mode(&self, mode: Mode) -> Vec<(&KeyChord, &str)> {
        self.bindings
            .get(&mode)
            .map(|m| m.iter().map(|(k, v)| (k, v.as_str())).collect())
            .unwrap_or_default()
    }

    /// Get all global bindings
    pub fn global_bindings(&self) -> Vec<(&KeyChord, &str)> {
        self.global_bindings
            .iter()
            .map(|(k, v)| (k, v.as_str()))
            .collect()
    }
}

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

/// Result of key lookup
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LookupResult {
    /// No matching binding
    None,
    /// Matched an action
    Action(String),
    /// Could be part of a longer chord, waiting for more keys
    Pending,
}

/// Vim-style keymap preset
pub fn vim_preset() -> KeymapConfig {
    let mut config = KeymapConfig::new();

    // Normal mode
    config.bind(Mode::Normal, "h", "move_left");
    config.bind(Mode::Normal, "j", "move_down");
    config.bind(Mode::Normal, "k", "move_up");
    config.bind(Mode::Normal, "l", "move_right");
    config.bind(Mode::Normal, "i", "enter_insert");
    config.bind(Mode::Normal, "a", "append");
    config.bind(Mode::Normal, "A", "append_end");
    config.bind(Mode::Normal, "o", "open_below");
    config.bind(Mode::Normal, "O", "open_above");
    config.bind(Mode::Normal, "v", "enter_visual");
    config.bind(Mode::Normal, ":", "enter_command");
    config.bind(Mode::Normal, "/", "search_forward");
    config.bind(Mode::Normal, "?", "search_backward");
    config.bind(Mode::Normal, "n", "search_next");
    config.bind(Mode::Normal, "N", "search_prev");
    config.bind(Mode::Normal, "g g", "goto_first");
    config.bind(Mode::Normal, "G", "goto_last");
    config.bind(Mode::Normal, "Ctrl-u", "page_up");
    config.bind(Mode::Normal, "Ctrl-d", "page_down");
    config.bind(Mode::Normal, "d d", "delete_line");
    config.bind(Mode::Normal, "y y", "yank_line");
    config.bind(Mode::Normal, "p", "paste_after");
    config.bind(Mode::Normal, "P", "paste_before");
    config.bind(Mode::Normal, "u", "undo");
    config.bind(Mode::Normal, "Ctrl-r", "redo");

    // Insert mode
    config.bind(Mode::Insert, "Escape", "exit_insert");
    config.bind(Mode::Insert, "Ctrl-c", "exit_insert");

    // Visual mode
    config.bind(Mode::Visual, "Escape", "exit_visual");
    config.bind(Mode::Visual, "h", "extend_left");
    config.bind(Mode::Visual, "j", "extend_down");
    config.bind(Mode::Visual, "k", "extend_up");
    config.bind(Mode::Visual, "l", "extend_right");
    config.bind(Mode::Visual, "y", "yank_selection");
    config.bind(Mode::Visual, "d", "delete_selection");

    // Command mode
    config.bind(Mode::Command, "Escape", "exit_command");
    config.bind(Mode::Command, "Enter", "execute_command");

    // Global
    config.bind_global("Ctrl-c", "quit");
    config.bind_global("Ctrl-z", "suspend");

    config
}

/// Emacs-style keymap preset
pub fn emacs_preset() -> KeymapConfig {
    let mut config = KeymapConfig::new();

    // Navigation
    config.bind(Mode::Normal, "Ctrl-p", "move_up");
    config.bind(Mode::Normal, "Ctrl-n", "move_down");
    config.bind(Mode::Normal, "Ctrl-b", "move_left");
    config.bind(Mode::Normal, "Ctrl-f", "move_right");
    config.bind(Mode::Normal, "Ctrl-a", "line_start");
    config.bind(Mode::Normal, "Ctrl-e", "line_end");
    config.bind(Mode::Normal, "Alt-<", "goto_first");
    config.bind(Mode::Normal, "Alt->", "goto_last");
    config.bind(Mode::Normal, "Ctrl-v", "page_down");
    config.bind(Mode::Normal, "Alt-v", "page_up");

    // Editing
    config.bind(Mode::Normal, "Ctrl-d", "delete_char");
    config.bind(Mode::Normal, "Ctrl-k", "kill_line");
    config.bind(Mode::Normal, "Ctrl-y", "yank");
    config.bind(Mode::Normal, "Ctrl-w", "cut_region");
    config.bind(Mode::Normal, "Alt-w", "copy_region");

    // Search
    config.bind(Mode::Normal, "Ctrl-s", "search_forward");
    config.bind(Mode::Normal, "Ctrl-r", "search_backward");

    // Undo
    config.bind(Mode::Normal, "Ctrl-/", "undo");
    config.bind(Mode::Normal, "Ctrl-x u", "undo");

    // File operations
    config.bind(Mode::Normal, "Ctrl-x Ctrl-s", "save");
    config.bind(Mode::Normal, "Ctrl-x Ctrl-c", "quit");
    config.bind(Mode::Normal, "Ctrl-x Ctrl-f", "open_file");

    config
}