summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-04-11 15:38:33 -0400
committerDan Davison <dandavison7@gmail.com>2020-04-12 00:47:34 -0400
commitdefc0eadff002ba94269637f38f0e342ec823aff (patch)
treee55719e0cf60b251c9ab53cc214cb30cf176005c
parent7a23a5c536616f860e73226f220ec3a24b8aa6a3 (diff)
Add --keep-plus-minus-markers option
-rw-r--r--Makefile5
-rw-r--r--completion/bash/completion.sh1
-rw-r--r--src/cli.rs5
-rw-r--r--src/config.rs15
-rw-r--r--src/delta.rs11
-rw-r--r--src/paint.rs9
6 files changed, 40 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 3bab6b55..9dd9e4f7 100644
--- a/Makefile
+++ b/Makefile
@@ -6,13 +6,14 @@ lint:
test:
cargo test
- bash -c "diff -u <(git log -p | cut -c 2-) \
+ bash -c "diff -u <(git log -p) \
<(git log -p | delta --width variable \
--tabs 0 \
+ --keep-plus-minus-markers \
--commit-style plain \
--file-style plain \
--hunk-style plain \
- | ansifilter | cut -c 2-)"
+ | ansifilter)"
release:
@make -f release.Makefile release
diff --git a/completion/bash/completion.sh b/completion/bash/completion.sh
index 3aa2d93f..b1a6a001 100644
--- a/completion/bash/completion.sh
+++ b/completion/bash/completion.sh
@@ -31,6 +31,7 @@ _delta() {
--minus-emph-color
--plus-color
--plus-emph-color
+ --keep-plus-minus-markers
--show-background-colors
--theme
--version
diff --git a/src/cli.rs b/src/cli.rs
index 3398e401..f60d5c50 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -97,6 +97,11 @@ pub struct Opt {
/// apply syntax highlighting to unchanged and new lines only.
pub highlight_removed: bool,
+ #[structopt(long = "keep-plus-minus-markers")]
+ /// Prefix added/removed lines with a +/- character, respectively, exactly as git does. The
+ /// default behavior is to output a space character in place of these markers.
+ pub keep_plus_minus_markers: bool,
+
#[structopt(long = "commit-style", default_value = "plain")]
/// Formatting style for the commit section of git output. Options
/// are: plain, box.
diff --git a/src/config.rs b/src/config.rs
index ce8b7f43..11b2299c 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -18,6 +18,8 @@ pub struct Config<'a> {
pub minus_emph_style_modifier: StyleModifier,
pub plus_style_modifier: StyleModifier,
pub plus_emph_style_modifier: StyleModifier,
+ pub minus_line_marker: &'a str,
+ pub plus_line_marker: &'a str,
pub highlight_removed: bool,
pub commit_style: cli::SectionStyle,
pub commit_color: Color,
@@ -102,6 +104,17 @@ pub fn get_config<'a>(
font_style: None,
};
+ let minus_line_marker = if opt.keep_plus_minus_markers {
+ "-"
+ } else {
+ " "
+ };
+ let plus_line_marker = if opt.keep_plus_minus_markers {
+ "+"
+ } else {
+ " "
+ };
+
Config {
theme,
theme_name,
@@ -111,6 +124,8 @@ pub fn get_config<'a>(
plus_style_modifier,
plus_emph_style_modifier,
highlight_removed: opt.highlight_removed,
+ minus_line_marker,
+ plus_line_marker,
commit_style: opt.commit_style,
commit_color: color_from_rgb_or_ansi_code(&opt.commit_color),
file_style: opt.file_style,
diff --git a/src/delta.rs b/src/delta.rs
index 897b2f10..49dbb2d1 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -271,6 +271,7 @@ fn handle_hunk_meta_line(
)]],
&mut painter.output_buffer,
config,
+ "",
style::NO_BACKGROUND_COLOR_STYLE_MODIFIER,
false,
);
@@ -321,6 +322,7 @@ fn handle_hunk_line(painter: &mut Painter, line: &str, state: State, config: &Co
State::HunkPlus
}
_ => {
+ let is_empty = line.is_empty();
painter.paint_buffered_lines();
let line = prepare(&line, true, config);
let syntax_style_sections = Painter::get_line_syntax_style_sections(
@@ -334,6 +336,7 @@ fn handle_hunk_line(painter: &mut Painter, line: &str, state: State, config: &Co
vec![vec![(style::NO_BACKGROUND_COLOR_STYLE_MODIFIER, &line)]],
&mut painter.output_buffer,
config,
+ if is_empty { "" } else { " " },
style::NO_BACKGROUND_COLOR_STYLE_MODIFIER,
true,
);
@@ -352,8 +355,9 @@ fn prepare(line: &str, append_newline: bool, config: &Config) -> String {
if !line.is_empty() {
let mut line = line.graphemes(true);
- // The first column contains a -/+/space character, added by git. We skip it here and insert
- // a replacement space when formatting the line below.
+ // The first column contains a -/+/space character, added by git. We drop it now, so that
+ // it is not present during syntax highlighting, and inject a replacement when emitting the
+ // line.
line.next();
// Expand tabs as spaces.
@@ -365,7 +369,7 @@ fn prepare(line: &str, append_newline: bool, config: &Config) -> String {
} else {
line.collect::<String>()
};
- format!(" {}{}", output_line, terminator)
+ format!("{}{}", output_line, terminator)
} else {
terminator.to_string()
}
@@ -604,6 +608,7 @@ mod tests {
minus_emph_color: None,
plus_color: None,
plus_emph_color: None,
+ keep_plus_minus_markers: false,
theme: None,
highlight_removed: false,
commit_style: cli::SectionStyle::Plain,
diff --git a/src/paint.rs b/src/paint.rs
index d3c6806c..a65ff00d 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -77,6 +77,7 @@ impl<'a> Painter<'a> {
minus_line_diff_style_sections,
&mut self.output_buffer,
self.config,
+ self.config.minus_line_marker,
self.config.minus_style_modifier,
true,
);
@@ -87,6 +88,7 @@ impl<'a> Painter<'a> {
plus_line_diff_style_sections,
&mut self.output_buffer,
self.config,
+ self.config.plus_line_marker,
self.config.plus_style_modifier,
true,
);
@@ -102,14 +104,20 @@ impl<'a> Painter<'a> {
diff_style_sections: Vec<Vec<(StyleModifier, &str)>>,
output_buffer: &mut String,
config: &config::Config,
+ prefix: &str,
background_style_modifier: StyleModifier,
should_trim_newline_and_right_pad: bool,
) {
+ let background_style = config.no_style.apply(background_style_modifier);
+ let background_ansi_style = to_ansi_style(background_style, config.true_color);
for (syntax_sections, diff_sections) in
syntax_style_sections.iter().zip(diff_style_sections.iter())
{
let mut text_width = 0;
let mut ansi_strings = Vec::new();
+ if prefix != "" {
+ ansi_strings.push(background_ansi_style.paint(prefix));
+ }
for (style, text) in superimpose_style_sections(syntax_sections, diff_sections) {
if config.width.is_some() {
text_width += text.graphemes(true).count();
@@ -122,7 +130,6 @@ impl<'a> Painter<'a> {
match config.width {
Some(width) if width > text_width => {
// Right pad to requested width with spaces.
- let background_style = config.no_style.apply(background_style_modifier);
paint_text(
&" ".repeat(width - text_width),
background_style,