forked from mirrors/jj
cli: report invalid ui.pager config value
If you set e.g.`ui.pager = 5`, we currently ignore that and fall back to the default. It seems better to let the user know that their config is invalid, as we generally do. This commit does that. With this change, the error message will look like this: ``` Config error: Invalid `ui.pager`: data did not match any variant of untagged enum CommandNameAndArgs ``` Maybe the key name will be redundant once the `config` crate releases a version including https://github.com/mehcode/config-rs/pull/413 (thanks, Yuya).
This commit is contained in:
parent
5fe5991ca8
commit
5ae6698f81
1 changed files with 18 additions and 9 deletions
27
src/ui.rs
27
src/ui.rs
|
@ -20,6 +20,7 @@ use std::{env, fmt, io, mem};
|
||||||
use crossterm::tty::IsTty;
|
use crossterm::tty::IsTty;
|
||||||
use maplit::hashmap;
|
use maplit::hashmap;
|
||||||
|
|
||||||
|
use crate::cli_util::CommandError;
|
||||||
use crate::config::{CommandNameAndArgs, NonEmptyCommandArgsVec};
|
use crate::config::{CommandNameAndArgs, NonEmptyCommandArgsVec};
|
||||||
use crate::formatter::{Formatter, FormatterFactory, LabeledWriter};
|
use crate::formatter::{Formatter, FormatterFactory, LabeledWriter};
|
||||||
|
|
||||||
|
@ -91,18 +92,26 @@ pub enum PaginationChoice {
|
||||||
Auto,
|
Auto,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pager_setting(config: &config::Config) -> CommandNameAndArgs {
|
fn pager_setting(config: &config::Config) -> Result<CommandNameAndArgs, CommandError> {
|
||||||
config
|
config
|
||||||
.get("ui.pager")
|
.get::<CommandNameAndArgs>("ui.pager")
|
||||||
.unwrap_or_else(|_| CommandNameAndArgs::Structured {
|
.or_else(|err| match err {
|
||||||
command: NonEmptyCommandArgsVec::try_from(vec!["less".to_string(), "-FRX".to_string()])
|
config::ConfigError::NotFound(_) => Ok(CommandNameAndArgs::Structured {
|
||||||
|
command: NonEmptyCommandArgsVec::try_from(vec![
|
||||||
|
"less".to_string(),
|
||||||
|
"-FRX".to_string(),
|
||||||
|
])
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
env: hashmap! { "LESSCHARSET".to_string() => "utf-8".to_string() },
|
env: hashmap! { "LESSCHARSET".to_string() => "utf-8".to_string() },
|
||||||
|
}),
|
||||||
|
err => Err(CommandError::ConfigError(format!(
|
||||||
|
"Invalid `ui.pager`: {err:?}"
|
||||||
|
))),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ui {
|
impl Ui {
|
||||||
pub fn with_config(config: &config::Config) -> Result<Ui, config::ConfigError> {
|
pub fn with_config(config: &config::Config) -> Result<Ui, CommandError> {
|
||||||
let color = use_color(color_setting(config));
|
let color = use_color(color_setting(config));
|
||||||
// Sanitize ANSI escape codes if we're printing to a terminal. Doesn't affect
|
// Sanitize ANSI escape codes if we're printing to a terminal. Doesn't affect
|
||||||
// ANSI escape codes that originate from the formatter itself.
|
// ANSI escape codes that originate from the formatter itself.
|
||||||
|
@ -112,16 +121,16 @@ impl Ui {
|
||||||
Ok(Ui {
|
Ok(Ui {
|
||||||
color,
|
color,
|
||||||
formatter_factory,
|
formatter_factory,
|
||||||
pager_cmd: pager_setting(config),
|
pager_cmd: pager_setting(config)?,
|
||||||
paginate: PaginationChoice::Auto,
|
paginate: PaginationChoice::Auto,
|
||||||
progress_indicator,
|
progress_indicator,
|
||||||
output: UiOutput::new_terminal(),
|
output: UiOutput::new_terminal(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self, config: &config::Config) -> Result<(), config::ConfigError> {
|
pub fn reset(&mut self, config: &config::Config) -> Result<(), CommandError> {
|
||||||
self.color = use_color(color_setting(config));
|
self.color = use_color(color_setting(config));
|
||||||
self.pager_cmd = pager_setting(config);
|
self.pager_cmd = pager_setting(config)?;
|
||||||
self.progress_indicator = progress_indicator_setting(config);
|
self.progress_indicator = progress_indicator_setting(config);
|
||||||
let sanitize = io::stdout().is_tty();
|
let sanitize = io::stdout().is_tty();
|
||||||
self.formatter_factory = FormatterFactory::prepare(config, self.color, sanitize)?;
|
self.formatter_factory = FormatterFactory::prepare(config, self.color, sanitize)?;
|
||||||
|
|
Loading…
Reference in a new issue