nu_cmd_lang/core_commands/overlay/
hide.rs

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
use nu_engine::command_prelude::*;
use nu_protocol::engine::CommandType;

#[derive(Clone)]
pub struct OverlayHide;

impl Command for OverlayHide {
    fn name(&self) -> &str {
        "overlay hide"
    }

    fn description(&self) -> &str {
        "Hide an active overlay."
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build("overlay hide")
            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
            .optional("name", SyntaxShape::String, "Overlay to hide.")
            .switch(
                "keep-custom",
                "Keep all newly added commands and aliases in the next activated overlay.",
                Some('k'),
            )
            .named(
                "keep-env",
                SyntaxShape::List(Box::new(SyntaxShape::String)),
                "List of environment variables to keep in the next activated overlay",
                Some('e'),
            )
            .category(Category::Core)
    }

    fn extra_description(&self) -> &str {
        r#"This command is a parser keyword. For details, check:
  https://www.nushell.sh/book/thinking_in_nu.html"#
    }

    fn command_type(&self) -> CommandType {
        CommandType::Keyword
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let overlay_name: Spanned<String> = if let Some(name) = call.opt(engine_state, stack, 0)? {
            name
        } else {
            Spanned {
                item: stack.last_overlay_name()?,
                span: call.head,
            }
        };

        if !stack.is_overlay_active(&overlay_name.item) {
            return Err(ShellError::OverlayNotFoundAtRuntime {
                overlay_name: overlay_name.item,
                span: overlay_name.span,
            });
        }

        let keep_env: Option<Vec<Spanned<String>>> =
            call.get_flag(engine_state, stack, "keep-env")?;

        let env_vars_to_keep = if let Some(env_var_names_to_keep) = keep_env {
            let mut env_vars_to_keep = vec![];

            for name in env_var_names_to_keep.into_iter() {
                match stack.get_env_var(engine_state, &name.item) {
                    Some(val) => env_vars_to_keep.push((name.item, val.clone())),
                    None => {
                        return Err(ShellError::EnvVarNotFoundAtRuntime {
                            envvar_name: name.item,
                            span: name.span,
                        })
                    }
                }
            }

            env_vars_to_keep
        } else {
            vec![]
        };

        stack.remove_overlay(&overlay_name.item);

        for (name, val) in env_vars_to_keep {
            stack.add_env_var(name, val);
        }

        Ok(PipelineData::empty())
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Keep a custom command after hiding the overlay",
                example: r#"module spam { export def foo [] { "foo" } }
    overlay use spam
    def bar [] { "bar" }
    overlay hide spam --keep-custom
    bar
    "#,
                result: None,
            },
            Example {
                description: "Hide an overlay created from a file",
                example: r#"'export alias f = "foo"' | save spam.nu
    overlay use spam.nu
    overlay hide spam"#,
                result: None,
            },
            Example {
                description: "Hide the last activated overlay",
                example: r#"module spam { export-env { $env.FOO = "foo" } }
    overlay use spam
    overlay hide"#,
                result: None,
            },
            Example {
                description: "Keep the current working directory when removing an overlay",
                example: r#"overlay new spam
    cd some-dir
    overlay hide --keep-env [ PWD ] spam"#,
                result: None,
            },
        ]
    }
}