Skip to content

Commit

Permalink
Move mark_empty_lines() to Perl
Browse files Browse the repository at this point in the history
We're down to five calls to sed
  • Loading branch information
Scott Baker committed Mar 9, 2016
1 parent 0ea95e5 commit cbd916a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 0 additions & 5 deletions diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ colorize_context_line () {
$SED -E "s/@@$reset_color $reset_color(.*$)/@@ $dim_magenta\1/g"
}

mark_empty_lines () {
$SED -E "s/^$color_code_regex[\+\-]$reset_color\s*$/$invert_color\1 $reset_escape/g"
}

print_horizontal_rule () {
let width="$(tput cols)"

Expand All @@ -74,5 +70,4 @@ cat $input \
| $diff_highlight \
| format_diff_header \
| colorize_context_line \
| mark_empty_lines \
| print_header_clean
21 changes: 21 additions & 0 deletions libs/header_clean/header_clean.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
my $clean_permission_changes = 1;
my $change_hunk_indicators = 1;
my $strip_leading_indicators = 1;
my $mark_empty_lines = 1;

#################################################################################

Expand Down Expand Up @@ -150,12 +151,32 @@ sub strip_first_column {
return 1;
}

sub mark_empty_lines {
my $array = shift(); # Array passed in by reference
my $line_count = scalar(@$array);
my $ansi_color_regex = qr/(\e\[[0-9]{1,3}(?:;[0-9]{1,3}){0,3}[mK])?/;

my $reset_color = "\e\\[0?m";
my $reset_escape = "\e\[m";
my $invert_color = "\e\[7m";

foreach my $line (@$array) {
$line =~ s/^(${ansi_color_regex})[+-]$reset_color\s*$/$invert_color$1 $reset_escape\n/;
}

return 1;
}

sub clean_up_input {
my $input_array_ref = shift();

# Usually the first line of a diff is whitespace so we remove that
strip_empty_first_line($input_array_ref);

if ($mark_empty_lines) {
mark_empty_lines($input_array_ref);
}

# Remove + or - at the beginning of the lines
if ($strip_leading_indicators) {
strip_leading_indicators($input_array_ref);
Expand Down

0 comments on commit cbd916a

Please sign in to comment.