nu_command/path/
self_.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
use nu_engine::command_prelude::*;
use nu_path::expand_path_with;
use nu_protocol::{engine::StateWorkingSet, shell_error::io::IoError};

#[derive(Clone)]
pub struct SubCommand;

impl Command for SubCommand {
    fn name(&self) -> &str {
        "path self"
    }

    fn signature(&self) -> Signature {
        Signature::build("path self")
            .input_output_type(Type::Nothing, Type::String)
            .allow_variants_without_examples(true)
            .optional(
                "path",
                SyntaxShape::Filepath,
                "Path to get instead of the current file.",
            )
            .category(Category::Path)
    }

    fn description(&self) -> &str {
        "Get the absolute path of the script or module containing this command at parse time."
    }

    fn is_const(&self) -> bool {
        true
    }

    fn run(
        &self,
        _engine_state: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        Err(ShellError::GenericError {
            error: "this command can only run during parse-time".into(),
            msg: "can't run after parse-time".into(),
            span: Some(call.head),
            help: Some("try assigning this command's output to a const variable".into()),
            inner: vec![],
        })
    }

    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let path: Option<String> = call.opt_const(working_set, 0)?;
        let cwd = working_set.permanent_state.cwd(None)?;
        let current_file = working_set.files.top().ok_or_else(|| {
            IoError::new_with_additional_context(
                std::io::ErrorKind::NotFound,
                call.head,
                None,
                "Couldn't find current file",
            )
        })?;

        let out = if let Some(path) = path {
            let dir = expand_path_with(
                current_file.parent().ok_or_else(|| {
                    IoError::new_with_additional_context(
                        std::io::ErrorKind::NotFound,
                        call.head,
                        current_file.to_owned(),
                        "Couldn't find current file's parent.",
                    )
                })?,
                &cwd,
                true,
            );
            Value::string(
                expand_path_with(path, dir, false).to_string_lossy(),
                call.head,
            )
        } else {
            Value::string(
                expand_path_with(current_file, &cwd, true).to_string_lossy(),
                call.head,
            )
        };

        Ok(out.into_pipeline_data())
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Get the path of the current file",
                example: r#"const current_file = path self"#,
                result: None,
            },
            Example {
                description: "Get the path of the directory containing the current file",
                example: r#"const current_file = path self ."#,
                result: None,
            },
            #[cfg(windows)]
            Example {
                description: "Get the absolute form of a path relative to the current file",
                example: r#"const current_file = path self ..\foo"#,
                result: None,
            },
            #[cfg(not(windows))]
            Example {
                description: "Get the absolute form of a path relative to the current file",
                example: r#"const current_file = path self ../foo"#,
                result: None,
            },
        ]
    }
}

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

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(SubCommand {})
    }
}