| 1 |
//! Delta doesn't have a formal concept of a "theme". What it has is |
| 2 |
//! |
| 3 |
//! 1. The choice of "theme". This is the language syntax highlighting theme; you have to make this |
| 4 |
//! choice when using `bat` also. |
| 5 |
//! 2. The choice of "light vs dark mode". This determines whether the background colors should be |
| 6 |
//! chosen for a light or dark terminal background. (`bat` has no equivalent.) |
| 7 |
//! |
| 8 |
//! Basically: |
| 9 |
//! 1. The theme is specified by the `--syntax-theme` option. If this isn't supplied then it is specified |
| 10 |
//! by the `BAT_THEME` environment variable. |
| 11 |
//! 2. Light vs dark mode is specified by the `--light` or `--dark` options. If these aren't |
| 12 |
//! supplied then it detected from the terminal. If this fails it is inferred from the chosen theme. |
| 13 |
//! |
| 14 |
//! In the absence of other factors, the default assumes a dark terminal background. |
| 15 |
|
| 16 |
use std::io::{stdout, IsTerminal}; |
| 17 |
|
| 18 |
use bat; |
| 19 |
use bat::assets::HighlightingAssets; |
| 20 |
#[cfg(not(test))] |
| 21 |
use terminal_colorsaurus::{color_scheme, QueryOptions}; |
| 22 |
|
| 23 |
use crate::cli::{self, DetectDarkLight}; |
| 24 |
use crate::color::{ColorMode, ColorMode::*}; |
| 25 |
|
| 26 |
#[allow(non_snake_case)] |
| 27 |
pub fn set__color_mode__syntax_theme__syntax_set(opt: &mut cli::Opt, assets: HighlightingAssets) { |
| 28 |
let (color_mode, syntax_theme_name) = |
| 29 |
get_color_mode_and_syntax_theme_name(opt.syntax_theme.as_ref(), get_color_mode(opt)); |
| 30 |
opt.computed.color_mode = color_mode; |
| 31 |
|
| 32 |
opt.computed.syntax_theme = if is_no_syntax_highlighting_syntax_theme_name(&syntax_theme_name) { |
| 33 |
None |
| 34 |
} else { |
| 35 |
Some(assets.get_theme(&syntax_theme_name).clone()) |
| 36 |
}; |
| 37 |
opt.computed.syntax_set = assets.get_syntax_set().unwrap().clone(); |
| 38 |
} |
| 39 |
|
| 40 |
pub fn is_light_syntax_theme(theme: &str) -> bool { |
| 41 |
LIGHT_SYNTAX_THEMES.contains(&theme) || theme.to_lowercase().contains("light") |
| 42 |
} |
| 43 |
|
| 44 |
pub fn color_mode_from_syntax_theme(theme: &str) -> ColorMode { |
| 45 |
if is_light_syntax_theme(theme) { |
| 46 |
ColorMode::Light |
| 47 |
} else { |
| 48 |
ColorMode::Dark |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
const LIGHT_SYNTAX_THEMES: [&str; 7] = [ |
| 53 |
"Catppuccin Latte", |
| 54 |
"GitHub", |
| 55 |
"gruvbox-light", |
| 56 |
"gruvbox-white", |
| 57 |
"Monokai Extended Light", |
| 58 |
"OneHalfLight", |
| 59 |
"Solarized (light)", |
| 60 |
]; |
| 61 |
|
| 62 |
const DEFAULT_LIGHT_SYNTAX_THEME: &str = "GitHub"; |
| 63 |
const DEFAULT_DARK_SYNTAX_THEME: &str = "Monokai Extended"; |
| 64 |
|
| 65 |
fn is_no_syntax_highlighting_syntax_theme_name(theme_name: &str) -> bool { |
| 66 |
theme_name.to_lowercase() == "none" |
| 67 |
} |
| 68 |
|
| 69 |
/// Return a (theme_name, color_mode) tuple. |
| 70 |
/// theme_name == None in return value means syntax highlighting is disabled. |
| 71 |
fn get_color_mode_and_syntax_theme_name( |
| 72 |
syntax_theme: Option<&String>, |
| 73 |
mode: Option<ColorMode>, |
| 74 |
) -> (ColorMode, String) { |
| 75 |
match (syntax_theme, mode) { |
| 76 |
(Some(theme), None) => (color_mode_from_syntax_theme(theme), theme.to_string()), |
| 77 |
(Some(theme), Some(mode)) => (mode, theme.to_string()), |
| 78 |
(None, None | Some(Dark)) => (Dark, DEFAULT_DARK_SYNTAX_THEME.to_string()), |
| 79 |
(None, Some(Light)) => (Light, DEFAULT_LIGHT_SYNTAX_THEME.to_string()), |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
fn get_color_mode(opt: &cli::Opt) -> Option<ColorMode> { |
| 84 |
if opt.light { |
| 85 |
Some(Light) |
| 86 |
} else if opt.dark { |
| 87 |
Some(Dark) |
| 88 |
} else if should_detect_color_mode(opt) { |
| 89 |
detect_color_mode() |
| 90 |
} else { |
| 91 |
None |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/// See [`cli::Opt::detect_dark_light`] for a detailed explanation. |
| 96 |
fn should_detect_color_mode(opt: &cli::Opt) -> bool { |
| 97 |
match opt.detect_dark_light { |
| 98 |
DetectDarkLight::Auto => opt.color_only || stdout().is_terminal(), |
| 99 |
DetectDarkLight::Always => true, |
| 100 |
DetectDarkLight::Never => false, |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[cfg(not(test))] |
| 105 |
fn detect_color_mode() -> Option<ColorMode> { |
| 106 |
color_scheme(QueryOptions::default()) |
| 107 |
.ok() |
| 108 |
.map(ColorMode::from) |
| 109 |
} |
| 110 |
|
| 111 |
impl From<terminal_colorsaurus::ColorScheme> for ColorMode { |
| 112 |
fn from(value: terminal_colorsaurus::ColorScheme) -> Self { |
| 113 |
match value { |
| 114 |
terminal_colorsaurus::ColorScheme::Dark => ColorMode::Dark, |
| 115 |
terminal_colorsaurus::ColorScheme::Light => ColorMode::Light, |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(test)] |
| 121 |
fn detect_color_mode() -> Option<ColorMode> { |
| 122 |
None |
| 123 |
} |
| 124 |
|
| 125 |
#[cfg(test)] |
| 126 |
mod tests { |
| 127 |
use super::*; |
| 128 |
use crate::color; |
| 129 |
use crate::tests::integration_test_utils; |
| 130 |
|
| 131 |
// TODO: Test influence of BAT_THEME env var. E.g. see utils::process::tests::FakeParentArgs. |
| 132 |
#[test] |
| 133 |
fn test_syntax_theme_selection() { |
| 134 |
for ( |
| 135 |
syntax_theme, |
| 136 |
mode, // (--light, --dark) |
| 137 |
expected_syntax_theme, |
| 138 |
expected_mode, |
| 139 |
) in vec![ |
| 140 |
(None, None, DEFAULT_DARK_SYNTAX_THEME, Dark), |
| 141 |
(Some("GitHub"), None, "GitHub", Light), |
| 142 |
(Some("Nord"), None, "Nord", Dark), |
| 143 |
(None, Some(Dark), DEFAULT_DARK_SYNTAX_THEME, Dark), |
| 144 |
(None, Some(Light), DEFAULT_LIGHT_SYNTAX_THEME, Light), |
| 145 |
(Some("GitHub"), Some(Light), "GitHub", Light), |
| 146 |
(Some("GitHub"), Some(Dark), "GitHub", Dark), |
| 147 |
(Some("Nord"), Some(Light), "Nord", Light), |
| 148 |
(Some("Nord"), Some(Dark), "Nord", Dark), |
| 149 |
(Some("none"), None, "none", Dark), |
| 150 |
(Some("none"), Some(Dark), "none", Dark), |
| 151 |
(Some("None"), Some(Light), "none", Light), |
| 152 |
] { |
| 153 |
let mut args = vec![]; |
| 154 |
if let Some(syntax_theme) = syntax_theme { |
| 155 |
args.push("--syntax-theme"); |
| 156 |
args.push(syntax_theme); |
| 157 |
} |
| 158 |
let is_true_color = true; |
| 159 |
if is_true_color { |
| 160 |
args.push("--true-color"); |
| 161 |
args.push("always"); |
| 162 |
} else { |
| 163 |
args.push("--true-color"); |
| 164 |
args.push("never"); |
| 165 |
} |
| 166 |
match mode { |
| 167 |
Some(Light) => { |
| 168 |
args.push("--light"); |
| 169 |
} |
| 170 |
Some(Dark) => { |
| 171 |
args.push("--dark"); |
| 172 |
} |
| 173 |
None => {} |
| 174 |
} |
| 175 |
let config = integration_test_utils::make_config_from_args(&args); |
| 176 |
assert_eq!( |
| 177 |
&config |
| 178 |
.syntax_theme |
| 179 |
.clone() |
| 180 |
.map(|t| t.name.unwrap()) |
| 181 |
.unwrap_or("none".to_string()), |
| 182 |
expected_syntax_theme |
| 183 |
); |
| 184 |
if is_no_syntax_highlighting_syntax_theme_name(expected_syntax_theme) { |
| 185 |
assert!(config.syntax_theme.is_none()) |
| 186 |
} else { |
| 187 |
assert_eq!( |
| 188 |
config.syntax_theme.unwrap().name.as_ref().unwrap(), |
| 189 |
expected_syntax_theme |
| 190 |
); |
| 191 |
} |
| 192 |
assert_eq!( |
| 193 |
config.minus_style.ansi_term_style.background.unwrap(), |
| 194 |
color::get_minus_background_color_default(expected_mode, is_true_color) |
| 195 |
); |
| 196 |
assert_eq!( |
| 197 |
config.minus_emph_style.ansi_term_style.background.unwrap(), |
| 198 |
color::get_minus_emph_background_color_default(expected_mode, is_true_color) |
| 199 |
); |
| 200 |
assert_eq!( |
| 201 |
config.plus_style.ansi_term_style.background.unwrap(), |
| 202 |
color::get_plus_background_color_default(expected_mode, is_true_color) |
| 203 |
); |
| 204 |
assert_eq!( |
| 205 |
config.plus_emph_style.ansi_term_style.background.unwrap(), |
| 206 |
color::get_plus_emph_background_color_default(expected_mode, is_true_color) |
| 207 |
); |
| 208 |
} |
| 209 |
} |
| 210 |
} |