nu-protocol 0.115.0

Nushell's internal protocols, including its abstract syntax tree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Module containing the internal representation of user configuration

use crate::FromValue;
use crate::{self as nu_protocol, Filesize};
use helper::*;
use prelude::*;
use std::collections::HashMap;

pub use ansi_coloring::UseAnsiColoring;
pub use clip::ClipConfig;
pub use completions::{
    CompletionAlgorithm, CompletionConfig, CompletionSort, ExternalCompleterConfig,
};
pub use datetime_format::DatetimeFormatConfig;
pub use defaults::default_color_config;
pub use display_errors::DisplayErrors;
pub use duration_max_unit::DurationMaxUnit;
pub use filesize::FilesizeConfig;
pub use helper::extract_value;
pub use hinter::HinterConfig;
pub use history::{HistoryConfig, HistoryFileFormat, HistoryPath};
pub use hooks::Hooks;
pub use ls::LsConfig;
pub use output::{BannerKind, ErrorStyle};
pub use plugin_gc::{PluginGcConfig, PluginGcConfigs};
pub use reedline::{CursorShapeConfig, EditBindings, NuCursorShape, ParsedKeybinding, ParsedMenu};
pub use rm::RmConfig;
pub use shell_integration::ShellIntegrationConfig;
pub use table::{FooterMode, TableConfig, TableIndent, TableIndexMode, TableMode, TrimStrategy};

mod ansi_coloring;
mod clip;
mod completions;
mod datetime_format;
mod defaults;
mod display_errors;
mod duration_max_unit;
mod error;
mod filesize;
mod helper;
mod hinter;
mod history;
mod hooks;
mod ls;
mod output;
mod plugin_gc;
mod prelude;
mod reedline;
mod rm;
mod shell_integration;
mod table;

#[derive(Clone, Debug, IntoValue, Serialize, Deserialize)]
pub struct Config {
    pub filesize: FilesizeConfig,
    pub table: TableConfig,
    pub ls: LsConfig,
    pub clip: ClipConfig,
    pub color_config: HashMap<String, Value>,
    pub footer_mode: FooterMode,
    pub float_precision: i64,
    pub recursion_limit: i64,
    pub use_ansi_coloring: UseAnsiColoring,
    pub completions: CompletionConfig,
    pub edit_mode: EditBindings,
    pub show_hints: bool,
    pub hinter: HinterConfig,
    pub history: HistoryConfig,
    pub keybindings: Vec<ParsedKeybinding>,
    pub abbreviations: HashMap<String, String>,
    pub menus: Vec<ParsedMenu>,
    pub hooks: Hooks,
    pub rm: RmConfig,
    pub shell_integration: ShellIntegrationConfig,
    pub buffer_editor: Value,
    pub show_banner: BannerKind,
    pub bracketed_paste: bool,
    pub render_right_prompt_on_last_line: bool,
    pub explore: HashMap<String, Value>,
    pub cursor_shape: CursorShapeConfig,
    pub datetime_format: DatetimeFormatConfig,
    pub error_style: ErrorStyle,
    pub error_lines: i64,
    pub display_errors: DisplayErrors,
    pub use_kitty_protocol: bool,
    pub highlight_resolved_externals: bool,
    pub auto_cd_implicit: bool,
    pub duration_max_unit: DurationMaxUnit,
    /// Maximum estimated memory size of the interactive last-result payload (`$ans.last`).
    ///
    /// Measured with [`Value::memory_size`]. Default is `0` (no `.last` payload; opt-in).
    /// Oversized results are truncated to fit this budget. The variable name itself is a code
    /// constant (`LAST_RESULT_VAR_NAME`), not a config option. With a positive budget, `$ans`
    /// is `{ last, exit_code, duration, command }`. With `0`, `$ans` still has `exit_code`,
    /// `duration`, and `command` but omits `last` entirely.
    pub max_last_result_size: Filesize,
    /// Configuration for plugins.
    ///
    /// Users can provide configuration for a plugin through this entry.  The entry name must
    /// match the registered plugin name so `plugin add nu_plugin_example` will be able to place
    /// its configuration under a `nu_plugin_example` column.
    pub plugins: HashMap<String, Value>,
    /// Configuration for plugin garbage collection.
    pub plugin_gc: PluginGcConfigs,
}

impl Default for Config {
    fn default() -> Config {
        Config {
            show_banner: BannerKind::default(),

            table: TableConfig::default(),
            rm: RmConfig::default(),
            ls: LsConfig::default(),

            datetime_format: DatetimeFormatConfig::default(),

            explore: defaults::default_explore(),

            history: HistoryConfig::default(),

            completions: CompletionConfig::default(),

            recursion_limit: 50,

            filesize: FilesizeConfig::default(),

            cursor_shape: CursorShapeConfig::default(),

            clip: ClipConfig::default(),

            color_config: defaults::default_color_config(),
            footer_mode: FooterMode::RowCount(25),
            float_precision: 2,
            buffer_editor: Value::nothing(Span::unknown()),
            use_ansi_coloring: UseAnsiColoring::default(),
            bracketed_paste: true,
            edit_mode: EditBindings::default(),
            show_hints: true,
            hinter: HinterConfig::default(),

            shell_integration: ShellIntegrationConfig::default(),

            render_right_prompt_on_last_line: false,

            hooks: Hooks::new(),

            menus: defaults::default_menus(),

            keybindings: defaults::default_keybindings(),
            abbreviations: HashMap::new(),

            error_style: ErrorStyle::default(),
            error_lines: 1,
            display_errors: DisplayErrors::default(),

            use_kitty_protocol: false,
            highlight_resolved_externals: false,

            auto_cd_implicit: false,
            duration_max_unit: DurationMaxUnit::default(),

            // Opt-in for `.last` payload: 0 drops last, keeps exit_code/duration/command.
            max_last_result_size: Filesize::ZERO,

            plugins: HashMap::new(),
            plugin_gc: PluginGcConfigs::default(),
        }
    }
}

impl UpdateFromValue for Config {
    fn update<'a>(
        &mut self,
        value: &'a Value,
        path: &mut ConfigPath<'a>,
        errors: &mut ConfigErrors,
    ) {
        let Value::Record { val: record, .. } = value else {
            errors.type_mismatch(path, Type::record(), value);
            return;
        };

        for (col, val) in record.iter() {
            let current_path = &mut path.push(col);

            match col.as_str() {
                "ls" => self.ls.update(val, current_path, errors),
                "rm" => self.rm.update(val, current_path, errors),
                "history" => self.history.update(val, current_path, errors),
                "completions" => self.completions.update(val, current_path, errors),
                "cursor_shape" => self.cursor_shape.update(val, current_path, errors),
                "table" => self.table.update(val, current_path, errors),
                "filesize" => self.filesize.update(val, current_path, errors),
                "explore" => self.explore.update(val, current_path, errors),
                "color_config" => self.color_config.update(val, current_path, errors),
                "clip" => self.clip.update(val, current_path, errors),
                "footer_mode" => self.footer_mode.update(val, current_path, errors),
                "float_precision" => self.float_precision.update(val, current_path, errors),
                "use_ansi_coloring" => self.use_ansi_coloring.update(val, current_path, errors),
                "edit_mode" => self.edit_mode.update(val, current_path, errors),
                "show_hints" => self.show_hints.update(val, current_path, errors),
                "hinter" => self.hinter.update(val, current_path, errors),
                "shell_integration" => self.shell_integration.update(val, current_path, errors),
                "show_banner" => self.show_banner.update(val, current_path, errors),
                "display_errors" => self.display_errors.update(val, current_path, errors),
                "render_right_prompt_on_last_line" => {
                    self.render_right_prompt_on_last_line
                        .update(val, current_path, errors)
                }
                "bracketed_paste" => self.bracketed_paste.update(val, current_path, errors),
                "use_kitty_protocol" => self.use_kitty_protocol.update(val, current_path, errors),
                "highlight_resolved_externals" => {
                    self.highlight_resolved_externals
                        .update(val, current_path, errors)
                }
                "auto_cd_implicit" => self.auto_cd_implicit.update(val, current_path, errors),
                "duration_max_unit" => self.duration_max_unit.update(val, current_path, errors),
                "plugins" => self.plugins.update(val, current_path, errors),
                "plugin_gc" => self.plugin_gc.update(val, current_path, errors),
                "abbreviations" => self.abbreviations.update(val, current_path, errors),
                "hooks" => self.hooks.update(val, current_path, errors),
                "datetime_format" => self.datetime_format.update(val, current_path, errors),
                "error_style" => self.error_style.update(val, current_path, errors),

                "buffer_editor" => match val {
                    Value::Nothing { .. } | Value::String { .. } => {
                        self.buffer_editor = val.clone();
                    }
                    Value::List { vals: values, .. }
                        if values
                            .iter()
                            .all(|list_element| matches!(list_element, Value::String { .. })) =>
                    {
                        self.buffer_editor = val.clone();
                    }
                    _ => errors.type_mismatch(
                        current_path,
                        Type::custom("string, list<string>, or nothing"),
                        val,
                    ),
                },

                "max_last_result_size" => {
                    self.max_last_result_size.update(val, current_path, errors)
                }

                "menus" => match Vec::<ParsedMenu>::from_value(val.clone()) {
                    Ok(menus) => {
                        for menu in menus {
                            let target_name = menu.name.to_expanded_string("", self);

                            let found_index = self.menus.iter().position(|existing_menu| {
                                existing_menu.name.to_expanded_string("", self) == target_name
                            });

                            if let Some(index) = found_index {
                                self.menus[index] = menu;
                            } else {
                                self.menus.push(menu);
                            }
                        }
                    }
                    Err(error) => errors.error(error.into()),
                },

                "keybindings" => match Vec::<ParsedKeybinding>::from_value(val.clone()) {
                    Ok(keybindings) => {
                        for keybinding in keybindings {
                            // An unnamed binding has no name to merge on, so match it on
                            // the key it binds. Otherwise it matches nothing and is
                            // appended again on every assignment, growing the list each
                            // time a config is re-sourced.
                            let found_index =
                                self.keybindings.iter().position(|existing_keybinding| {
                                    match (&keybinding.name, &existing_keybinding.name) {
                                        (Some(name), Some(existing_name)) => {
                                            name.to_expanded_string("", self)
                                                == existing_name.to_expanded_string("", self)
                                        }
                                        (None, None) => {
                                            keybinding.modifier == existing_keybinding.modifier
                                                && keybinding.keycode == existing_keybinding.keycode
                                                && keybinding.mode == existing_keybinding.mode
                                        }
                                        _ => false,
                                    }
                                });

                            if let Some(index) = found_index {
                                self.keybindings[index] = keybinding;
                            } else {
                                self.keybindings.push(keybinding);
                            }
                        }
                    }
                    Err(error) => errors.error(error.into()),
                },

                "error_lines" => match val.as_int() {
                    Ok(integer) if integer >= 0 => self.error_lines = integer,
                    Ok(_) => {
                        errors.invalid_value(current_path, "an int greater than or equal to 0", val)
                    }
                    Err(_) => errors.type_mismatch(current_path, Type::Int, val),
                },

                "recursion_limit" => match val.as_int() {
                    Ok(integer) if integer > 1 => self.recursion_limit = integer,
                    Ok(_) => errors.invalid_value(current_path, "an int greater than 1", val),
                    Err(_) => errors.type_mismatch(current_path, Type::Int, val),
                },

                _ => errors.unknown_option(current_path, val),
            }
        }
    }
}

impl UpdateFromValue for Filesize {
    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
        match value.as_filesize() {
            Ok(size) if !size.is_negative() => *self = size,
            Ok(_) => errors.invalid_value(path, "a non-negative filesize", value),
            Err(_) => errors.type_mismatch(path, Type::Filesize, value),
        }
    }
}

impl Config {
    /// Returns the configured last-result size budget in bytes (`0` disables `.last` only).
    pub fn max_last_result_size_bytes(&self) -> usize {
        self.max_last_result_size.get().max(0) as usize
    }

    pub fn update_from_value(
        &mut self,
        old: &Config,
        value: &Value,
    ) -> Result<Option<ShellWarning>, ShellError> {
        self.update_from_value_with_options(old, value, false)
    }

    /// Like [`Config::update_from_value`], but allows callers to indicate that runtime-locked
    /// options should refuse to change.
    ///
    /// `history_locked_after_startup` should be set to `true` once the REPL has finished
    /// initializing reedline's history backend. After that point, changing any of the
    /// startup-only history fields (`path`, `max_size`, `file_format`, `isolation`) has no
    /// effect on the live history, so we reject the assignment with a clear error instead of
    /// silently ignoring it.
    pub fn update_from_value_with_options(
        &mut self,
        old: &Config,
        value: &Value,
        history_locked_after_startup: bool,
    ) -> Result<Option<ShellWarning>, ShellError> {
        // Current behaviour is that config errors are displayed, but do not prevent the rest
        // of the config from being updated (fields with errors are skipped/not updated).
        // Errors are simply collected one-by-one and wrapped into a ShellError variant at the end.
        let mut errors =
            ConfigErrors::new(old).with_history_locked_after_startup(history_locked_after_startup);
        let mut path = ConfigPath::new();

        self.update(value, &mut path, &mut errors);

        errors.check()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A record-valued config field is a full-record replace on assignment (e.g.
    /// `$env.config.keybindings = [...]`), but `update_from_value` must still merge
    /// named defaults into place rather than silently dropping ones the caller
    /// didn't mention.
    #[test]
    fn reassigning_a_named_list_field_keeps_unmentioned_defaults() {
        let old = Config::default();
        let mut new = old.clone();

        let mut extra_menu = old.menus[0].clone();
        extra_menu.name = Value::test_string("added_menu");
        let mut extra_keybinding = old.keybindings[0].clone();
        extra_keybinding.name = Some(Value::test_string("added_binding"));

        let value = Value::test_record(record! {
            "menus" => Value::test_list(vec![extra_menu.into_value(Span::test_data())]),
            "keybindings" => Value::test_list(vec![extra_keybinding.into_value(Span::test_data())]),
        });
        new.update_from_value(&old, &value)
            .expect("update should succeed");

        for default_menu in &old.menus {
            let name = default_menu.name.to_expanded_string("", &old);
            assert!(
                new.menus
                    .iter()
                    .any(|m| m.name.to_expanded_string("", &new) == name),
                "default menu {name:?} was lost after reassigning `menus`"
            );
        }
        for default_keybinding in &old.keybindings {
            let Some(name) = default_keybinding
                .name
                .as_ref()
                .map(|n| n.to_expanded_string("", &old))
            else {
                continue;
            };
            assert!(
                new.keybindings.iter().any(|k| k

                    .name
                    .as_ref()
                    .is_some_and(|n| n.to_expanded_string("", &new) == name)),
                "default keybinding {name:?} was lost after reassigning `keybindings`"
            );
        }
    }

    /// Guards the unnamed case: with no `name` to merge on, every reassignment
    /// used to append another copy.
    #[test]
    fn reassigning_an_unnamed_keybinding_does_not_duplicate_it() {
        let old = Config::default();
        let mut new = old.clone();

        let mut unnamed = old.keybindings[0].clone();
        unnamed.name = None;
        unnamed.modifier = Value::test_string("alt");
        unnamed.keycode = Value::test_string("char_j");
        new.keybindings.push(unnamed);

        let expected = new.keybindings.len();

        // Feed the list back through `update_from_value` the way re-sourcing a
        // config (or any `$env.config.keybindings = ...`) does.
        for _ in 0..2 {
            let value = Value::test_record(record! {
                "keybindings" => Value::test_list(
                    new.keybindings
                        .iter()
                        .map(|keybinding| keybinding.clone().into_value(Span::test_data()))
                        .collect(),
                ),
            });
            new.update_from_value(&old, &value)
                .expect("update should succeed");
        }

        assert_eq!(
            new.keybindings.len(),
            expected,
            "reassigning `keybindings` duplicated the unnamed binding"
        );
    }
}