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
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Value,
};

const NEWLINE_ESCAPE_CODE: &str = "<\\n>";

fn decode_newlines(escaped: &str) -> String {
    escaped.replace(NEWLINE_ESCAPE_CODE, "\n")
}

#[derive(Clone)]
pub struct History;

impl Command for History {
    fn name(&self) -> &str {
        "history"
    }

    fn usage(&self) -> &str {
        "Get the command history"
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build("history")
            .switch("clear", "Clears out the history entries", Some('c'))
            .category(Category::Core)
    }

    fn run(
        &self,
        engine_state: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
        let head = call.head;
        if let Some(config_path) = nu_path::config_dir() {
            let clear = call.has_flag("clear");
            let ctrlc = engine_state.ctrlc.clone();

            let mut history_path = config_path;
            history_path.push("nushell");
            history_path.push("history.txt");

            if clear {
                let _ = std::fs::remove_file(history_path);
                Ok(PipelineData::new(head))
            } else {
                let contents = std::fs::read_to_string(history_path);

                if let Ok(contents) = contents {
                    Ok(contents
                        .lines()
                        .enumerate()
                        .map(move |(index, command)| Value::Record {
                            cols: vec!["command".to_string(), "index".to_string()],
                            vals: vec![
                                Value::String {
                                    val: decode_newlines(command),
                                    span: head,
                                },
                                Value::Int {
                                    val: index as i64,
                                    span: head,
                                },
                            ],
                            span: head,
                        })
                        .collect::<Vec<_>>()
                        .into_iter()
                        .into_pipeline_data(ctrlc))
                } else {
                    Err(ShellError::FileNotFound(head))
                }
            }
        } else {
            Err(ShellError::FileNotFound(head))
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                example: "history | length",
                description: "Get current history length",
                result: None,
            },
            Example {
                example: "history | last 5",
                description: "Show last 5 commands you have ran",
                result: None,
            },
            Example {
                example: "history | wrap cmd | where cmd =~ cargo",
                description: "Search all the commands from history that contains 'cargo'",
                result: None,
            },
        ]
    }
}