nu_protocol/config/
reedline.rs

1use super::{config_update_string_enum, prelude::*};
2use crate as nu_protocol;
3use crate::{engine::Closure, FromValue};
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    pub only_buffer_difference: Value,
21    pub style: Value,
22    pub r#type: Value,
23    pub source: Option<Closure>,
24}
25
26/// Definition of a Nushell CursorShape (to be mapped to crossterm::cursor::CursorShape)
27#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
28pub enum NuCursorShape {
29    Underscore,
30    Line,
31    Block,
32    BlinkUnderscore,
33    BlinkLine,
34    BlinkBlock,
35    #[default]
36    Inherit,
37}
38
39impl FromStr for NuCursorShape {
40    type Err = &'static str;
41
42    fn from_str(s: &str) -> Result<NuCursorShape, &'static str> {
43        match s.to_ascii_lowercase().as_str() {
44        "line" => Ok(NuCursorShape::Line),
45        "block" => Ok(NuCursorShape::Block),
46        "underscore" => Ok(NuCursorShape::Underscore),
47        "blink_line" => Ok(NuCursorShape::BlinkLine),
48        "blink_block" => Ok(NuCursorShape::BlinkBlock),
49        "blink_underscore" => Ok(NuCursorShape::BlinkUnderscore),
50        "inherit" => Ok(NuCursorShape::Inherit),
51        _ => Err("'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"),
52        }
53    }
54}
55
56impl UpdateFromValue for NuCursorShape {
57    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
58        config_update_string_enum(self, value, path, errors)
59    }
60}
61
62#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
63pub struct CursorShapeConfig {
64    pub emacs: NuCursorShape,
65    pub vi_insert: NuCursorShape,
66    pub vi_normal: NuCursorShape,
67}
68
69impl UpdateFromValue for CursorShapeConfig {
70    fn update<'a>(
71        &mut self,
72        value: &'a Value,
73        path: &mut ConfigPath<'a>,
74        errors: &mut ConfigErrors,
75    ) {
76        let Value::Record { val: record, .. } = value else {
77            errors.type_mismatch(path, Type::record(), value);
78            return;
79        };
80
81        for (col, val) in record.iter() {
82            let path = &mut path.push(col);
83            match col.as_str() {
84                "vi_insert" => self.vi_insert.update(val, path, errors),
85                "vi_normal" => self.vi_normal.update(val, path, errors),
86                "emacs" => self.emacs.update(val, path, errors),
87                _ => errors.unknown_option(path, val),
88            }
89        }
90    }
91}
92
93#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
94pub enum EditBindings {
95    Vi,
96    #[default]
97    Emacs,
98}
99
100impl FromStr for EditBindings {
101    type Err = &'static str;
102
103    fn from_str(s: &str) -> Result<Self, Self::Err> {
104        match s.to_ascii_lowercase().as_str() {
105            "vi" => Ok(Self::Vi),
106            "emacs" => Ok(Self::Emacs),
107            _ => Err("'emacs' or 'vi'"),
108        }
109    }
110}
111
112impl UpdateFromValue for EditBindings {
113    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
114        config_update_string_enum(self, value, path, errors)
115    }
116}