From da95ae65be8f399ce42b6dbb87b158573aa42b01 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 1 Jan 2023 15:56:31 +0900 Subject: [PATCH] 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(). --- src/cli_util.rs | 2 +- src/ui.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index 212972474..fb623f49f 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -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()))) } } diff --git a/src/ui.rs b/src/ui.rs index 8e4de4b06..9424e0be1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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));