Skip to content

Commit

Permalink
Command line option to apply line number style to unchanged lines
Browse files Browse the repository at this point in the history
  • Loading branch information
clnoll committed Jun 9, 2020
1 parent 39412bc commit 118f472
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ pub struct Opt {
#[structopt(long = "number-plus-style", default_value = "auto")]
pub number_plus_style: String,

/// Style (foreground, background, attributes) to apply on unchanged lines (if --number is set),
/// overriding --number-minus-style and --number-plus-style. See STYLES section.
#[structopt(long = "number-zero-style")]
pub number_zero_style: Option<String>,

/// Format string for the left (minus) column of line numbers (--number), if --number is set.
/// Should include the placeholder %ln to indicate the position of the line number.
/// See the LINE NUMBERS section.
Expand Down
18 changes: 17 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct Config<'a> {
pub number_plus_format: String,
pub number_plus_format_style: Style,
pub number_plus_style: Style,
pub number_zero_style: Option<Style>,
pub paging_mode: PagingMode,
pub plus_emph_style: Style,
pub plus_file: Option<PathBuf>,
Expand Down Expand Up @@ -132,6 +133,7 @@ pub fn get_config<'a>(
number_minus_style,
number_plus_format_style,
number_plus_style,
number_zero_style,
) = make_line_number_styles(
&opt,
hunk_header_style.decoration_ansi_term_style(),
Expand Down Expand Up @@ -188,6 +190,7 @@ pub fn get_config<'a>(
number_plus_format: opt.number_plus_format,
number_plus_format_style: number_plus_format_style,
number_plus_style: number_plus_style,
number_zero_style: number_zero_style,
paging_mode,
plus_emph_style,
plus_file: opt.plus_file.map(|s| s.clone()),
Expand Down Expand Up @@ -293,7 +296,7 @@ fn make_line_number_styles<'a>(
opt: &'a cli::Opt,
default_style: Option<ansi_term::Style>,
true_color: bool,
) -> (Style, Style, Style, Style) {
) -> (Style, Style, Style, Style, Option<Style>) {
let (default_foreground, default_background) = match default_style {
Some(default_style) => (default_style.foreground, default_style.background),
None => (None, None),
Expand Down Expand Up @@ -335,11 +338,24 @@ fn make_line_number_styles<'a>(
false,
);

let number_zero_style = match &opt.number_zero_style {
Some(x) => Some(Style::from_str(
x,
default_foreground,
default_background,
None,
true_color,
false,
)),
None => None,
};

(
number_minus_format_style,
number_minus_style,
number_plus_format_style,
number_plus_style,
number_zero_style,
)
}

Expand Down
33 changes: 28 additions & 5 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,26 @@ impl<'a> Painter<'a> {
get_line_number_components(minus, &config.number_minus_format);
let (plus_before, plus_number, plus_after) =
get_line_number_components(plus, &config.number_plus_format);

let number_minus_style = get_zero_or_default_style(
minus,
plus,
config.number_zero_style,
config.number_minus_style,
);
let number_plus_style = get_zero_or_default_style(
minus,
plus,
config.number_zero_style,
config.number_plus_style,
);

vec![
config
.number_minus_format_style
.ansi_term_style
.paint(minus_before),
config
.number_minus_style
.ansi_term_style
.paint(minus_number),
number_minus_style.ansi_term_style.paint(minus_number),
config
.number_minus_format_style
.ansi_term_style
Expand All @@ -176,7 +187,7 @@ impl<'a> Painter<'a> {
.number_plus_format_style
.ansi_term_style
.paint(plus_before),
config.number_plus_style.ansi_term_style.paint(plus_number),
number_plus_style.ansi_term_style.paint(plus_number),
config
.number_plus_format_style
.ansi_term_style
Expand Down Expand Up @@ -567,6 +578,18 @@ fn format_line_number(line_number: Option<usize>) -> String {
}
}

fn get_zero_or_default_style(
minus: Option<usize>,
plus: Option<usize>,
zero_style: Option<Style>,
default_style: Style,
) -> Style {
match (zero_style, minus, plus) {
(Some(z), Some(_), Some(_)) => z,
_ => default_style,
}
}

fn get_line_number_components(
number: Option<usize>,
number_format: &str,
Expand Down
6 changes: 5 additions & 1 deletion src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ fn _rewrite_options_to_honor_git_config(
arg_matches
);
set_delta_options__option_string!(
[("syntax_theme", syntax_theme), ("width", width)],
[
("number-zero-style", number_zero_style),
("syntax_theme", syntax_theme),
("width", width)
],
opt,
git_config,
arg_matches
Expand Down

0 comments on commit 118f472

Please # to comment.