nu_command/env/config/
config_nu.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::PipelineMetadata;
3
4#[derive(Clone)]
5pub struct ConfigNu;
6
7impl Command for ConfigNu {
8    fn name(&self) -> &str {
9        "config nu"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build(self.name())
14            .category(Category::Env)
15            .input_output_types(vec![(Type::Nothing, Type::Any)])
16            .switch(
17                "default",
18                "Print the internal default `config.nu` file instead.",
19                Some('d'),
20            )
21            .switch(
22                "doc",
23                "Print a commented `config.nu` with documentation instead.",
24                Some('s'),
25            )
26    }
27
28    fn description(&self) -> &str {
29        "Edit nu configurations."
30    }
31
32    fn examples(&self) -> Vec<Example> {
33        vec![
34            Example {
35                description: "open user's config.nu in the default editor",
36                example: "config nu",
37                result: None,
38            },
39            Example {
40                description: "pretty-print a commented `config.nu` that explains common settings",
41                example: "config nu --doc | nu-highlight",
42                result: None,
43            },
44            Example {
45                description: "pretty-print the internal `config.nu` file which is loaded before user's config",
46                example: "config nu --default | nu-highlight",
47                result: None,
48            },
49        ]
50    }
51
52    fn run(
53        &self,
54        engine_state: &EngineState,
55        stack: &mut Stack,
56        call: &Call,
57        _input: PipelineData,
58    ) -> Result<PipelineData, ShellError> {
59        let default_flag = call.has_flag(engine_state, stack, "default")?;
60        let doc_flag = call.has_flag(engine_state, stack, "doc")?;
61        if default_flag && doc_flag {
62            return Err(ShellError::IncompatibleParameters {
63                left_message: "can't use `--default` at the same time".into(),
64                left_span: call.get_flag_span(stack, "default").expect("has flag"),
65                right_message: "because of `--doc`".into(),
66                right_span: call.get_flag_span(stack, "doc").expect("has flag"),
67            });
68        }
69
70        // `--default` flag handling
71        if default_flag {
72            let head = call.head;
73            return Ok(Value::string(nu_utils::get_default_config(), head)
74                .into_pipeline_data_with_metadata(
75                    PipelineMetadata::default()
76                        .with_content_type("application/x-nuscript".to_string().into()),
77                ));
78        }
79
80        // `--doc` flag handling
81        if doc_flag {
82            let head = call.head;
83            return Ok(Value::string(nu_utils::get_doc_config(), head)
84                .into_pipeline_data_with_metadata(
85                    PipelineMetadata::default()
86                        .with_content_type("application/x-nuscript".to_string().into()),
87                ));
88        }
89
90        super::config_::start_editor("config-path", engine_state, stack, call)
91    }
92}