cli: initialize Ui without using UserSettings, use config::Config

UserSettings will be instantiated after both user and repo configs are
loaded. We might want to add a wrapper for CLI settings, but I have no idea
how that should be structured. Let's use bare config::Config until then.
This commit is contained in:
Yuya Nishihara 2023-01-04 21:58:24 +09:00
parent 3c99852ebd
commit d0d92a0e06
3 changed files with 22 additions and 30 deletions

View file

@ -1623,7 +1623,7 @@ fn handle_early_args(
}
if !args.config_toml.is_empty() {
settings.incorporate_toml_strings(&args.config_toml)?;
ui.reset(settings);
ui.reset(settings.config());
}
Ok(())
}
@ -1784,7 +1784,7 @@ impl CliRunner {
pub fn run(self, ui: &mut Ui) -> Result<(), CommandError> {
let cwd = env::current_dir().unwrap(); // TODO: maybe map_err to CommandError?
let mut settings = crate::config::read_config()?;
ui.reset(&settings);
ui.reset(settings.config());
let string_args = expand_args(&self.app, std::env::args_os(), &settings)?;
let (matches, args) = parse_args(
ui,

View file

@ -17,8 +17,6 @@ use std::io;
use std::io::{Error, Read, Write};
use std::sync::Arc;
use jujutsu_lib::settings::UserSettings;
// Lets the caller label strings and translates the labels to colors
pub trait Formatter: Write {
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
@ -68,9 +66,9 @@ enum FormatterFactoryKind {
}
impl FormatterFactory {
pub fn prepare(settings: &UserSettings, color: bool) -> Self {
pub fn prepare(config: &config::Config, color: bool) -> Self {
let kind = if color {
let colors = Arc::new(config_colors(settings));
let colors = Arc::new(config_colors(config));
FormatterFactoryKind::Color { colors }
} else {
FormatterFactoryKind::PlainText
@ -129,9 +127,9 @@ pub struct ColorFormatter<W> {
current_color: Vec<u8>,
}
fn config_colors(user_settings: &UserSettings) -> HashMap<String, String> {
fn config_colors(config: &config::Config) -> HashMap<String, String> {
let mut result = HashMap::new();
if let Ok(table) = user_settings.config().get_table("colors") {
if let Ok(table) = config.get_table("colors") {
for (key, value) in table {
result.insert(key, value.to_string());
}

View file

@ -18,7 +18,6 @@ use std::str::FromStr;
use std::{fmt, io, mem};
use crossterm::tty::IsTty;
use jujutsu_lib::settings::UserSettings;
use crate::config::FullCommandArgs;
use crate::formatter::{Formatter, FormatterFactory};
@ -32,11 +31,8 @@ pub struct Ui {
output: UiOutput,
}
fn progress_indicator_setting(settings: &UserSettings) -> bool {
settings
.config()
.get_bool("ui.progress-indicator")
.unwrap_or(true)
fn progress_indicator_setting(config: &config::Config) -> bool {
config.get_bool("ui.progress-indicator").unwrap_or(true)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@ -76,9 +72,8 @@ impl fmt::Display for ColorChoice {
}
}
fn color_setting(settings: &UserSettings) -> ColorChoice {
settings
.config()
fn color_setting(config: &config::Config) -> ColorChoice {
config
.get_string("ui.color")
.ok()
.and_then(|s| s.parse().ok())
@ -105,9 +100,8 @@ impl Default for PaginationChoice {
}
}
fn pager_setting(settings: &UserSettings) -> FullCommandArgs {
settings
.config()
fn pager_setting(config: &config::Config) -> FullCommandArgs {
config
.get("ui.pager")
.unwrap_or_else(|_| "less -FRX".into())
}
@ -120,25 +114,25 @@ impl Default for Ui {
impl Ui {
pub fn new() -> Ui {
let settings = UserSettings::from_config(crate::config::default_config());
let color = use_color(color_setting(&settings));
let progress_indicator = progress_indicator_setting(&settings);
let formatter_factory = FormatterFactory::prepare(&settings, color);
let config = crate::config::default_config();
let color = use_color(color_setting(&config));
let progress_indicator = progress_indicator_setting(&config);
let formatter_factory = FormatterFactory::prepare(&config, color);
Ui {
color,
formatter_factory,
pager_cmd: pager_setting(&settings),
pager_cmd: pager_setting(&config),
paginate: PaginationChoice::Auto,
progress_indicator,
output: UiOutput::new_terminal(),
}
}
pub fn reset(&mut self, settings: &UserSettings) {
self.color = use_color(color_setting(settings));
self.pager_cmd = pager_setting(settings);
self.progress_indicator = progress_indicator_setting(settings);
self.formatter_factory = FormatterFactory::prepare(settings, self.color);
pub fn reset(&mut self, config: &config::Config) {
self.color = use_color(color_setting(config));
self.pager_cmd = pager_setting(config);
self.progress_indicator = progress_indicator_setting(config);
self.formatter_factory = FormatterFactory::prepare(config, self.color);
}
/// Sets the pagination value.