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,80 +39,78 @@ 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 {
// todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history` return Ok(PipelineData::empty());
let Some(history_path) = history.file_path() else { };
return Err(ShellError::ConfigDirNotFound { span: Some(head) }); // 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 clear = call.has_flag(engine_state, stack, "clear")?; return Err(ShellError::ConfigDirNotFound { span: Some(head) });
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 history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
HistoryFileFormat::Sqlite => { let long = call.has_flag(engine_state, stack, "long")?;
SqliteBackedHistory::with_file(history_path.clone(), None, None) let signals = engine_state.signals().clone();
.map(|inner| { let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); HistoryFileFormat::Sqlite => {
boxed SqliteBackedHistory::with_file(history_path.clone(), None, None)
})
.ok()
}
HistoryFileFormat::Plaintext => FileBackedHistory::with_file(
history.max_size as usize,
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 {
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)),
}
} }
} else { HistoryFileFormat::Plaintext => {
Ok(PipelineData::empty()) FileBackedHistory::with_file(history.max_size as usize, history_path.clone())
.map(|inner| {
let boxed: Box<dyn ReedlineHistory> = 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)),
} }
} }