Debian Codegraph @

src/options/theme.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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
21 use crate::cli::{self, DetectDarkLight};
22 use crate::color::{ColorMode, ColorMode::*};
23
24 #[allow(non_snake_case)]
25 pub fn set__color_mode__syntax_theme__syntax_set(opt: &mut cli::Opt, assets: HighlightingAssets) {
26 let (color_mode, syntax_theme_name) =
27 get_color_mode_and_syntax_theme_name(opt.syntax_theme.as_ref(), get_color_mode(opt));
28 opt.computed.color_mode = color_mode;
29
30 opt.computed.syntax_theme = if is_no_syntax_highlighting_syntax_theme_name(&syntax_theme_name) {
31 None
32 } else {
33 Some(assets.get_theme(&syntax_theme_name).clone())
34 };
35 opt.computed.syntax_set = assets.get_syntax_set().unwrap().clone();
36 }
37
38 pub fn is_light_syntax_theme(theme: &str) -> bool {
39 LIGHT_SYNTAX_THEMES.contains(&theme) || theme.to_lowercase().contains("light")
40 }
41
42 pub fn color_mode_from_syntax_theme(theme: &str) -> ColorMode {
43 if is_light_syntax_theme(theme) {
44 ColorMode::Light
45 } else {
46 ColorMode::Dark
47 }
48 }
49
50 const LIGHT_SYNTAX_THEMES: [&str; 7] = [
51 "Catppuccin Latte",
52 "GitHub",
53 "gruvbox-light",
54 "gruvbox-white",
55 "Monokai Extended Light",
56 "OneHalfLight",
57 "Solarized (light)",
58 ];
59
60 const DEFAULT_LIGHT_SYNTAX_THEME: &str = "GitHub";
61 const DEFAULT_DARK_SYNTAX_THEME: &str = "Monokai Extended";
62
63 fn is_no_syntax_highlighting_syntax_theme_name(theme_name: &str) -> bool {
64 theme_name.to_lowercase() == "none"
65 }
66
67 /// Return a (theme_name, color_mode) tuple.
68 /// theme_name == None in return value means syntax highlighting is disabled.
69 fn get_color_mode_and_syntax_theme_name(
70 syntax_theme: Option<&String>,
71 mode: Option<ColorMode>,
72 ) -> (ColorMode, String) {
73 match (syntax_theme, mode) {
74 (Some(theme), None) => (color_mode_from_syntax_theme(theme), theme.to_string()),
75 (Some(theme), Some(mode)) => (mode, theme.to_string()),
76 (None, None | Some(Dark)) => (Dark, DEFAULT_DARK_SYNTAX_THEME.to_string()),
77 (None, Some(Light)) => (Light, DEFAULT_LIGHT_SYNTAX_THEME.to_string()),
78 }
79 }
80
81 fn get_color_mode(opt: &cli::Opt) -> Option<ColorMode> {
82 if opt.light {
83 Some(Light)
84 } else if opt.dark {
85 Some(Dark)
86 } else if should_detect_color_mode(opt) {
87 detect_color_mode()
88 } else {
89 None
90 }
91 }
92
93 /// See [`cli::Opt::detect_dark_light`] for a detailed explanation.
94 fn should_detect_color_mode(opt: &cli::Opt) -> bool {
95 match opt.detect_dark_light {
96 DetectDarkLight::Auto => opt.color_only || stdout().is_terminal(),
97 DetectDarkLight::Always => true,
98 DetectDarkLight::Never => false,
99 }
100 }
101
102 fn detect_color_mode() -> Option<ColorMode> {
103 None
104 }
105
106 #[cfg(test)]
107 mod tests {
108 use super::*;
109 use crate::color;
110 use crate::tests::integration_test_utils;
111
112 // TODO: Test influence of BAT_THEME env var. E.g. see utils::process::tests::FakeParentArgs.
113 #[test]
114 fn test_syntax_theme_selection() {
115 for (
116 syntax_theme,
117 mode, // (--light, --dark)
118 expected_syntax_theme,
119 expected_mode,
120 ) in vec![
121 (None, None, DEFAULT_DARK_SYNTAX_THEME, Dark),
122 (Some("GitHub"), None, "GitHub", Light),
123 (Some("Nord"), None, "Nord", Dark),
124 (None, Some(Dark), DEFAULT_DARK_SYNTAX_THEME, Dark),
125 (None, Some(Light), DEFAULT_LIGHT_SYNTAX_THEME, Light),
126 (Some("GitHub"), Some(Light), "GitHub", Light),
127 (Some("GitHub"), Some(Dark), "GitHub", Dark),
128 (Some("Nord"), Some(Light), "Nord", Light),
129 (Some("Nord"), Some(Dark), "Nord", Dark),
130 (Some("none"), None, "none", Dark),
131 (Some("none"), Some(Dark), "none", Dark),
132 (Some("None"), Some(Light), "none", Light),
133 ] {
134 let mut args = vec![];
135 if let Some(syntax_theme) = syntax_theme {
136 args.push("--syntax-theme");
137 args.push(syntax_theme);
138 }
139 let is_true_color = true;
140 if is_true_color {
141 args.push("--true-color");
142 args.push("always");
143 } else {
144 args.push("--true-color");
145 args.push("never");
146 }
147 match mode {
148 Some(Light) => {
149 args.push("--light");
150 }
151 Some(Dark) => {
152 args.push("--dark");
153 }
154 None => {}
155 }
156 let config = integration_test_utils::make_config_from_args(&args);
157 assert_eq!(
158 &config
159 .syntax_theme
160 .clone()
161 .map(|t| t.name.unwrap())
162 .unwrap_or("none".to_string()),
163 expected_syntax_theme
164 );
165 if is_no_syntax_highlighting_syntax_theme_name(expected_syntax_theme) {
166 assert!(config.syntax_theme.is_none())
167 } else {
168 assert_eq!(
169 config.syntax_theme.unwrap().name.as_ref().unwrap(),
170 expected_syntax_theme
171 );
172 }
173 assert_eq!(
174 config.minus_style.ansi_term_style.background.unwrap(),
175 color::get_minus_background_color_default(expected_mode, is_true_color)
176 );
177 assert_eq!(
178 config.minus_emph_style.ansi_term_style.background.unwrap(),
179 color::get_minus_emph_background_color_default(expected_mode, is_true_color)
180 );
181 assert_eq!(
182 config.plus_style.ansi_term_style.background.unwrap(),
183 color::get_plus_background_color_default(expected_mode, is_true_color)
184 );
185 assert_eq!(
186 config.plus_emph_style.ansi_term_style.background.unwrap(),
187 color::get_plus_emph_background_color_default(expected_mode, is_true_color)
188 );
189 }
190 }
191 }