add more columns to the history command when using sqlite history (#5817)

This commit is contained in:
Darren Schroeder 2022-06-17 09:35:34 -05:00 committed by GitHub
parent 6cc8402127
commit a17d46f200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 127 additions and 29 deletions

4
Cargo.lock generated
View File

@ -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",

View File

@ -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"}

View File

@ -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::<i64>() {
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::<i64>() {
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))