Expose new theme selection in CLI

This commit is contained in:
Tau Gärtli 2024-07-18 15:38:28 +02:00
parent cda363a3f7
commit cea45e05f3
No known key found for this signature in database
8 changed files with 227 additions and 33 deletions

51
Cargo.lock generated
View File

@ -149,6 +149,7 @@ dependencies = [
"shell-words", "shell-words",
"syntect", "syntect",
"tempfile", "tempfile",
"terminal-colorsaurus",
"thiserror", "thiserror",
"toml", "toml",
"unicode-width", "unicode-width",
@ -620,6 +621,12 @@ version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12"
[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]] [[package]]
name = "home" name = "home"
version = "0.5.9" version = "0.5.9"
@ -688,9 +695,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.149" version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]] [[package]]
name = "libgit2-sys" name = "libgit2-sys"
@ -755,9 +762,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
@ -768,6 +775,18 @@ dependencies = [
"adler", "adler",
] ]
[[package]]
name = "mio"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4929e1f84c5e54c3ec6141cd5d8b5a5c055f031f80cf78f2072920173cb4d880"
dependencies = [
"hermit-abi",
"libc",
"wasi",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.26.4" version = "0.26.4"
@ -1309,6 +1328,30 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "terminal-colorsaurus"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f99bb1dc5cde9eada5a8f466641240f9d5b9f55291d675df4160b097fbfa42e"
dependencies = [
"cfg-if",
"libc",
"memchr",
"mio",
"terminal-trx",
]
[[package]]
name = "terminal-trx"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d4c86910e10c782a02d3b7606de43cf7ebd80e1fafdca8e49a0db2b0d4611f0"
dependencies = [
"cfg-if",
"libc",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "terminal_size" name = "terminal_size"
version = "0.3.0" version = "0.3.0"

View File

@ -20,6 +20,7 @@ application = [
"bugreport", "bugreport",
"build-assets", "build-assets",
"git", "git",
"detect-color-scheme",
"minimal-application", "minimal-application",
] ]
# Mainly for developers that want to iterate quickly # Mainly for developers that want to iterate quickly
@ -35,6 +36,7 @@ git = ["git2"] # Support indicating git modifications
paging = ["shell-words", "grep-cli"] # Support applying a pager on the output paging = ["shell-words", "grep-cli"] # Support applying a pager on the output
lessopen = ["run_script", "os_str_bytes"] # Support $LESSOPEN preprocessor lessopen = ["run_script", "os_str_bytes"] # Support $LESSOPEN preprocessor
build-assets = ["syntect/yaml-load", "syntect/plist-load", "regex", "walkdir"] build-assets = ["syntect/yaml-load", "syntect/plist-load", "regex", "walkdir"]
detect-color-scheme = ["dep:terminal-colorsaurus"]
# You need to use one of these if you depend on bat as a library: # You need to use one of these if you depend on bat as a library:
regex-onig = ["syntect/regex-onig"] # Use the "oniguruma" regex engine regex-onig = ["syntect/regex-onig"] # Use the "oniguruma" regex engine
@ -68,6 +70,7 @@ bytesize = { version = "1.3.0" }
encoding_rs = "0.8.34" encoding_rs = "0.8.34"
os_str_bytes = { version = "~7.0", optional = true } os_str_bytes = { version = "~7.0", optional = true }
run_script = { version = "^0.10.1", optional = true} run_script = { version = "^0.10.1", optional = true}
terminal-colorsaurus = { version = "0.4", optional = true }
[dependencies.git2] [dependencies.git2]
version = "0.18" version = "0.18"

View File

@ -109,9 +109,39 @@ Options:
'bat --ignored-suffix ".dev" my_file.json.dev' will use JSON syntax, and ignore '.dev' 'bat --ignored-suffix ".dev" my_file.json.dev' will use JSON syntax, and ignore '.dev'
--theme <theme> --theme <theme>
Set the theme for syntax highlighting. Use '--list-themes' to see all available themes. To Set the theme for syntax highlighting. Note that this option overrides '--theme-dark' and
set a default theme, add the '--theme="..."' option to the configuration file or export '--theme-light'. Use '--list-themes' to see all available themes. To set a default theme,
the BAT_THEME environment variable (e.g.: export BAT_THEME="..."). add the '--theme="..."' option to the configuration file or export the BAT_THEME
environment variable (e.g.: export BAT_THEME="...").
--detect-color-scheme <when>
Specify when to query the terminal for its colors in order to pick an appropriate 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.
Possible values:
* auto (default):
Only query the terminals colors if the output is not redirected. This is to prevent
race conditions with pagers such as less.
* never
Never query the terminal for its colors and assume that the terminal has a dark
background.
* always
Always query the terminal for its colors, regardless of whether or not the output is
redirected.
--theme-light <theme>
Sets the theme name for syntax highlighting used when the terminal uses a light
background. Use '--list-themes' to see all available themes. To set a default theme, add
the '--theme-light="..." option to the configuration file or export the BAT_THEME_LIGHT
environment variable (e.g. export BAT_THEME_LIGHT="...").
--theme-dark <theme>
Sets the theme name for syntax highlighting used when the terminal uses a dark background.
Use '--list-themes' to see all available themes. To set a default theme, add the
'--theme-dark="..." option to the configuration file or export the BAT_THEME_DARK
environment variable (e.g. export BAT_THEME_DARK="...").
--list-themes --list-themes
Display a list of supported themes for syntax highlighting. Display a list of supported themes for syntax highlighting.

View File

@ -41,6 +41,12 @@ Options:
Use the specified syntax for files matching the glob pattern ('*.cpp:C++'). Use the specified syntax for files matching the glob pattern ('*.cpp:C++').
--theme <theme> --theme <theme>
Set the color theme for syntax highlighting. Set the color theme for syntax highlighting.
--detect-color-scheme <when>
Specify when to query the terminal for its colors.
--theme-light <theme>
Sets the color theme for syntax highlighting used for light backgrounds.
--theme-dark <theme>
Sets the color theme for syntax highlighting used for dark backgrounds.
--list-themes --list-themes
Display all supported highlighting themes. Display all supported highlighting themes.
-s, --squeeze-blank -s, --squeeze-blank

View File

@ -9,6 +9,9 @@ use crate::{
config::{get_args_from_config_file, get_args_from_env_opts_var, get_args_from_env_vars}, config::{get_args_from_config_file, get_args_from_env_opts_var, get_args_from_env_vars},
}; };
use bat::style::StyleComponentList; use bat::style::StyleComponentList;
use bat::theme::{
theme, ColorScheme, ColorSchemeDetector, DetectColorScheme, ThemeOptions, ThemeRequest,
};
use bat::StripAnsiMode; use bat::StripAnsiMode;
use clap::ArgMatches; use clap::ArgMatches;
@ -16,7 +19,6 @@ use console::Term;
use crate::input::{new_file_input, new_stdin_input}; use crate::input::{new_file_input, new_stdin_input};
use bat::{ use bat::{
assets::HighlightingAssets,
bat_warning, bat_warning,
config::{Config, VisibleLines}, config::{Config, VisibleLines},
error::*, error::*,
@ -254,18 +256,7 @@ impl App {
Some("auto") => StripAnsiMode::Auto, Some("auto") => StripAnsiMode::Auto,
_ => unreachable!("other values for --strip-ansi are not allowed"), _ => unreachable!("other values for --strip-ansi are not allowed"),
}, },
theme: self theme: theme(self.theme_options(), &TerminalColorSchemeDetector),
.matches
.get_one::<String>("theme")
.map(String::from)
.map(|s| {
if s == "default" {
String::from(HighlightingAssets::default_theme())
} else {
s
}
})
.unwrap_or_else(|| String::from(HighlightingAssets::default_theme())),
visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default() visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default()
&& self.matches.get_flag("diff") && self.matches.get_flag("diff")
{ {
@ -424,4 +415,72 @@ impl App {
Ok(styled_components) Ok(styled_components)
} }
fn theme_options(&self) -> ThemeOptions {
let theme = self
.matches
.get_one::<String>("theme")
.map(|t| ThemeRequest::from_str(t).unwrap());
let theme_dark = self
.matches
.get_one::<String>("theme-dark")
.map(|t| ThemeRequest::from_str(t).unwrap());
let theme_light = self
.matches
.get_one::<String>("theme-light")
.map(|t| ThemeRequest::from_str(t).unwrap());
let detect_color_scheme = match self
.matches
.get_one::<String>("detect-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"),
};
ThemeOptions {
theme,
theme_dark,
theme_light,
detect_color_scheme,
}
}
}
struct TerminalColorSchemeDetector;
#[cfg(feature = "detect-color-scheme")]
impl ColorSchemeDetector for TerminalColorSchemeDetector {
fn should_detect(&self) -> bool {
// Querying the terminal for its colors via OSC 10 / OSC 11 requires "exclusive" access
// since we read/write from the terminal and enable/disable raw mode.
// This causes race conditions with pagers such as less when they are attached to the
// same terminal as us.
//
// This is usually only an issue when the output is manually piped to a pager.
// For example: `bat Cargo.toml | less`.
// Otherwise, if we start the pager ourselves, then there's no race condition
// since the pager is started *after* the color is detected.
std::io::stdout().is_terminal()
}
fn detect(&self) -> Option<ColorScheme> {
use terminal_colorsaurus::{color_scheme, ColorScheme as ColorsaurusScheme, QueryOptions};
match color_scheme(QueryOptions::default()).ok()? {
ColorsaurusScheme::Dark => Some(ColorScheme::Dark),
ColorsaurusScheme::Light => Some(ColorScheme::Light),
}
}
}
#[cfg(not(feature = "detect-color-scheme"))]
impl ColorSchemeDetector for TerminalColorSchemeDetector {
fn should_detect(&self) -> bool {
false
}
fn detect(&self) -> Option<ColorScheme> {
None
}
} }

View File

@ -375,13 +375,64 @@ pub fn build_app(interactive_output: bool) -> Command {
.overrides_with("theme") .overrides_with("theme")
.help("Set the color theme for syntax highlighting.") .help("Set the color theme for syntax highlighting.")
.long_help( .long_help(
"Set the theme for syntax highlighting. Use '--list-themes' to \ "Set the theme for syntax highlighting. Note that this option overrides \
'--theme-dark' and '--theme-light'. Use '--list-themes' to \
see all available themes. To set a default theme, add the \ see all available themes. To set a default theme, add the \
'--theme=\"...\"' option to the configuration file or export the \ '--theme=\"...\"' option to the configuration file or export the \
BAT_THEME environment variable (e.g.: export \ BAT_THEME environment variable (e.g.: export \
BAT_THEME=\"...\").", BAT_THEME=\"...\").",
), ),
) )
.arg(
Arg::new("detect-color-scheme")
.long("detect-color-scheme")
.overrides_with("detect-color-scheme")
.value_name("when")
.value_parser(["auto", "never", "always"])
.default_value("auto")
.hide_default_value(true)
.help("Specify when to query the terminal for its colors.")
.long_help(
"Specify when to query the terminal for its colors \
in order to pick an appropriate 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\
Possible values:\n\
* auto (default):\n \
Only query the terminals colors 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."),
)
.arg(
Arg::new("theme-light")
.long("theme-light")
.overrides_with("theme-light")
.value_name("theme")
.help("Sets the color theme for syntax highlighting used for light backgrounds.")
.long_help(
"Sets the theme name for syntax highlighting used when the terminal uses a light background. \
Use '--list-themes' to see all available themes. To set a default theme, add the \
'--theme-light=\"...\" option to the configuration file or export the BAT_THEME_LIGHT \
environment variable (e.g. export BAT_THEME_LIGHT=\"...\")."),
)
.arg(
Arg::new("theme-dark")
.long("theme-dark")
.overrides_with("theme-dark")
.value_name("theme")
.help("Sets the color theme for syntax highlighting used for dark backgrounds.")
.long_help(
"Sets the theme name for syntax highlighting used when the terminal uses a dark background. \
Use '--list-themes' to see all available themes. To set a default theme, add the \
'--theme-dark=\"...\" option to the configuration file or export the BAT_THEME_DARK \
environment variable (e.g. export BAT_THEME_DARK=\"...\")."),
)
.arg( .arg(
Arg::new("list-themes") Arg::new("list-themes")
.long("list-themes") .long("list-themes")

View File

@ -141,6 +141,8 @@ pub fn get_args_from_env_vars() -> Vec<OsString> {
[ [
("--tabs", "BAT_TABS"), ("--tabs", "BAT_TABS"),
("--theme", "BAT_THEME"), ("--theme", "BAT_THEME"),
("--theme-dark", "BAT_THEME_DARK"),
("--theme-light", "BAT_THEME_LIGHT"),
("--pager", "BAT_PAGER"), ("--pager", "BAT_PAGER"),
("--paging", "BAT_PAGING"), ("--paging", "BAT_PAGING"),
("--style", "BAT_STYLE"), ("--style", "BAT_STYLE"),

View File

@ -18,8 +18,8 @@ pub fn theme(options: ThemeOptions, detector: &dyn ColorSchemeDetector) -> Strin
fn choose_theme(options: ThemeOptions, color_scheme: ColorScheme) -> Option<ThemeRequest> { fn choose_theme(options: ThemeOptions, color_scheme: ColorScheme) -> Option<ThemeRequest> {
match color_scheme { match color_scheme {
ColorScheme::Dark => options.dark_theme, ColorScheme::Dark => options.theme_dark,
ColorScheme::Light => options.light_theme, ColorScheme::Light => options.theme_light,
} }
} }
@ -45,9 +45,9 @@ pub struct ThemeOptions {
/// Always use this theme regardless of the terminal's background color. /// Always use this theme regardless of the terminal's background color.
pub theme: Option<ThemeRequest>, pub theme: Option<ThemeRequest>,
/// The theme to use in case the terminal uses a dark background with light text. /// The theme to use in case the terminal uses a dark background with light text.
pub dark_theme: Option<ThemeRequest>, pub theme_dark: Option<ThemeRequest>,
/// The theme to use in case the terminal uses a light background with dark text. /// The theme to use in case the terminal uses a light background with dark text.
pub light_theme: Option<ThemeRequest>, pub theme_light: Option<ThemeRequest>,
/// Detect whether or not the terminal is dark or light by querying for its colors. /// Detect whether or not the terminal is dark or light by querying for its colors.
pub detect_color_scheme: DetectColorScheme, pub detect_color_scheme: DetectColorScheme,
} }
@ -182,8 +182,8 @@ mod tests {
}, },
ThemeOptions { ThemeOptions {
theme: Some(ThemeRequest::Named("Theme".to_string())), theme: Some(ThemeRequest::Named("Theme".to_string())),
dark_theme: Some(ThemeRequest::Named("Dark Theme".to_string())), theme_dark: Some(ThemeRequest::Named("Dark Theme".to_string())),
light_theme: Some(ThemeRequest::Named("Light Theme".to_string())), theme_light: Some(ThemeRequest::Named("Light Theme".to_string())),
..Default::default() ..Default::default()
}, },
] { ] {
@ -237,8 +237,8 @@ mod tests {
for options in [ for options in [
ThemeOptions::default(), ThemeOptions::default(),
ThemeOptions { ThemeOptions {
dark_theme: Some(ThemeRequest::Default), theme_dark: Some(ThemeRequest::Default),
light_theme: Some(ThemeRequest::Default), theme_light: Some(ThemeRequest::Default),
..Default::default() ..Default::default()
}, },
] { ] {
@ -256,8 +256,8 @@ mod tests {
fn chooses_dark_theme_if_dark_or_unknown() { fn chooses_dark_theme_if_dark_or_unknown() {
for color_scheme in [Some(Dark), None] { for color_scheme in [Some(Dark), None] {
let options = ThemeOptions { let options = ThemeOptions {
dark_theme: Some(ThemeRequest::Named("Dark".to_string())), theme_dark: Some(ThemeRequest::Named("Dark".to_string())),
light_theme: Some(ThemeRequest::Named("Light".to_string())), theme_light: Some(ThemeRequest::Named("Light".to_string())),
..Default::default() ..Default::default()
}; };
let detector = ConstantDetector(color_scheme); let detector = ConstantDetector(color_scheme);
@ -268,8 +268,8 @@ mod tests {
#[test] #[test]
fn chooses_light_theme_if_light() { fn chooses_light_theme_if_light() {
let options = ThemeOptions { let options = ThemeOptions {
dark_theme: Some(ThemeRequest::Named("Dark".to_string())), theme_dark: Some(ThemeRequest::Named("Dark".to_string())),
light_theme: Some(ThemeRequest::Named("Light".to_string())), theme_light: Some(ThemeRequest::Named("Light".to_string())),
..Default::default() ..Default::default()
}; };
let detector = ConstantDetector(Some(ColorScheme::Light)); let detector = ConstantDetector(Some(ColorScheme::Light));