bubbletea_widgets/textinput/
keymap.rs

1//! Key bindings for the textinput component.
2
3use crate::key::{new_binding, with_keys_str, Binding};
4
5/// KeyMap is the key bindings for different actions within the textinput.
6/// Matches Go's KeyMap struct exactly.
7#[derive(Debug, Clone)]
8pub struct KeyMap {
9    /// Move cursor one character right.
10    pub character_forward: Binding,
11    /// Move cursor one character left.
12    pub character_backward: Binding,
13    /// Move cursor one word right.
14    pub word_forward: Binding,
15    /// Move cursor one word left.
16    pub word_backward: Binding,
17    /// Delete the previous word.
18    pub delete_word_backward: Binding,
19    /// Delete the next word.
20    pub delete_word_forward: Binding,
21    /// Delete from cursor to end of line.
22    pub delete_after_cursor: Binding,
23    /// Delete from start of line to cursor.
24    pub delete_before_cursor: Binding,
25    /// Delete one character backward.
26    pub delete_character_backward: Binding,
27    /// Delete one character forward.
28    pub delete_character_forward: Binding,
29    /// Move to start of line.
30    pub line_start: Binding,
31    /// Move to end of line.
32    pub line_end: Binding,
33    /// Paste from clipboard.
34    pub paste: Binding,
35    /// Accept the current suggestion.
36    pub accept_suggestion: Binding,
37    /// Move to the next suggestion.
38    pub next_suggestion: Binding,
39    /// Move to the previous suggestion.
40    pub prev_suggestion: Binding,
41}
42
43/// DefaultKeyMap is the default set of key bindings for navigating and acting
44/// upon the textinput. Matches Go's DefaultKeyMap exactly.
45pub fn default_key_map() -> KeyMap {
46    KeyMap {
47        character_forward: new_binding(vec![with_keys_str(&["right", "ctrl+f"])]),
48        character_backward: new_binding(vec![with_keys_str(&["left", "ctrl+b"])]),
49        word_forward: new_binding(vec![with_keys_str(&["alt+right", "ctrl+right", "alt+f"])]),
50        word_backward: new_binding(vec![with_keys_str(&["alt+left", "ctrl+left", "alt+b"])]),
51        delete_word_backward: new_binding(vec![with_keys_str(&["alt+backspace", "ctrl+w"])]),
52        delete_word_forward: new_binding(vec![with_keys_str(&["alt+delete", "alt+d"])]),
53        delete_after_cursor: new_binding(vec![with_keys_str(&["ctrl+k"])]),
54        delete_before_cursor: new_binding(vec![with_keys_str(&["ctrl+u"])]),
55        delete_character_backward: new_binding(vec![with_keys_str(&["backspace", "ctrl+h"])]),
56        delete_character_forward: new_binding(vec![with_keys_str(&["delete", "ctrl+d"])]),
57        line_start: new_binding(vec![with_keys_str(&["home", "ctrl+a"])]),
58        line_end: new_binding(vec![with_keys_str(&["end", "ctrl+e"])]),
59        paste: new_binding(vec![with_keys_str(&["ctrl+v"])]),
60        accept_suggestion: new_binding(vec![with_keys_str(&["tab"])]),
61        next_suggestion: new_binding(vec![with_keys_str(&["down", "ctrl+n"])]),
62        prev_suggestion: new_binding(vec![with_keys_str(&["up", "ctrl+p"])]),
63    }
64}