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    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(
52                "'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'",
53            ),
54        }
55    }
56}
57
58impl UpdateFromValue for NuCursorShape {
59    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
60        config_update_string_enum(self, value, path, errors)
61    }
62}
63
64#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
65pub struct CursorShapeConfig {
66    pub emacs: NuCursorShape,
67    pub vi_insert: NuCursorShape,
68    pub vi_normal: NuCursorShape,
69}
70
71impl UpdateFromValue for CursorShapeConfig {
72    fn update<'a>(
73        &mut self,
74        value: &'a Value,
75        path: &mut ConfigPath<'a>,
76        errors: &mut ConfigErrors,
77    ) {
78        let Value::Record { val: record, .. } = value else {
79            errors.type_mismatch(path, Type::record(), value);
80            return;
81        };
82
83        for (col, val) in record.iter() {
84            let path = &mut path.push(col);
85            match col.as_str() {
86                "vi_insert" => self.vi_insert.update(val, path, errors),
87                "vi_normal" => self.vi_normal.update(val, path, errors),
88                "emacs" => self.emacs.update(val, path, errors),
89                _ => errors.unknown_option(path, val),
90            }
91        }
92    }
93}
94
95#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
96pub enum EditBindings {
97    Vi,
98    #[default]
99    Emacs,
100}
101
102impl FromStr for EditBindings {
103    type Err = &'static str;
104
105    fn from_str(s: &str) -> Result<Self, Self::Err> {
106        match s.to_ascii_lowercase().as_str() {
107            "vi" => Ok(Self::Vi),
108            "emacs" => Ok(Self::Emacs),
109            _ => Err("'emacs' or 'vi'"),
110        }
111    }
112}
113
114impl UpdateFromValue for EditBindings {
115    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
116        config_update_string_enum(self, value, path, errors)
117    }
118}