Skip to content

Commit e178338

Browse files
Merge branch 'patch-mode-35' of https://github.com/wren/diff-so-fancy into next
2 parents c0099d9 + 13d3f89 commit e178338

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ git config --global color.diff-highlight.newHighlight "green bold 22"
3838

3939
git config --global color.diff.meta "11"
4040
git config --global color.diff.frag "magenta bold"
41+
git config --global color.diff.func "146 bold"
4142
git config --global color.diff.commit "yellow bold"
4243
git config --global color.diff.old "red bold"
4344
git config --global color.diff.new "green bold"

diff-so-fancy

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use warnings FATAL => 'all';
1818
my $remove_file_add_header = 1;
1919
my $remove_file_delete_header = 1;
2020
my $clean_permission_changes = 1;
21+
my $patch_mode = 0;
2122
my $manually_color_lines = 0; # Usually git/hg colorizes the lines, but for raw patches we use this
2223
my $change_hunk_indicators = git_config_boolean("diff-so-fancy.changeHunkIndicators","true");
2324
my $strip_leading_indicators = git_config_boolean("diff-so-fancy.stripLeadingSymbols","true");
@@ -52,6 +53,13 @@ if ($args->{color_on}) {
5253
$color_forced = 1;
5354
}
5455

56+
# `git add --patch` requries our output to match the number of lines from the
57+
# input. So, when patch mode is active, we print out empty lines to pad our
58+
# output to match any lines we've consumed.
59+
if ($args->{patch}) {
60+
$patch_mode = 1;
61+
}
62+
5563
# We only process ARGV if we don't have STDIN
5664
if (!$has_stdin) {
5765
if ($args->{v} || $args->{version}) {
@@ -179,6 +187,10 @@ sub do_dsf_stuff {
179187

180188
$last_file_seen =~ s|^\w/||; # Remove a/ (and handle diff.mnemonicPrefix).
181189
$in_hunk = 0;
190+
if ($patch_mode) {
191+
# we are consuming one line, and the debt must be paid
192+
print "\n";
193+
}
182194
########################################
183195
# Find the first file: --- a/README.md #
184196
########################################
@@ -282,6 +294,9 @@ sub do_dsf_stuff {
282294
} elsif ($remove_file_delete_header && $line =~ /^${ansi_color_regex}deleted file mode/) {
283295
# Don't print the line (i.e. remove it from the output);
284296
$last_file_mode = "delete";
297+
if ($patch_mode) {
298+
print "\n";
299+
}
285300
################################
286301
# Look for binary file changes #
287302
################################
@@ -302,6 +317,10 @@ sub do_dsf_stuff {
302317
}
303318

304319
my ($new_mode) = $next =~ m/new mode (\d+)/;
320+
321+
if ($patch_mode) {
322+
print "\n";
323+
}
305324
print "$last_file_seen changed file mode from $old_mode to $new_mode\n";
306325

307326
###############
@@ -544,16 +563,23 @@ sub strip_leading_indicators {
544563
if ($manually_color_lines) {
545564
if (defined($5) && $5 eq "+") {
546565
my $add_line_color = get_config_color("add_line");
547-
$line = $add_line_color . $line . $reset_color;
566+
$line = $add_line_color . insert_reset_at_line_end($line);
548567
} elsif (defined($5) && $5 eq "-") {
549568
my $remove_line_color = get_config_color("remove_line");
550-
$line = $remove_line_color . $line . $reset_color;
569+
$line = $remove_line_color . insert_reset_at_line_end($line);
551570
}
552571
}
553572

554573
return $line;
555574
}
556575

576+
# Insert the color reset code at end of line, but before any newlines
577+
sub insert_reset_at_line_end {
578+
my $line = shift();
579+
$line =~ s/^(.*)([\n\r]+)?$/${1}${reset_color}${2}/;
580+
return $line;
581+
}
582+
557583
# Count the number of a given char in a string
558584
sub char_count {
559585
my ($needle,$str) = @_;

pro-tips.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,12 @@ zplug "zdharma/zsh-diff-so-fancy", as:command, use:bin/git-dsf
5555
zgen zdharma/zsh-diff-so-fancy
5656
```
5757

58+
#### `hg` configuration
59+
60+
You can configure `hg diff` output to use `diff-so-fancy` by adding this alias
61+
to your `hgrc` file:
62+
63+
```
64+
[alias]
65+
diff = !HGPLAIN=1 $HG diff --pager=on --config pager.pager=diff-so-fancy $@
66+
```

0 commit comments

Comments
 (0)