diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-08-20 20:30:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-20 20:30:14 -0400 |
commit | ecb2da1e271aa0daa6dd2ed4c6658d59347020e6 (patch) | |
tree | c8168a1c02a91d65594949bbbe180f27996fe2d5 | |
parent | 98d1da411e185130fe4c167786107cb90725bdef (diff) |
Interpret `line-numbers = false` in gitconfig as no line numbers at all (#296)
Fixes #292
-rw-r--r-- | src/cli.rs | 14 | ||||
-rw-r--r-- | src/config.rs | 10 | ||||
-rw-r--r-- | src/delta.rs | 2 | ||||
-rw-r--r-- | src/options/option_value.rs | 7 | ||||
-rw-r--r-- | src/options/set.rs | 29 |
5 files changed, 59 insertions, 3 deletions
@@ -566,6 +566,7 @@ pub struct ComputedValues { pub decorations_width: Width, pub inspect_raw_lines: InspectRawLines, pub is_light_mode: bool, + pub line_numbers_mode: LineNumbersMode, pub paging_mode: PagingMode, pub syntax_dummy_theme: SyntaxTheme, pub syntax_set: SyntaxSet, @@ -597,6 +598,19 @@ impl Default for InspectRawLines { } } +#[derive(Clone, Debug, PartialEq)] +pub enum LineNumbersMode { + None, + First, + Full, +} + +impl Default for LineNumbersMode { + fn default() -> Self { + LineNumbersMode::First + } +} + impl Default for PagingMode { fn default() -> Self { PagingMode::Never diff --git a/src/config.rs b/src/config.rs index 91887d3b..b383784a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -40,6 +40,7 @@ pub struct Config { pub line_numbers_plus_style: Style, pub line_numbers_right_format: String, pub line_numbers_right_style: Style, + pub line_numbers_show_first_line_number: bool, pub line_numbers_zero_style: Style, pub max_buffered_lines: usize, pub max_line_distance: f64, @@ -160,13 +161,20 @@ impl From<cli::Opt> for Config { hyperlinks_file_link_format: opt.hyperlinks_file_link_format, inspect_raw_lines: opt.computed.inspect_raw_lines, keep_plus_minus_markers: opt.keep_plus_minus_markers, - line_numbers: opt.line_numbers, + line_numbers: match opt.computed.line_numbers_mode { + cli::LineNumbersMode::Full => true, + _ => false, + }, line_numbers_left_format: opt.line_numbers_left_format, line_numbers_left_style, line_numbers_minus_style, line_numbers_plus_style, line_numbers_right_format: opt.line_numbers_right_format, line_numbers_right_style, + line_numbers_show_first_line_number: match opt.computed.line_numbers_mode { + cli::LineNumbersMode::First => true, + _ => false, + }, line_numbers_zero_style, max_buffered_lines: 32, max_line_distance: opt.max_line_distance, diff --git a/src/delta.rs b/src/delta.rs index d256e77a..9c411d8e 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -476,7 +476,7 @@ fn handle_hunk_header_line( painter .line_numbers_data .initialize_hunk(line_numbers, plus_file.to_string()); - } else if !config.hunk_header_style.is_raw { + } else if config.line_numbers_show_first_line_number && !config.hunk_header_style.is_raw { let plus_line_number = line_numbers[line_numbers.len() - 1].0; let formatted_plus_line_number = if config.hyperlinks { features::hyperlinks::format_osc8_file_hyperlink( 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<OptionValue> for Option<String> { 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<String>."), } } 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<String, features::BuiltinFeature>, + git_config: &mut Option<git_config::GitConfig>, + 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<Option<String>> = 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, |