Skip to content

Commit 9fa449e

Browse files
ulwludandavison
andauthored
Fix computed values to be computed after all set_options (#690)
* Make truecolor option honorable from gitconfig * Add test that true-color can be read from git config The new test fails before 7ed3c2c * Place computing process after set_options * Add test for checking if values requre computing set correctly * Remove unnecessary computed values candidate Co-authored-by: Dan Davison <dandavison7@gmail.com>
1 parent ea3417a commit 9fa449e

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

src/cli.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ pub struct ComputedValues {
637637
pub inspect_raw_lines: InspectRawLines,
638638
pub is_light_mode: bool,
639639
pub paging_mode: PagingMode,
640-
pub syntax_dummy_theme: SyntaxTheme,
641640
pub syntax_set: SyntaxSet,
642641
pub syntax_theme: Option<SyntaxTheme>,
643642
pub true_color: bool,

src/config.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,37 @@ pub fn delta_unreachable(message: &str) -> ! {
496496
);
497497
process::exit(error_exit_code);
498498
}
499+
500+
#[cfg(test)]
501+
pub mod tests {
502+
use crate::bat_utils::output::PagingMode;
503+
use crate::cli;
504+
use crate::tests::integration_test_utils;
505+
use std::fs::remove_file;
506+
507+
#[test]
508+
fn test_get_computed_values_from_config() {
509+
let git_config_contents = b"
510+
[delta]
511+
true-color = never
512+
width = 100
513+
inspect-raw-lines = true
514+
paging = never
515+
syntax-theme = None
516+
";
517+
let git_config_path = "delta__test_get_true_color_from_config.gitconfig";
518+
let config = integration_test_utils::make_config_from_args_and_git_config(
519+
&[],
520+
Some(git_config_contents),
521+
Some(git_config_path),
522+
);
523+
assert_eq!(config.true_color, false);
524+
assert_eq!(config.decorations_width, cli::Width::Fixed(100));
525+
assert_eq!(config.background_color_extends_to_terminal_width, true);
526+
assert_eq!(config.inspect_raw_lines, cli::InspectRawLines::True);
527+
assert_eq!(config.paging_mode, PagingMode::Never);
528+
assert!(config.syntax_theme.is_none());
529+
// syntax_set doesn't depend on gitconfig.
530+
remove_file(git_config_path).unwrap();
531+
}
532+
}

src/options/set.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@ pub fn set_options(
9090
let features = gather_features(opt, &builtin_features, git_config);
9191
opt.features = features.join(" ");
9292

93-
set_widths(opt, git_config, arg_matches, &option_names);
94-
9593
// Set light, dark, and syntax-theme.
96-
set_true_color(opt);
9794
set__light__dark__syntax_theme__options(opt, git_config, arg_matches, &option_names);
98-
theme::set__is_light_mode__syntax_theme__syntax_set(opt, assets);
9995

10096
// HACK: make minus-line styles have syntax-highlighting iff side-by-side.
10197
if features.contains(&"side-by-side".to_string()) {
@@ -192,6 +188,10 @@ pub fn set_options(
192188
true
193189
);
194190

191+
// Setting ComputedValues
192+
set_widths(opt);
193+
set_true_color(opt);
194+
theme::set__is_light_mode__syntax_theme__syntax_set(opt, assets);
195195
opt.computed.inspect_raw_lines =
196196
cli::InspectRawLines::from_str(&opt.inspect_raw_lines).unwrap();
197197
opt.computed.paging_mode = parse_paging_mode(&opt.paging_mode);
@@ -509,28 +509,10 @@ fn parse_paging_mode(paging_mode_string: &str) -> PagingMode {
509509
}
510510
}
511511

512-
fn set_widths(
513-
opt: &mut cli::Opt,
514-
git_config: &mut Option<GitConfig>,
515-
arg_matches: &clap::ArgMatches,
516-
option_names: &HashMap<&str, &str>,
517-
) {
512+
fn set_widths(opt: &mut cli::Opt) {
518513
// Allow one character in case e.g. `less --status-column` is in effect. See #41 and #10.
519514
opt.computed.available_terminal_width = (Term::stdout().size().1 - 1) as usize;
520515

521-
let empty_builtin_features = HashMap::new();
522-
if opt.width.is_none() {
523-
set_options!(
524-
[width],
525-
opt,
526-
&empty_builtin_features,
527-
git_config,
528-
arg_matches,
529-
option_names,
530-
false
531-
);
532-
}
533-
534516
let (decorations_width, background_color_extends_to_terminal_width) = match opt.width.as_deref()
535517
{
536518
Some("variable") => (cli::Width::Variable, false),
@@ -559,6 +541,7 @@ fn set_true_color(opt: &mut cli::Opt) {
559541
opt.true_color = _24_bit_color.clone();
560542
}
561543
}
544+
562545
opt.computed.true_color = match opt.true_color.as_ref() {
563546
"always" => true,
564547
"never" => false,
@@ -721,6 +704,7 @@ pub mod tests {
721704
assert_eq!(opt.side_by_side, true);
722705
assert_eq!(opt.syntax_theme, Some("xxxyyyzzz".to_string()));
723706
assert_eq!(opt.tab_width, 77);
707+
assert_eq!(opt.true_color, "never");
724708
assert_eq!(opt.whitespace_error_style, "black black");
725709
assert_eq!(opt.width, Some("77".to_string()));
726710
assert_eq!(opt.tokenization_regex, "xxxyyyzzz");

0 commit comments

Comments
 (0)