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
WAS: Fix right panel background extending beyond the truncation symbol

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.

However due to the padding after the right panel the color will
extend beyond a possible truncation symbol:

"TEXT#AB→#", not "TEXT#AB→ " with "#" being the green background.

If any line is truncated, the entire block is now padded with spaces
(if only selected lines used spaces the line would show a jagged
right edge)

Add enums `BgFillWidth` and `BgFillMethod` to better distinguish
if the background should be filled, and if so how (ANSI or Spaces).
  • Loading branch information
th1000s committed Sep 16, 2021
1 parent 0620789 commit f520ac7
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 165 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 fill in background color until 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-right-method")]
pub line_fill_right_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
15 changes: 15 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_right_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,18 @@ impl From<cli::Opt> for Config {
let file_renamed_label = opt.file_renamed_label;
let hunk_label = opt.hunk_label;

let line_fill_right_method = match opt.line_fill_right_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-right-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 +259,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_right_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 @@ -384,7 +384,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 @@ -408,7 +408,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 @@ -490,7 +490,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 @@ -515,7 +515,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 @@ -526,7 +526,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
Loading

0 comments on commit f520ac7

Please sign in to comment.