Skip to main content

nu_protocol/config/
reedline.rs

1use super::{config_update_string_enum, prelude::*};
2use crate as nu_protocol;
3use crate::{FromValue, engine::Closure};
4
5/// Definition of a parsed keybinding from the config object
6#[derive(Clone, Debug, FromValue, IntoValue, Serialize, Deserialize)]
7pub struct ParsedKeybinding {
8    pub name: Option<Value>,
9    pub modifier: Value,
10    pub keycode: Value,
11    pub event: Value,
12    pub mode: Value,
13}
14
15/// Definition of a parsed menu from the config object
16#[derive(Clone, Debug, FromValue, IntoValue, Serialize, Deserialize)]
17pub struct ParsedMenu {
18    pub name: Value,
19    pub marker: Value,
20    /// Legacy two-state input behavior. Required unless `input_mode` is set,
21    /// which supersedes it.
22    pub only_buffer_difference: Option<Value>,
23    /// Optional reedline `InputMode` ("diff" / "cursor_prefix" / "full_buffer").
24    /// Supersedes `only_buffer_difference` when set; absent keeps current behavior.
25    pub input_mode: Option<Value>,
26    /// Optional reedline `OutputMode` ("suggested_span" / "full_buffer" / "extend_to_end").
27    pub output_mode: Option<Value>,
28    pub style: Value,
29    pub r#type: Value,
30    pub source: Option<Closure>,
31}
32
33/// Definition of a Nushell CursorShape (to be mapped to crossterm::cursor::CursorShape)
34#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
35pub enum NuCursorShape {
36    Underscore,
37    Line,
38    Block,
39    BlinkUnderscore,
40    BlinkLine,
41    BlinkBlock,
42    #[default]
43    Inherit,
44}
45
46impl FromStr for NuCursorShape {
47    type Err = &'static str;
48
49    fn from_str(s: &str) -> Result<NuCursorShape, &'static str> {
50        match s.to_ascii_lowercase().as_str() {
51            "line" => Ok(NuCursorShape::Line),
52            "block" => Ok(NuCursorShape::Block),
53            "underscore" => Ok(NuCursorShape::Underscore),
54            "blink_line" => Ok(NuCursorShape::BlinkLine),
55            "blink_block" => Ok(NuCursorShape::BlinkBlock),
56            "blink_underscore" => Ok(NuCursorShape::BlinkUnderscore),
57            "inherit" => Ok(NuCursorShape::Inherit),
58            _ => Err(
59                "'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'",
60            ),
61        }
62    }
63}
64
65impl UpdateFromValue for NuCursorShape {
66    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
67        config_update_string_enum(self, value, path, errors)
68    }
69}
70
71#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
72pub struct CursorShapeConfig {
73    pub emacs: NuCursorShape,
74    pub vi_insert: NuCursorShape,
75    pub vi_normal: NuCursorShape,
76}
77
78impl UpdateFromValue for CursorShapeConfig {
79    fn update<'a>(
80        &mut self,
81        value: &'a Value,
82        path: &mut ConfigPath<'a>,
83        errors: &mut ConfigErrors,
84    ) {
85        let Value::Record { val: record, .. } = value else {
86            errors.type_mismatch(path, Type::record(), value);
87            return;
88        };
89
90        for (col, val) in record.iter() {
91            let path = &mut path.push(col);
92            match col.as_str() {
93                "vi_insert" => self.vi_insert.update(val, path, errors),
94                "vi_normal" => self.vi_normal.update(val, path, errors),
95                "emacs" => self.emacs.update(val, path, errors),
96                _ => errors.unknown_option(path, val),
97            }
98        }
99    }
100}
101
102#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
103pub enum EditBindings {
104    Vi,
105    #[default]
106    Emacs,
107}
108
109impl FromStr for EditBindings {
110    type Err = &'static str;
111
112    fn from_str(s: &str) -> Result<Self, Self::Err> {
113        match s.to_ascii_lowercase().as_str() {
114            "vi" => Ok(Self::Vi),
115            "emacs" => Ok(Self::Emacs),
116            _ => Err("'emacs' or 'vi'"),
117        }
118    }
119}
120
121impl UpdateFromValue for EditBindings {
122    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
123        config_update_string_enum(self, value, path, errors)
124    }
125}