summaryrefslogtreecommitdiff
path: root/diff-so-fancy
diff options
context:
space:
mode:
authorAnton Bobov <abobov@gmail.com>2021-05-07 09:44:49 +0500
committerAnton Bobov <abobov@gmail.com>2021-05-07 10:10:39 +0500
commitf563df749c7638ac3c7d82ae6ab48895581c10b9 (patch)
tree09f5abeb0173a4e858fb4d509cd85f6a5a9ed7ab /diff-so-fancy
parent91d03df8a6ea971f1adbc071fe1f4e056f0e7b36 (diff)
Updates.
Diffstat (limited to 'diff-so-fancy')
-rwxr-xr-xdiff-so-fancy92
1 files changed, 84 insertions, 8 deletions
diff --git a/diff-so-fancy b/diff-so-fancy
index 8dc9a99..7d33a34 100755
--- a/diff-so-fancy
+++ b/diff-so-fancy
@@ -332,7 +332,7 @@ unshift @INC, bless \%fatpacked, $class;
} # END OF FATPACK CODE
-my $VERSION = "1.3.0";
+my $VERSION = "1.4.0";
#################################################################################
@@ -350,6 +350,7 @@ use warnings FATAL => 'all';
my $remove_file_add_header = 1;
my $remove_file_delete_header = 1;
my $clean_permission_changes = 1;
+my $patch_mode = 0;
my $manually_color_lines = 0; # Usually git/hg colorizes the lines, but for raw patches we use this
my $change_hunk_indicators = git_config_boolean("diff-so-fancy.changeHunkIndicators","true");
my $strip_leading_indicators = git_config_boolean("diff-so-fancy.stripLeadingSymbols","true");
@@ -384,6 +385,18 @@ if ($args->{color_on}) {
$color_forced = 1;
}
+if ($args->{debug}) {
+ show_debug_info();
+ exit();
+}
+
+# `git add --patch` requires our output to match the number of lines from the
+# input. So, when patch mode is active, we print out empty lines to pad our
+# output to match any lines we've consumed.
+if ($args->{patch}) {
+ $patch_mode = 1;
+}
+
# We only process ARGV if we don't have STDIN
if (!$has_stdin) {
if ($args->{v} || $args->{version}) {
@@ -511,6 +524,10 @@ sub do_dsf_stuff {
$last_file_seen =~ s|^\w/||; # Remove a/ (and handle diff.mnemonicPrefix).
$in_hunk = 0;
+ if ($patch_mode) {
+ # we are consuming one line, and the debt must be paid
+ print "\n";
+ }
########################################
# Find the first file: --- a/README.md #
########################################
@@ -608,12 +625,18 @@ sub do_dsf_stuff {
} elsif ($remove_file_add_header && $line =~ /^${ansi_color_regex}.*new file mode/) {
# Don't print the line (i.e. remove it from the output);
$last_file_mode = "add";
+ if ($patch_mode) {
+ print "\n";
+ }
######################################
# Remove any delete file permissions #
######################################
} elsif ($remove_file_delete_header && $line =~ /^${ansi_color_regex}deleted file mode/) {
# Don't print the line (i.e. remove it from the output);
$last_file_mode = "delete";
+ if ($patch_mode) {
+ print "\n";
+ }
################################
# Look for binary file changes #
################################
@@ -634,6 +657,10 @@ sub do_dsf_stuff {
}
my ($new_mode) = $next =~ m/new mode (\d+)/;
+
+ if ($patch_mode) {
+ print "\n";
+ }
print "$last_file_seen changed file mode from $old_mode to $new_mode\n";
###############
@@ -812,11 +839,15 @@ sub in_unit_test {
sub get_less_charset {
my @less_char_vars = ("LESSCHARSET", "LESSCHARDEF", "LC_ALL", "LC_CTYPE", "LANG");
- foreach (@less_char_vars) {
- return $ENV{$_} if defined $ENV{$_};
+ foreach my $key (@less_char_vars) {
+ my $val = $ENV{$key};
+
+ if (defined $val) {
+ return ($key, $val);
+ }
}
- return "";
+ return ();
}
sub should_print_unicode {
@@ -826,7 +857,8 @@ sub should_print_unicode {
}
# Otherwise, assume we're piping to less(1)
- my $less_charset = get_less_charset();
+ my ($less_env_var, $less_charset) = get_less_charset();
+ $less_charset //= "";
if ($less_charset =~ /utf-?8/i) {
return 1;
}
@@ -876,16 +908,23 @@ sub strip_leading_indicators {
if ($manually_color_lines) {
if (defined($5) && $5 eq "+") {
my $add_line_color = get_config_color("add_line");
- $line = $add_line_color . $line . $reset_color;
+ $line = $add_line_color . insert_reset_at_line_end($line);
} elsif (defined($5) && $5 eq "-") {
my $remove_line_color = get_config_color("remove_line");
- $line = $remove_line_color . $line . $reset_color;
+ $line = $remove_line_color . insert_reset_at_line_end($line);
}
}
return $line;
}
+# Insert the color reset code at end of line, but before any newlines
+sub insert_reset_at_line_end {
+ my $line = shift();
+ $line =~ s/^(.*)([\n\r]+)?$/${1}${reset_color}${2}/;
+ return $line;
+}
+
# Count the number of a given char in a string
sub char_count {
my ($needle,$str) = @_;
@@ -1017,9 +1056,13 @@ diff -u one.txt two.txt | diff-so-fancy # Use d-s-f on unified diff output
diff-so-fancy --colors # View the commands to set the recommended colors
diff-so-fancy --set-defaults # Configure git-diff to use diff-so-fancy and suggested colors
+diff-so-fancy --patch # Use diff-so-fancy in patch mode (interoperable with `git add --patch`)
# Configure git to use d-s-f for *all* diff operations
-git config --global core.pager \"diff-so-fancy | less --tabs=4 -RFX\"\n";
+git config --global core.pager \"diff-so-fancy | less --tabs=4 -RFX\"
+
+# Configure git to use d-s-f for `git add --patch`
+git config --global interactive.diffFilter \"diff-so-fancy --patch\"\n";
return $out;
}
@@ -1226,4 +1269,37 @@ sub get_terminal_width {
return $width;
}
+sub show_debug_info {
+ my @less = get_less_charset();
+ my $git_ver = trim(`git --version`);
+ $git_ver =~ s/[^\d.]//g;
+
+ print "Diff-so-fancy : v$VERSION\n";
+ print "Git : v$git_ver\n";
+ print "Perl : $^V\n";
+ print "\n";
+
+ print "Terminal width : " . get_terminal_width() . "\n";
+ print "Terminal \$LANG : " . $ENV{LANG} . "\n";
+ print "\n";
+ print "Supports Unicode: " . yes_no(should_print_unicode()) . "\n";
+ print "Unicode Ruler : " . yes_no($use_unicode_dash_for_ruler) . "\n";
+ print "\n";
+ print "Less Charset Var: " . ($less[0] // "") . "\n";
+ print "Less Charset : " . ($less[1] // "") . "\n";
+ print "\n";
+ print "Is Windows : " . yes_no(is_windows()) . "\n";
+ print "Operating System: $^O\n";
+}
+
+sub yes_no {
+ my $val = shift();
+
+ if ($val) {
+ return "Yes";
+ } else {
+ return "No";
+ }
+}
+
# vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4