diff --git a/crates/nu-cli/src/commands/history/history_.rs b/crates/nu-cli/src/commands/history/history_.rs index f5289e24a4..602574229e 100644 --- a/crates/nu-cli/src/commands/history/history_.rs +++ b/crates/nu-cli/src/commands/history/history_.rs @@ -39,80 +39,78 @@ impl Command for History { ) -> Result { let head = call.head; - if let Some(history) = engine_state.history_config() { - // todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history` - let Some(history_path) = history.file_path() else { - return Err(ShellError::ConfigDirNotFound { span: Some(head) }); - }; - let clear = call.has_flag(engine_state, stack, "clear")?; - let long = call.has_flag(engine_state, stack, "long")?; - let signals = engine_state.signals().clone(); + let Some(history) = engine_state.history_config() else { + return Ok(PipelineData::empty()); + }; + // todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history` + let Some(history_path) = history.file_path() else { + return Err(ShellError::ConfigDirNotFound { span: Some(head) }); + }; - if clear { - let _ = std::fs::remove_file(history_path); - // TODO: FIXME also clear the auxiliary files when using sqlite - Ok(PipelineData::empty()) - } else { - let history_reader: Option> = match history.file_format { - HistoryFileFormat::Sqlite => { - SqliteBackedHistory::with_file(history_path.clone(), None, None) - .map(|inner| { - let boxed: Box = Box::new(inner); - boxed - }) - .ok() - } - HistoryFileFormat::Plaintext => FileBackedHistory::with_file( - history.max_size as usize, - history_path.clone(), - ) + if call.has_flag(engine_state, stack, "clear")? { + let _ = std::fs::remove_file(history_path); + // TODO: FIXME also clear the auxiliary files when using sqlite + return Ok(PipelineData::empty()); + } + + let long = call.has_flag(engine_state, stack, "long")?; + let signals = engine_state.signals().clone(); + let history_reader: Option> = match history.file_format { + HistoryFileFormat::Sqlite => { + SqliteBackedHistory::with_file(history_path.clone(), None, None) .map(|inner| { let boxed: Box = Box::new(inner); boxed }) - .ok(), - }; - match history.file_format { - HistoryFileFormat::Plaintext => Ok(history_reader - .and_then(|h| { - h.search(SearchQuery::everything(SearchDirection::Forward, None)) - .ok() - }) - .map(move |entries| { - entries.into_iter().enumerate().map(move |(idx, entry)| { - Value::record( - record! { - "command" => Value::string(entry.command_line, head), - "index" => Value::int(idx as i64, head), - }, - head, - ) - }) - }) - .ok_or(ShellError::FileNotFound { - file: history_path.display().to_string(), - span: head, - })? - .into_pipeline_data(head, signals)), - HistoryFileFormat::Sqlite => Ok(history_reader - .and_then(|h| { - h.search(SearchQuery::everything(SearchDirection::Forward, None)) - .ok() - }) - .map(move |entries| { - entries.into_iter().enumerate().map(move |(idx, entry)| { - create_history_record(idx, entry, long, head) - }) - }) - .ok_or(ShellError::FileNotFound { - file: history_path.display().to_string(), - span: head, - })? - .into_pipeline_data(head, signals)), - } + .ok() } - } else { - Ok(PipelineData::empty()) + HistoryFileFormat::Plaintext => { + FileBackedHistory::with_file(history.max_size as usize, history_path.clone()) + .map(|inner| { + let boxed: Box = Box::new(inner); + boxed + }) + .ok() + } + }; + match history.file_format { + HistoryFileFormat::Plaintext => Ok(history_reader + .and_then(|h| { + h.search(SearchQuery::everything(SearchDirection::Forward, None)) + .ok() + }) + .map(move |entries| { + entries.into_iter().enumerate().map(move |(idx, entry)| { + Value::record( + record! { + "command" => Value::string(entry.command_line, head), + "index" => Value::int(idx as i64, head), + }, + head, + ) + }) + }) + .ok_or(ShellError::FileNotFound { + file: history_path.display().to_string(), + span: head, + })? + .into_pipeline_data(head, signals)), + HistoryFileFormat::Sqlite => Ok(history_reader + .and_then(|h| { + h.search(SearchQuery::everything(SearchDirection::Forward, None)) + .ok() + }) + .map(move |entries| { + entries + .into_iter() + .enumerate() + .map(move |(idx, entry)| create_history_record(idx, entry, long, head)) + }) + .ok_or(ShellError::FileNotFound { + file: history_path.display().to_string(), + span: head, + })? + .into_pipeline_data(head, signals)), } }