nu_cmd_lang/core_commands/overlay/
new.rs

1use nu_engine::{command_prelude::*, redirect_env};
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct OverlayNew;
6
7impl Command for OverlayNew {
8    fn name(&self) -> &str {
9        "overlay new"
10    }
11
12    fn description(&self) -> &str {
13        "Create an empty overlay."
14    }
15
16    fn signature(&self) -> nu_protocol::Signature {
17        Signature::build("overlay new")
18            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
19            .allow_variants_without_examples(true)
20            .required("name", SyntaxShape::String, "Name of the overlay.")
21            .switch(
22                "reload",
23                "If the overlay already exists, reload its environment.",
24                Some('r'),
25            )
26            // TODO:
27            // .switch(
28            //     "prefix",
29            //     "Prepend module name to the imported symbols",
30            //     Some('p'),
31            // )
32            .category(Category::Core)
33    }
34
35    fn extra_description(&self) -> &str {
36        r#"The command will first create an empty module, then add it as an overlay.
37
38This command is a parser keyword. For details, check:
39  https://www.nushell.sh/book/thinking_in_nu.html"#
40    }
41
42    fn command_type(&self) -> CommandType {
43        CommandType::Keyword
44    }
45
46    fn run(
47        &self,
48        engine_state: &EngineState,
49        caller_stack: &mut Stack,
50        call: &Call,
51        _input: PipelineData,
52    ) -> Result<PipelineData, ShellError> {
53        let name_arg: Spanned<String> = call.req(engine_state, caller_stack, 0)?;
54        let reload = call.has_flag(engine_state, caller_stack, "reload")?;
55
56        if reload {
57            let callee_stack = caller_stack.clone();
58            caller_stack.add_overlay(name_arg.item);
59            redirect_env(engine_state, caller_stack, &callee_stack);
60        } else {
61            caller_stack.add_overlay(name_arg.item);
62        }
63
64        Ok(PipelineData::empty())
65    }
66
67    fn examples(&self) -> Vec<Example<'_>> {
68        vec![Example {
69            description: "Create an empty overlay",
70            example: r#"overlay new spam"#,
71            result: None,
72        }]
73    }
74}
75
76#[cfg(test)]
77mod test {
78    use super::*;
79
80    #[test]
81    fn test_examples() {
82        use crate::test_examples;
83
84        test_examples(OverlayNew {})
85    }
86}