2019-10-27 16:58:39 +00:00
|
|
|
use crate::prelude::*;
|
2020-09-16 23:22:58 +00:00
|
|
|
use nu_data::config::{Conf, NuConfig};
|
2021-01-10 02:50:49 +00:00
|
|
|
use nu_engine::history_path;
|
|
|
|
use nu_engine::WholeStreamCommand;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2020-04-27 02:04:54 +00:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
2019-10-27 16:58:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{BufRead, BufReader};
|
|
|
|
|
2020-11-09 16:23:41 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Arguments {
|
|
|
|
clear: Option<bool>,
|
|
|
|
}
|
|
|
|
|
2019-10-27 16:58:39 +00:00
|
|
|
pub struct History;
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
#[async_trait]
|
2020-04-27 02:04:54 +00:00
|
|
|
impl WholeStreamCommand for History {
|
2019-10-27 16:58:39 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"history"
|
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
fn signature(&self) -> Signature {
|
2020-11-09 16:23:41 +00:00
|
|
|
Signature::build("history").switch("clear", "Clears out the history entries", Some('c'))
|
2019-10-27 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Display command history."
|
|
|
|
}
|
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
history(args).await
|
2020-04-27 02:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-27 16:58:39 +00:00
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
async fn history(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-09-16 23:22:58 +00:00
|
|
|
let config: Box<dyn Conf> = Box::new(NuConfig::new());
|
2020-11-09 16:23:41 +00:00
|
|
|
let tag = args.call_info.name_tag.clone();
|
2020-12-18 07:53:49 +00:00
|
|
|
let (Arguments { clear }, _) = args.process().await?;
|
2020-11-09 16:23:41 +00:00
|
|
|
|
2020-08-27 11:06:25 +00:00
|
|
|
let path = history_path(&config);
|
2020-11-09 16:23:41 +00:00
|
|
|
|
|
|
|
match clear {
|
|
|
|
Some(_) => {
|
|
|
|
// This is a NOOP, the logic to clear is handled in cli.rs
|
|
|
|
Ok(OutputStream::empty())
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if let Ok(file) = File::open(path) {
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
// Skips the first line, which is a Rustyline internal
|
|
|
|
let output = reader.lines().skip(1).filter_map(move |line| match line {
|
|
|
|
Ok(line) => Some(ReturnSuccess::value(
|
|
|
|
UntaggedValue::string(line).into_value(tag.clone()),
|
|
|
|
)),
|
|
|
|
Err(_) => None,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(futures::stream::iter(output).to_output_stream())
|
|
|
|
} else {
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
"Could not open history",
|
|
|
|
"history file could not be opened",
|
|
|
|
tag,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 08:43:21 +00:00
|
|
|
}
|
2019-10-27 16:58:39 +00:00
|
|
|
}
|
2020-05-18 12:56:01 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::History;
|
2020-10-03 14:06:02 +00:00
|
|
|
use super::ShellError;
|
2020-05-18 12:56:01 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 14:06:02 +00:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 12:56:01 +00:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 10:13:14 +00:00
|
|
|
test_examples(History {})
|
2020-05-18 12:56:01 +00:00
|
|
|
}
|
|
|
|
}
|