Skip to content

Commit

Permalink
Option to set the background extension mode to ANSI or spaces
Browse files Browse the repository at this point in the history
In side-by-side mode, if `background_color_extends_to_terminal_width`
is set, the left panel color is extended via spaces, but the right
one via an ANSI sequence which instructs the terminal emulator to
fill the background color rightwards.

The command line option --line-fill-method ansi|spaces can change
how the right panel background is filled.

Add enums `BgShouldFill` and `BgFillMethod` to better distinguish
if the background should be filled, and if so, how.
  • Loading branch information
th1000s authored and dandavison committed Sep 19, 2021
1 parent db04d5e commit b9952f9
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 164 deletions.
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,11 @@ pub struct Opt {
/// delta will be slow on very long lines (e.g. minified .js) if truncation is disabled.
pub max_line_length: usize,

/// How to extend the background color to the end of the line in side-by-side mode. Can
/// be ansi (default) or spaces. Has no effect if --width=variable is given.
#[structopt(long = "line-fill-method")]
pub line_fill_method: Option<String>,

/// The width of underline/overline decorations. Use --width=variable to extend decorations and
/// background colors to the end of the text only. Otherwise background colors extend to the
/// full terminal width.
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::env;
use crate::features::navigate;
use crate::features::side_by_side;
use crate::git_config::{GitConfig, GitConfigEntry};
use crate::paint::BgFillMethod;
use crate::style::{self, Style};

pub struct Config {
Expand Down Expand Up @@ -51,6 +52,7 @@ pub struct Config {
pub hyperlinks_file_link_format: String,
pub inspect_raw_lines: cli::InspectRawLines,
pub keep_plus_minus_markers: bool,
pub line_fill_method: BgFillMethod,
pub line_numbers: bool,
pub line_numbers_left_format: String,
pub line_numbers_left_style: Style,
Expand Down Expand Up @@ -187,6 +189,16 @@ impl From<cli::Opt> for Config {
let file_renamed_label = opt.file_renamed_label;
let hunk_label = opt.hunk_label;

let line_fill_method = match opt.line_fill_method.as_deref() {
// Note that "default" is not documented
Some("ansi") | Some("default") | None => BgFillMethod::TryAnsiSequence,
Some("spaces") => BgFillMethod::Spaces,
_ => {
eprintln!("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\".");
process::exit(1);
}
};

let navigate_regexp = if opt.navigate || opt.show_themes {
Some(navigate::make_navigate_regexp(
opt.show_themes,
Expand Down Expand Up @@ -245,6 +257,7 @@ 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_fill_method,
line_numbers: opt.line_numbers,
line_numbers_left_format: opt.line_numbers_left_format,
line_numbers_left_style,
Expand Down
10 changes: 5 additions & 5 deletions src/features/line_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ pub mod tests {
let mut lines = output.lines().skip(7);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!(strip_ansi_codes(line_1), " 1 ⋮ │a = 1");
assert_eq!(strip_ansi_codes(line_2), " 2 ⋮ │b = 2");
assert_eq!(strip_ansi_codes(line_2), " 2 ⋮ │b = 23456");
}

#[test]
Expand All @@ -407,7 +407,7 @@ pub mod tests {
let mut lines = output.lines().skip(7);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!(strip_ansi_codes(line_1), " ⋮ 1 │a = 1");
assert_eq!(strip_ansi_codes(line_2), " ⋮ 2 │b = 2");
assert_eq!(strip_ansi_codes(line_2), " ⋮ 2 │b = 234567");
}

#[test]
Expand Down Expand Up @@ -489,7 +489,7 @@ pub mod tests {
let mut lines = output.lines().skip(5);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!(strip_ansi_codes(line_1), " 1 ⋮ │-a = 1");
assert_eq!(strip_ansi_codes(line_2), " 2 ⋮ │-b = 2");
assert_eq!(strip_ansi_codes(line_2), " 2 ⋮ │-b = 23456");
}

#[test]
Expand All @@ -514,7 +514,7 @@ index 223ca50..e69de29 100644
+++ w/a.py
@@ -1,2 +0,0 @@
-a = 1
-b = 2
-b = 23456
";

pub const TWO_PLUS_LINES_DIFF: &str = "\
Expand All @@ -525,7 +525,7 @@ index 0000000..223ca50
+++ i/a.py
@@ -0,0 +1,2 @@
+a = 1
+b = 2
+b = 234567
";

pub const ONE_MINUS_ONE_PLUS_LINE_DIFF: &str = "\
Expand Down

0 comments on commit b9952f9

Please sign in to comment.