From ecb2da1e271aa0daa6dd2ed4c6658d59347020e6 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 20 Aug 2020 20:30:14 -0400 Subject: Interpret `line-numbers = false` in gitconfig as no line numbers at all (#296) Fixes #292 --- src/options/option_value.rs | 7 +++++++ src/options/set.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'src/options') diff --git a/src/options/option_value.rs b/src/options/option_value.rs index cbe5b383..d569f676 100644 --- a/src/options/option_value.rs +++ b/src/options/option_value.rs @@ -55,6 +55,13 @@ impl From for Option { fn from(value: OptionValue) -> Self { match value { OptionValue::OptionString(value) => value, + // HACK: See the comment in options::set::compute_line_numbers_mode(). That function + // deliberately reads what is normally a boolean value ('line-numbers') as a string. + // However options::get::get_option_value() can fall through to obtaining the value + // from builtin_features, in which case an OptionValue::Boolean will be encountered. + // See the comment in options::set::compute_line_numbers_mode() and docstring of + // options::get::get_option_value(). + OptionValue::Boolean(_) => None, _ => delta_unreachable("Error converting OptionValue to Option."), } } diff --git a/src/options/set.rs b/src/options/set.rs index 150246c9..b8ea10a7 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -16,7 +16,7 @@ use crate::features; use crate::git_config; use crate::git_config_entry::{self, GitConfigEntry}; use crate::options::option_value::{OptionValue, ProvenancedOptionValue}; -use crate::options::theme; +use crate::options::{self, theme}; macro_rules! set_options { ([$( $field_ident:ident ),* ], @@ -183,6 +183,8 @@ pub fn set_options( opt.computed.inspect_raw_lines = cli::InspectRawLines::from_str(&opt.inspect_raw_lines).unwrap(); + opt.computed.line_numbers_mode = + compute_line_numbers_mode(opt, &builtin_features, git_config, &option_names); opt.computed.paging_mode = parse_paging_mode(&opt.paging_mode); // --color-only is used for interactive.diffFilter (git add -p) and side-by-side cannot be used @@ -192,6 +194,31 @@ pub fn set_options( } } +fn compute_line_numbers_mode( + opt: &cli::Opt, + builtin_features: &HashMap, + git_config: &mut Option, + option_names: &HashMap<&str, &str>, +) -> cli::LineNumbersMode { + // line-numbers is in general treated as a boolean value. We read it as a string here in order + // to interpret an explicit "false" (as opposed to merely absence) as meaning "Do not show any + // line numbers; not even the first line number of the hunk". + let line_numbers_string_value: Option> = options::get::get_option_value( + option_names["line-numbers"], + builtin_features, + opt, + git_config, + ); + match ( + line_numbers_string_value.as_ref().map(|val| val.as_deref()), + opt.line_numbers, + ) { + (Some(Some("false")), _) => cli::LineNumbersMode::None, + (_, true) => cli::LineNumbersMode::Full, + (_, false) => cli::LineNumbersMode::First, + } +} + #[allow(non_snake_case)] fn set__light__dark__syntax_theme__options( opt: &mut cli::Opt, -- cgit v1.2.3