From a17d46f20016e007b5c2437cae541c93859a5569 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 17 Jun 2022 09:35:34 -0500 Subject: [PATCH] add more columns to the history command when using sqlite history (#5817) --- Cargo.lock | 4 +- Cargo.toml | 3 + crates/nu-command/src/misc/history.rs | 149 +++++++++++++++++++++----- 3 files changed, 127 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b06bfa6641..1c2a124d95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3825,13 +3825,13 @@ dependencies = [ [[package]] name = "reedline" version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f8163ab90fabf0b8978b824743bb7a384394501b4b40c198a146c0eb9670ca4" +source = "git+https://github.com/nushell/reedline?branch=main#680bc7a162b3536dd2db7353b4714bf540f4ae62" dependencies = [ "chrono", "crossterm", "fd-lock", "gethostname", + "itertools", "nu-ansi-term", "rusqlite", "serde", diff --git a/Cargo.toml b/Cargo.toml index 3820b7802d..e1a37ba44c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -119,3 +119,6 @@ debug = false [[bin]] name = "nu" path = "src/main.rs" + +[patch.crates-io] +reedline = { git = "https://github.com/nushell/reedline", branch = "main"} diff --git a/crates/nu-command/src/misc/history.rs b/crates/nu-command/src/misc/history.rs index 2110649179..d38099e47c 100644 --- a/crates/nu-command/src/misc/history.rs +++ b/crates/nu-command/src/misc/history.rs @@ -76,33 +76,128 @@ impl Command for History { .ok(), }; - let data = history_reader - .and_then(|h| { - h.search(SearchQuery::everything(SearchDirection::Forward)) - .ok() - }) - .map(move |entries| { - entries - .into_iter() - .enumerate() - .map(move |(idx, entry)| Value::Record { - cols: vec!["command".to_string(), "index".to_string()], - vals: vec![ - Value::String { - val: entry.command_line, - span: head, - }, - Value::Int { - val: idx as i64, - span: head, - }, - ], - span: head, - }) - }) - .ok_or(ShellError::FileNotFound(head))? - .into_pipeline_data(ctrlc); - Ok(data) + match engine_state.config.history_file_format { + HistoryFileFormat::PlainText => Ok(history_reader + .and_then(|h| { + h.search(SearchQuery::everything(SearchDirection::Forward)) + .ok() + }) + .map(move |entries| { + entries + .into_iter() + .enumerate() + .map(move |(idx, entry)| Value::Record { + cols: vec!["command".to_string(), "index".to_string()], + vals: vec![ + Value::String { + val: entry.command_line, + span: head, + }, + Value::Int { + val: idx as i64, + span: head, + }, + ], + span: head, + }) + }) + .ok_or(ShellError::FileNotFound(head))? + .into_pipeline_data(ctrlc)), + HistoryFileFormat::Sqlite => Ok(history_reader + .and_then(|h| { + h.search(SearchQuery::everything(SearchDirection::Forward)) + .ok() + }) + .map(move |entries| { + entries + .into_iter() + .enumerate() + .map(move |(idx, entry)| Value::Record { + cols: vec![ + "item_id".into(), + "start_timestamp".into(), + "command_line".to_string(), + "session_id".into(), + "hostname".into(), + "cwd".into(), + "duration".into(), + "exit_status".into(), + "index".to_string(), + ], + vals: vec![ + Value::Int { + val: match entry.id { + Some(id) => { + let ids = id.to_string(); + match ids.parse::() { + Ok(i) => i, + _ => 0i64, + } + } + None => 0i64, + }, + span: head, + }, + Value::String { + val: match entry.start_timestamp { + Some(time) => time.to_string(), + None => "".into(), + }, + span: head, + }, + Value::String { + val: entry.command_line, + span: head, + }, + Value::Int { + val: match entry.session_id { + Some(sid) => { + let sids = sid.to_string(); + match sids.parse::() { + Ok(i) => i, + _ => 0i64, + } + } + None => 0i64, + }, + span: head, + }, + Value::String { + val: match entry.hostname { + Some(host) => host, + None => "".into(), + }, + span: head, + }, + Value::String { + val: match entry.cwd { + Some(cwd) => cwd, + None => "".into(), + }, + span: head, + }, + Value::Duration { + val: match entry.duration { + Some(d) => d.as_millis().try_into().unwrap_or(0), + None => 0, + }, + span: head, + }, + Value::Int { + val: entry.exit_status.unwrap_or(0), + span: head, + }, + Value::Int { + val: idx as i64, + span: head, + }, + ], + span: head, + }) + }) + .ok_or(ShellError::FileNotFound(head))? + .into_pipeline_data(ctrlc)), + } } } else { Err(ShellError::FileNotFound(head))