From 71844755e53dd842fdf616c71dce85e98de85eb1 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Mon, 19 Sep 2022 14:30:04 -0500 Subject: [PATCH] add history session command (#6587) --- crates/nu-cli/tests/completions.rs | 11 ++--- crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/misc/history_session.rs | 43 +++++++++++++++++++ crates/nu-command/src/misc/mod.rs | 2 + crates/nu-engine/src/eval.rs | 3 -- 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 crates/nu-command/src/misc/history_session.rs diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs index 2e27a4ab3..10c88c17b 100644 --- a/crates/nu-cli/tests/completions.rs +++ b/crates/nu-cli/tests/completions.rs @@ -469,13 +469,12 @@ fn variables_completions() { // Test completions for $nu let suggestions = completer.complete("$nu.", 4); - assert_eq!(10, suggestions.len()); + assert_eq!(9, suggestions.len()); let expected: Vec = vec![ "config-path".into(), "env-path".into(), "history-path".into(), - "history-session-id".into(), "home-path".into(), "loginshell-path".into(), "os-info".into(), @@ -490,13 +489,9 @@ fn variables_completions() { // Test completions for $nu.h (filter) let suggestions = completer.complete("$nu.h", 5); - assert_eq!(3, suggestions.len()); + assert_eq!(2, suggestions.len()); - let expected: Vec = vec![ - "history-path".into(), - "history-session-id".into(), - "home-path".into(), - ]; + let expected: Vec = vec!["history-path".into(), "home-path".into()]; // Match results match_suggestions(expected, suggestions); diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 85af4285d..5fffd4861 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -138,6 +138,7 @@ pub fn create_default_context() -> EngineState { bind_command! { History, Tutor, + HistorySession, }; // Path diff --git a/crates/nu-command/src/misc/history_session.rs b/crates/nu-command/src/misc/history_session.rs new file mode 100644 index 000000000..08897aa1e --- /dev/null +++ b/crates/nu-command/src/misc/history_session.rs @@ -0,0 +1,43 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, Signature, Value}; + +#[derive(Clone)] +pub struct HistorySession; + +impl Command for HistorySession { + fn name(&self) -> &str { + "history session" + } + + fn usage(&self) -> &str { + "Get the command history session" + } + + fn signature(&self) -> nu_protocol::Signature { + Signature::build("history session").category(Category::Misc) + } + + fn examples(&self) -> Vec { + vec![Example { + example: "history session", + description: "Get current history session", + result: None, + }] + } + + fn run( + &self, + engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + Ok(Value::Record { + cols: vec!["session-id".into()], + vals: vec![Value::int(engine_state.history_session_id, call.head)], + span: call.head, + } + .into_pipeline_data()) + } +} diff --git a/crates/nu-command/src/misc/mod.rs b/crates/nu-command/src/misc/mod.rs index fcb539f60..02d682724 100644 --- a/crates/nu-command/src/misc/mod.rs +++ b/crates/nu-command/src/misc/mod.rs @@ -1,5 +1,7 @@ mod history; +mod history_session; mod tutor; pub use history::History; +pub use history_session::HistorySession; pub use tutor::Tutor; diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index a3174d7dd..c53da6af3 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1434,9 +1434,6 @@ pub fn eval_variable( output_cols.push("os-info".into()); output_vals.push(os_record); - output_cols.push("history-session-id".into()); - output_vals.push(Value::int(engine_state.history_session_id, span)); - Ok(Value::Record { cols: output_cols, vals: output_vals,