2019-10-27 16:58:39 +00:00
|
|
|
use crate::cli::History as HistoryFile;
|
2020-04-27 02:04:54 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-10-27 16:58:39 +00:00
|
|
|
use crate::prelude::*;
|
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};
|
|
|
|
|
|
|
|
pub struct History;
|
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct HistoryArgs;
|
|
|
|
|
|
|
|
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 {
|
2019-10-27 16:58:39 +00:00
|
|
|
Signature::build("history")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Display command history."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2020-04-27 02:04:54 +00:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
2019-10-27 16:58:39 +00:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-04-27 02:04:54 +00:00
|
|
|
args.process(registry, history)?.run()
|
|
|
|
}
|
|
|
|
}
|
2019-10-27 16:58:39 +00:00
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
fn history(
|
|
|
|
_: HistoryArgs,
|
|
|
|
RunnableContext { name: tag, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let stream = async_stream! {
|
|
|
|
let history_path = HistoryFile::path();
|
|
|
|
let file = File::open(history_path);
|
|
|
|
if let Ok(file) = file {
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
for line in reader.lines() {
|
|
|
|
if let Ok(line) = line {
|
|
|
|
yield ReturnSuccess::value(UntaggedValue::string(line).into_value(tag.clone()));
|
2019-10-27 16:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 02:04:54 +00:00
|
|
|
} else {
|
|
|
|
yield Err(ShellError::labeled_error("Could not open history", "history file could not be opened", tag.clone()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(stream.to_output_stream())
|
2019-10-27 16:58:39 +00:00
|
|
|
}
|