summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 3f1356ac6d7ecb0021b90ce42e059dd6ab6f195c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::process;
use std::str::FromStr;

use ansi_term::Color;
use syntect::highlighting::Color as SyntectColor;
use syntect::highlighting::Style as SyntectStyle;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;

use crate::bat::output::PagingMode;
use crate::bat::terminal::to_ansi_color;
use crate::cli;
use crate::env;
use crate::style::{self, Style};
use crate::syntect_color;

pub struct Config<'a> {
    pub theme: Option<Theme>,
    pub theme_name: String,
    pub dummy_theme: Theme,
    pub max_line_distance: f64,
    pub max_line_distance_for_naively_paired_lines: f64,
    pub minus_style: Style,
    pub minus_emph_style: Style,
    pub zero_style: Style,
    pub plus_style: Style,
    pub plus_emph_style: Style,
    pub minus_line_marker: &'a str,
    pub plus_line_marker: &'a str,
    pub commit_style: cli::SectionStyle,
    pub commit_color: Color,
    pub file_style: cli::SectionStyle,
    pub file_color: Color,
    pub hunk_style: cli::SectionStyle,
    pub hunk_color: Color,
    pub syntax_set: SyntaxSet,
    pub terminal_width: usize,
    pub true_color: bool,
    pub background_color_extends_to_terminal_width: bool,
    pub tab_width: usize,
    pub null_style: Style,
    pub null_syntect_style: SyntectStyle,
    pub max_buffered_lines: usize,
    pub paging_mode: PagingMode,
}

pub fn get_config<'a>(
    opt: cli::Opt,
    syntax_set: SyntaxSet,
    theme_set: ThemeSet,
    true_color: bool,
    terminal_width: usize,
    paging_mode: PagingMode,
) -> Config<'a> {
    let background_color_extends_to_terminal_width = opt.width != Some("variable".to_string());
    let theme_name_from_bat_pager = env::get_env_var("BAT_THEME");
    let (is_light_mode, theme_name) = get_is_light_mode_and_theme_name(
        opt.theme.as_ref(),
        theme_name_from_bat_pager.as_ref(),
        opt.light,
        &theme_set,
    );

    let (minus_style, minus_emph_style, zero_style, plus_style, plus_emph_style) =
        make_styles(&opt, is_light_mode, true_color);

    let theme = if style::is_no_syntax_highlighting_theme_name(&theme_name) {
        None
    } else {
        Some(theme_set.themes[&theme_name].clone())
    };
    let dummy_theme = theme_set.themes.values().next().unwrap().clone();

    let minus_line_marker = if opt.keep_plus_minus_markers {
        "-"
    } else {
        " "
    };
    let plus_line_marker = if opt.keep_plus_minus_markers {
        "+"
    } else {
        " "
    };

    let max_line_distance_for_naively_paired_lines =
        env::get_env_var("DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES")
            .map(|s| s.parse::<f64>().unwrap_or(0.0))
            .unwrap_or(0.0);

    Config {
        theme,
        theme_name,
        dummy_theme,
        max_line_distance: opt.max_line_distance,
        max_line_distance_for_naively_paired_lines,
        minus_style,
        minus_emph_style,
        zero_style,
        plus_style,
        plus_emph_style,
        minus_line_marker,
        plus_line_marker,
        commit_style: opt.commit_style,
        commit_color: color_from_rgb_or_ansi_code(&opt.commit_color, true_color),
        file_style: opt.file_style,
        file_color: color_from_rgb_or_ansi_code(&opt.file_color, true_color),
        hunk_style: opt.hunk_style,
        hunk_color: color_from_rgb_or_ansi_code(&opt.hunk_color, true_color),
        true_color,
        terminal_width,
        background_color_extends_to_terminal_width,
        tab_width: opt.tab_width,
        syntax_set,
        null_style: Style::new(),
        null_syntect_style: SyntectStyle::default(),
        max_buffered_lines: 32,
        paging_mode,
    }
}

/// Return a (theme_name, is_light_mode) tuple.
/// theme_name == None in return value means syntax highlighting is disabled.
///
/// There are two types of color choices that have to be made:
/// 1. The choice of "theme". This is the language syntax highlighting theme; you have to make this choice when using `bat` also.
/// 2. The choice of "light vs dark mode". This determines whether the background colors should be chosen for a light or dark terminal background. (`bat` has no equivalent.)
///
/// Basically:
/// 1. The theme is specified by the `--theme` option. If this isn't supplied then it is specified by the `BAT_PAGER` environment variable.
/// 2. Light vs dark mode is specified by the `--light` or `--dark` options. If these aren't supplied then it is inferred from the chosen theme.
///
/// In the absence of other factors, the default assumes a dark terminal background.
///
/// Specifically, the rules are as follows:
///
/// | --theme    | $BAT_THEME | --light/--dark | Behavior                                                                   |
/// |------------|------------|----------------|----------------------------------------------------------------------------|
/// | -          | -          | -              | default dark theme, dark mode                                              |
/// | some_theme | (IGNORED)  | -              | some_theme with light/dark mode inferred accordingly                       |
/// | -          | BAT_THEME  | -              | BAT_THEME, with light/dark mode inferred accordingly                       |
/// | -          | -          | yes            | default light/dark theme, light/dark mode                                  |
/// | some_theme | (IGNORED)  | yes            | some_theme, light/dark mode (even if some_theme conflicts with light/dark) |
/// | -          | BAT_THEME  | yes            | BAT_THEME, light/dark mode (even if BAT_THEME conflicts with light/dark)   |
fn get_is_light_mode_and_theme_name(
    theme_arg: Option<&String>,
    bat_theme_env_var: Option<&String>,
    light_mode_arg: bool,
    theme_set: &ThemeSet,
) -> (bool, String) {
    let theme_arg = valid_theme_name_or_none(theme_arg, theme_set);
    let bat_theme_env_var = valid_theme_name_or_none(bat_theme_env_var, theme_set);
    match (theme_arg, bat_theme_env_var, light_mode_arg) {
        (None, None, false) => (false, style::DEFAULT_DARK_THEME.to_string()),
        (Some(theme_name), _, false) => (style::is_light_theme(&theme_name), theme_name),
        (None, Some(theme_name), false) => (style::is_light_theme(&theme_name), theme_name),
        (None, None, true) => (true, style::DEFAULT_LIGHT_THEME.to_string()),
        (Some(theme_name), _, is_light_mode) => (is_light_mode, theme_name),
        (None, Some(theme_name), is_light_mode) => (is_light_mode, theme_name),
    }
}

// At this stage the theme name is considered valid if it is either a real theme name or the special
// no-syntax-highlighting name.
fn valid_theme_name_or_none(theme_name: Option<&String>, theme_set: &ThemeSet) -> Option<String> {
    match theme_name {
        Some(name)
            if style::is_no_syntax_highlighting_theme_name(name)
                || theme_set.themes.contains_key(name) =>
        {
            Some(name.to_string())
        }
        _ => None,
    }
}

fn make_styles<'a>(
    opt: &'a cli::Opt,
    is_light_mode: bool,
    true_color: bool,
) -> (Style, Style, Style, Style, Style) {
    let minus_style = parse_style_string(
        &opt.minus_style,
        None,
        Some(style::get_minus_background_color_default(
            is_light_mode,
            true_color,
        )),
        true_color,
    );

    let minus_emph_style = parse_style_string(
        &opt.minus_emph_style,
        None,
        Some(style::get_minus_emph_background_color_default(
            is_light_mode,
            true_color,
        )),
        true_color,
    );

    let zero_style = parse_style_string(&opt.zero_style, None, None, true_color);

    let plus_style = parse_style_string(
        &opt.plus_style,
        None,
        Some(style::get_plus_background_color_default(
            is_light_mode,
            true_color,
        )),
        true_color,
    );

    let plus_emph_style = parse_style_string(
        &opt.plus_emph_style,
        None,
        Some(style::get_plus_emph_background_color_default(
            is_light_mode,
            true_color,
        )),
        true_color,
    );

    (
        minus_style,
        minus_emph_style,
        zero_style,
        plus_style,
        plus_emph_style,
    )
}

/// Construct ansi_term Style from style string supplied on command line,
/// together with defaults.
pub fn parse_style_string(
    style_string: &str,
    foreground_default: Option<Color>,
    background_default: Option<Color>,
    true_color: bool,
) -> Style {
    let mut style = Style