Add --keep-plus-minus-markers option

This commit is contained in:
Dan Davison 2020-04-11 15:38:33 -04:00
parent 7a23a5c536
commit defc0eadff
6 changed files with 40 additions and 6 deletions

View File

@ -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

View File

@ -31,6 +31,7 @@ _delta() {
--minus-emph-color
--plus-color
--plus-emph-color
--keep-plus-minus-markers
--show-background-colors
--theme
--version

View File

@ -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.

View File

@ -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,

View File

@ -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,

View File

@ -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,