cli: extract Ui::new() that creates ui with default config

Since ui object is needed to report read_config() error, it makes sense to
create ui first without fallible user configuration. Ui::for_terminal() will
be replaced with this function and ui.reset(read_config()?).

Default::default() is also added to silence clippy. If we prefer, Ui::new()
can be replaced with Ui::default().
This commit is contained in:
Yuya Nishihara 2023-01-01 15:56:31 +09:00
parent 48356817ba
commit da95ae65be
2 changed files with 11 additions and 1 deletions

View file

@ -1475,7 +1475,7 @@ pub fn create_ui() -> (Ui, Result<(), CommandError>) {
match read_config() {
Ok(user_settings) => (Ui::for_terminal(user_settings), Ok(())),
Err(err) => {
let ui = Ui::for_terminal(UserSettings::from_config(crate::config::default_config()));
let ui = Ui::new();
(ui, Err(CommandError::ConfigError(err.to_string())))
}
}

View file

@ -114,7 +114,17 @@ fn pager_setting(settings: &UserSettings) -> FullCommandArgs {
.unwrap_or_else(|_| "less -FRX".into())
}
impl Default for Ui {
fn default() -> Self {
Self::new()
}
}
impl Ui {
pub fn new() -> Ui {
Self::for_terminal(UserSettings::from_config(crate::config::default_config()))
}
pub fn for_terminal(settings: UserSettings) -> Ui {
let cwd = std::env::current_dir().unwrap();
let color = use_color(color_setting(&settings));