1use crate::FromValue;
4use crate::{self as nu_protocol};
5use helper::*;
6use prelude::*;
7use std::collections::HashMap;
8
9pub use ansi_coloring::UseAnsiColoring;
10pub use clip::ClipConfig;
11pub use completions::{
12 CompletionAlgorithm, CompletionConfig, CompletionSort, ExternalCompleterConfig,
13};
14pub use datetime_format::DatetimeFormatConfig;
15pub use display_errors::DisplayErrors;
16pub use filesize::FilesizeConfig;
17pub use helper::extract_value;
18pub use hinter::HinterConfig;
19pub use history::{HistoryConfig, HistoryFileFormat, HistoryPath};
20pub use hooks::Hooks;
21pub use ls::LsConfig;
22pub use output::{BannerKind, ErrorStyle};
23pub use plugin_gc::{PluginGcConfig, PluginGcConfigs};
24pub use reedline::{CursorShapeConfig, EditBindings, NuCursorShape, ParsedKeybinding, ParsedMenu};
25pub use rm::RmConfig;
26pub use shell_integration::ShellIntegrationConfig;
27pub use table::{FooterMode, TableConfig, TableIndent, TableIndexMode, TableMode, TrimStrategy};
28
29mod ansi_coloring;
30mod clip;
31mod completions;
32mod datetime_format;
33mod display_errors;
34mod error;
35mod filesize;
36mod helper;
37mod hinter;
38mod history;
39mod hooks;
40mod ls;
41mod output;
42mod plugin_gc;
43mod prelude;
44mod reedline;
45mod rm;
46mod shell_integration;
47mod table;
48
49#[derive(Clone, Debug, IntoValue, Serialize, Deserialize)]
50pub struct Config {
51 pub filesize: FilesizeConfig,
52 pub table: TableConfig,
53 pub ls: LsConfig,
54 pub clip: ClipConfig,
55 pub color_config: HashMap<String, Value>,
56 pub footer_mode: FooterMode,
57 pub float_precision: i64,
58 pub recursion_limit: i64,
59 pub use_ansi_coloring: UseAnsiColoring,
60 pub completions: CompletionConfig,
61 pub edit_mode: EditBindings,
62 pub show_hints: bool,
63 pub hinter: HinterConfig,
64 pub history: HistoryConfig,
65 pub keybindings: Vec<ParsedKeybinding>,
66 pub menus: Vec<ParsedMenu>,
67 pub hooks: Hooks,
68 pub rm: RmConfig,
69 pub shell_integration: ShellIntegrationConfig,
70 pub buffer_editor: Value,
71 pub show_banner: BannerKind,
72 pub bracketed_paste: bool,
73 pub render_right_prompt_on_last_line: bool,
74 pub explore: HashMap<String, Value>,
75 pub cursor_shape: CursorShapeConfig,
76 pub datetime_format: DatetimeFormatConfig,
77 pub error_style: ErrorStyle,
78 pub error_lines: i64,
79 pub display_errors: DisplayErrors,
80 pub use_kitty_protocol: bool,
81 pub highlight_resolved_externals: bool,
82 pub plugins: HashMap<String, Value>,
88 pub plugin_gc: PluginGcConfigs,
90}
91
92impl Default for Config {
93 fn default() -> Config {
94 Config {
95 show_banner: BannerKind::default(),
96
97 table: TableConfig::default(),
98 rm: RmConfig::default(),
99 ls: LsConfig::default(),
100
101 datetime_format: DatetimeFormatConfig::default(),
102
103 explore: HashMap::new(),
104
105 history: HistoryConfig::default(),
106
107 completions: CompletionConfig::default(),
108
109 recursion_limit: 50,
110
111 filesize: FilesizeConfig::default(),
112
113 cursor_shape: CursorShapeConfig::default(),
114
115 clip: ClipConfig::default(),
116
117 color_config: HashMap::new(),
118 footer_mode: FooterMode::RowCount(25),
119 float_precision: 2,
120 buffer_editor: Value::nothing(Span::unknown()),
121 use_ansi_coloring: UseAnsiColoring::default(),
122 bracketed_paste: true,
123 edit_mode: EditBindings::default(),
124 show_hints: true,
125 hinter: HinterConfig::default(),
126
127 shell_integration: ShellIntegrationConfig::default(),
128
129 render_right_prompt_on_last_line: false,
130
131 hooks: Hooks::new(),
132
133 menus: Vec::new(),
134
135 keybindings: Vec::new(),
136
137 error_style: ErrorStyle::default(),
138 error_lines: 1,
139 display_errors: DisplayErrors::default(),
140
141 use_kitty_protocol: false,
142 highlight_resolved_externals: false,
143
144 plugins: HashMap::new(),
145 plugin_gc: PluginGcConfigs::default(),
146 }
147 }
148}
149
150impl UpdateFromValue for Config {
151 fn update<'a>(
152 &mut self,
153 value: &'a Value,
154 path: &mut ConfigPath<'a>,
155 errors: &mut ConfigErrors,
156 ) {
157 let Value::Record { val: record, .. } = value else {
158 errors.type_mismatch(path, Type::record(), value);
159 return;
160 };
161
162 for (col, val) in record.iter() {
163 let path = &mut path.push(col);
164 match col.as_str() {
165 "ls" => self.ls.update(val, path, errors),
166 "rm" => self.rm.update(val, path, errors),
167 "history" => self.history.update(val, path, errors),
168 "completions" => self.completions.update(val, path, errors),
169 "cursor_shape" => self.cursor_shape.update(val, path, errors),
170 "table" => self.table.update(val, path, errors),
171 "filesize" => self.filesize.update(val, path, errors),
172 "explore" => self.explore.update(val, path, errors),
173 "color_config" => self.color_config.update(val, path, errors),
174 "clip" => self.clip.update(val, path, errors),
175 "footer_mode" => self.footer_mode.update(val, path, errors),
176 "float_precision" => self.float_precision.update(val, path, errors),
177 "use_ansi_coloring" => self.use_ansi_coloring.update(val, path, errors),
178 "edit_mode" => self.edit_mode.update(val, path, errors),
179 "show_hints" => self.show_hints.update(val, path, errors),
180 "hinter" => self.hinter.update(val, path, errors),
181 "shell_integration" => self.shell_integration.update(val, path, errors),
182 "buffer_editor" => match val {
183 Value::Nothing { .. } | Value::String { .. } => {
184 self.buffer_editor = val.clone();
185 }
186 Value::List { vals, .. }
187 if vals.iter().all(|val| matches!(val, Value::String { .. })) =>
188 {
189 self.buffer_editor = val.clone();
190 }
191 _ => errors.type_mismatch(
192 path,
193 Type::custom("string, list<string>, or nothing"),
194 val,
195 ),
196 },
197 "show_banner" => self.show_banner.update(val, path, errors),
198 "display_errors" => self.display_errors.update(val, path, errors),
199 "render_right_prompt_on_last_line" => self
200 .render_right_prompt_on_last_line
201 .update(val, path, errors),
202 "bracketed_paste" => self.bracketed_paste.update(val, path, errors),
203 "use_kitty_protocol" => self.use_kitty_protocol.update(val, path, errors),
204 "highlight_resolved_externals" => {
205 self.highlight_resolved_externals.update(val, path, errors)
206 }
207 "plugins" => self.plugins.update(val, path, errors),
208 "plugin_gc" => self.plugin_gc.update(val, path, errors),
209 "menus" => match Vec::from_value(val.clone()) {
210 Ok(menus) => self.menus = menus,
211 Err(err) => errors.error(err.into()),
212 },
213 "keybindings" => match Vec::from_value(val.clone()) {
214 Ok(keybindings) => self.keybindings = keybindings,
215 Err(err) => errors.error(err.into()),
216 },
217 "hooks" => self.hooks.update(val, path, errors),
218 "datetime_format" => self.datetime_format.update(val, path, errors),
219 "error_style" => self.error_style.update(val, path, errors),
220 "error_lines" => {
221 if let Ok(lines) = val.as_int() {
222 if lines >= 0 {
223 self.error_lines = lines;
224 } else {
225 errors.invalid_value(path, "an int greater than or equal to 0", val);
226 }
227 } else {
228 errors.type_mismatch(path, Type::Int, val);
229 }
230 }
231 "recursion_limit" => {
232 if let Ok(limit) = val.as_int() {
233 if limit > 1 {
234 self.recursion_limit = limit;
235 } else {
236 errors.invalid_value(path, "an int greater than 1", val);
237 }
238 } else {
239 errors.type_mismatch(path, Type::Int, val);
240 }
241 }
242 _ => errors.unknown_option(path, val),
243 }
244 }
245 }
246}
247
248impl Config {
249 pub fn update_from_value(
250 &mut self,
251 old: &Config,
252 value: &Value,
253 ) -> Result<Option<ShellWarning>, ShellError> {
254 let mut errors = ConfigErrors::new(old);
258 let mut path = ConfigPath::new();
259
260 self.update(value, &mut path, &mut errors);
261
262 errors.check()
263 }
264}