1
0
mirror of https://github.com/sharkdp/bat synced 2026-08-04 19:01:44 +00:00

Generalize --detect-color-scheme to --color-scheme

This commit is contained in:
Tau Gärtli
2024-07-18 17:36:57 +02:00
parent abf9dada04
commit b9b981f657
11 changed files with 140 additions and 69 deletions
+10 -8
View File
@@ -9,7 +9,7 @@ use crate::{
config::{get_args_from_config_file, get_args_from_env_opts_var, get_args_from_env_vars},
};
use bat::style::StyleComponentList;
use bat::theme::{theme, DetectColorScheme, ThemeOptions, ThemeRequest};
use bat::theme::{theme, ColorSchemePreference, DetectColorScheme, ThemeOptions, ThemeRequest};
use bat::StripAnsiMode;
use clap::ArgMatches;
@@ -431,20 +431,22 @@ impl App {
theme,
theme_dark,
theme_light,
detect_color_scheme: self.detect_color_scheme(),
color_scheme: self.color_scheme_preference(),
}
}
pub(crate) fn detect_color_scheme(&self) -> DetectColorScheme {
pub(crate) fn color_scheme_preference(&self) -> ColorSchemePreference {
match self
.matches
.get_one::<String>("detect-color-scheme")
.get_one::<String>("color-scheme")
.map(|s| s.as_str())
{
Some("auto") => DetectColorScheme::Auto,
Some("never") => DetectColorScheme::Never,
Some("always") => DetectColorScheme::Always,
_ => unreachable!("other values for --detect-color-scheme are not allowed"),
Some("auto") => ColorSchemePreference::Auto(DetectColorScheme::Auto),
Some("auto:always") => ColorSchemePreference::Auto(DetectColorScheme::Always),
Some("dark") => ColorSchemePreference::Dark,
Some("light") => ColorSchemePreference::Light,
Some("system") => ColorSchemePreference::System,
_ => unreachable!("other values for --color-scheme are not allowed"),
}
}
}
+17 -17
View File
@@ -384,30 +384,30 @@ pub fn build_app(interactive_output: bool) -> Command {
),
)
.arg(
Arg::new("detect-color-scheme")
.long("detect-color-scheme")
.overrides_with("detect-color-scheme")
.value_name("when")
.value_parser(["auto", "never", "always"])
Arg::new("color-scheme")
.long("color-scheme")
.overrides_with("color-scheme")
.value_name("scheme")
.value_parser(["auto", "auto:always", "dark", "light", "system"])
.default_value("auto")
.hide_default_value(true)
.help("Specify when to query the terminal for its colors.")
.help("Specify whether to choose a dark or light theme.")
.long_help(
"Specify when to query the terminal for its colors \
in order to pick an appropriate syntax highlighting theme. \
"Specify whether to choose a dark or light syntax highlighting theme. \
Use '--theme-light' and '--theme-dark' (or the environment variables \
BAT_THEME_LIGHT and BAT_THEME_DARK) to configure which themes are picked. \
You may also use '--theme' to set a theme that is used regardless of the terminal's colors.\n\n\
You may also use '--theme' to set a theme that is used regardless of this choice.\n\n\
Possible values:\n\
* auto (default):\n \
Only query the terminals colors if the output is not redirected. \
* auto (default):\n \
Query the terminals for its color scheme if the output is not redirected. \
This is to prevent race conditions with pagers such as less.\n\
* never\n \
Never query the terminal for its colors \
and assume that the terminal has a dark background.\n\
* always\n \
Always query the terminal for its colors, \
regardless of whether or not the output is redirected."),
* 'auto:always':\n \
Always query the terminal for its color scheme, \
regardless of whether or not the output is redirected.\n\
* dark: Use a dark syntax highlighting theme.\n\
* light: Use a light syntax highlighting theme.\n\
* system: Query the OS for its color scheme. Only works on macOS.\n\
"),
)
.arg(
Arg::new("theme-light")
+9 -4
View File
@@ -35,7 +35,7 @@ use bat::{
error::*,
input::Input,
style::{StyleComponent, StyleComponents},
theme::{color_scheme, default_theme, ColorScheme, DetectColorScheme},
theme::{color_scheme, default_theme, ColorScheme, ColorSchemePreference},
MappingTarget, PagingMode,
};
@@ -193,7 +193,7 @@ pub fn list_themes(
cfg: &Config,
config_dir: &Path,
cache_dir: &Path,
detect_color_scheme: DetectColorScheme,
color_scheme_pref: ColorSchemePreference,
) -> Result<()> {
let assets = assets_from_cache_or_binary(cfg.use_custom_assets, cache_dir)?;
let mut config = cfg.clone();
@@ -205,7 +205,7 @@ pub fn list_themes(
let stdout = io::stdout();
let mut stdout = stdout.lock();
let default_theme_name = default_theme(color_scheme(detect_color_scheme));
let default_theme_name = default_theme(color_scheme(color_scheme_pref));
for theme in assets.themes() {
let default_theme_info = if default_theme_name == theme {
" (default)"
@@ -380,7 +380,12 @@ fn run() -> Result<bool> {
};
run_controller(inputs, &plain_config, cache_dir)
} else if app.matches.get_flag("list-themes") {
list_themes(&config, config_dir, cache_dir, app.detect_color_scheme())?;
list_themes(
&config,
config_dir,
cache_dir,
app.color_scheme_preference(),
)?;
Ok(true)
} else if app.matches.get_flag("config-file") {
println!("{}", config_file().to_string_lossy());