SQLite History MVP with timestamp, duration, working directory, exit status metadata (#5721)

This PR adds support for an SQLite history via nushell/reedline#401

The SQLite history is enabled by setting history_file_format: "sqlite" in config.nu.

* somewhat working sqlite history
* Hook up history command
* Fix error in SQlitebacked with empty lines

When entering an empty line there previously was the "No command run"
error with `SqliteBackedHistory` during addition of the metadata

May be considered a temporary fix

Co-authored-by: sholderbach <sholderbach@users.noreply.github.com>
This commit is contained in:
phiresky
2022-06-14 22:53:33 +02:00
committed by GitHub
parent 534e1fc3ce
commit 42dbfd1fa0
12 changed files with 243 additions and 127 deletions

View File

@ -66,6 +66,7 @@ pub struct Config {
pub edit_mode: String,
pub max_history_size: i64,
pub sync_history_on_enter: bool,
pub history_file_format: HistoryFileFormat,
pub log_level: String,
pub keybindings: Vec<ParsedKeybinding>,
pub menus: Vec<ParsedMenu>,
@ -98,6 +99,7 @@ impl Default for Config {
edit_mode: "emacs".into(),
max_history_size: i64::MAX,
sync_history_on_enter: true,
history_file_format: HistoryFileFormat::PlainText,
log_level: String::new(),
keybindings: Vec::new(),
menus: Vec::new(),
@ -125,6 +127,14 @@ pub enum FooterMode {
Auto,
}
#[derive(Serialize, Deserialize, Clone, Debug, Copy)]
pub enum HistoryFileFormat {
/// Store history as an SQLite database with additional context
Sqlite,
/// store history as a plain text file where every line is one command (without any context such as timestamps)
PlainText,
}
impl Value {
pub fn into_config(self) -> Result<Config, ShellError> {
let v = self.as_record();
@ -248,6 +258,23 @@ impl Value {
eprintln!("$config.edit_mode is not a string")
}
}
"history_file_format" => {
if let Ok(b) = value.as_string() {
let val_str = b.to_lowercase();
config.history_file_format = match val_str.as_ref() {
"sqlite" => HistoryFileFormat::Sqlite,
"plaintext" => HistoryFileFormat::PlainText,
_ => {
eprintln!(
"unrecognized $config.history_file_format '{val_str}'"
);
HistoryFileFormat::PlainText
}
};
} else {
eprintln!("$config.history_file_format is not a string")
}
}
"max_history_size" => {
if let Ok(i) = value.as_i64() {
config.max_history_size = i;