Reduce nesting in the history command code (#14069)

# Description

This is a purely cosmetic change to make the code read more linearly.

# User-Facing Changes

None.
This commit is contained in:
Piotr Kufel 2024-10-11 23:44:49 -07:00 committed by GitHub
parent de08b68ba8
commit e32e55938b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,20 +39,22 @@ impl Command for History {
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head; let head = call.head;
if let Some(history) = engine_state.history_config() { 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` // 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 { let Some(history_path) = history.file_path() else {
return Err(ShellError::ConfigDirNotFound { span: Some(head) }); 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();
if clear { if call.has_flag(engine_state, stack, "clear")? {
let _ = std::fs::remove_file(history_path); let _ = std::fs::remove_file(history_path);
// TODO: FIXME also clear the auxiliary files when using sqlite // TODO: FIXME also clear the auxiliary files when using sqlite
Ok(PipelineData::empty()) return Ok(PipelineData::empty());
} else { }
let long = call.has_flag(engine_state, stack, "long")?;
let signals = engine_state.signals().clone();
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format { let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
HistoryFileFormat::Sqlite => { HistoryFileFormat::Sqlite => {
SqliteBackedHistory::with_file(history_path.clone(), None, None) SqliteBackedHistory::with_file(history_path.clone(), None, None)
@ -62,15 +64,14 @@ impl Command for History {
}) })
.ok() .ok()
} }
HistoryFileFormat::Plaintext => FileBackedHistory::with_file( HistoryFileFormat::Plaintext => {
history.max_size as usize, FileBackedHistory::with_file(history.max_size as usize, history_path.clone())
history_path.clone(),
)
.map(|inner| { .map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed boxed
}) })
.ok(), .ok()
}
}; };
match history.file_format { match history.file_format {
HistoryFileFormat::Plaintext => Ok(history_reader HistoryFileFormat::Plaintext => Ok(history_reader
@ -100,9 +101,10 @@ impl Command for History {
.ok() .ok()
}) })
.map(move |entries| { .map(move |entries| {
entries.into_iter().enumerate().map(move |(idx, entry)| { entries
create_history_record(idx, entry, long, head) .into_iter()
}) .enumerate()
.map(move |(idx, entry)| create_history_record(idx, entry, long, head))
}) })
.ok_or(ShellError::FileNotFound { .ok_or(ShellError::FileNotFound {
file: history_path.display().to_string(), file: history_path.display().to_string(),
@ -111,10 +113,6 @@ impl Command for History {
.into_pipeline_data(head, signals)), .into_pipeline_data(head, signals)),
} }
} }
} else {
Ok(PipelineData::empty())
}
}
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![ vec![