Skip to content

Commit

Permalink
Move strip_leading_symbols() and strip_first_column() to Perl
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Baker committed Mar 9, 2016
1 parent 353cb07 commit efc22bb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
2 changes: 0 additions & 2 deletions diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,4 @@ cat $input \
| format_diff_header \
| colorize_context_line \
| mark_empty_lines \
| strip_leading_symbols \
| strip_first_column \
| print_header_clean
63 changes: 53 additions & 10 deletions libs/header_clean/header_clean.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
my $remove_file_delete_header = 1;
my $clean_permission_changes = 1;
my $change_hunk_indicators = 1;
my $strip_leading_indicators = 1;

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

my $ansi_sequence_regex = qr/(\e\[([0-9]{1,3}(;[0-9]{1,3}){0,3})[mK])?/;

my ($file_1,$file_2,$last_file_seen);
my @input = <>;
strip_empty_first_line(\@input);
clean_up_input(\@input);

my $ansi_sequence_regex = qr/(\e\[([0-9]{1,3}(;[0-9]{1,3}){0,3})[mK])?/;
my ($file_1,$file_2,$last_file_seen);
for (my $i = 0; $i <= $#input; $i++) {
my $line = $input[$i];

Expand Down Expand Up @@ -106,12 +106,11 @@

# Courtesy of github.com/git/git/blob/ab5d01a/git-add--interactive.perl#L798-L805
sub parse_hunk_header {
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
$line =~ /^@@+(?: -(\d+)(?:,(\d+))?)+ \+(\d+)(?:,(\d+))? @@+/;
$o_cnt = 1 unless defined $o_cnt;
$n_cnt = 1 unless defined $n_cnt;
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = $line =~ /^@@+(?: -(\d+)(?:,(\d+))?)+ \+(\d+)(?:,(\d+))? @@+/;
$o_cnt = 1 unless defined $o_cnt;
$n_cnt = 1 unless defined $n_cnt;
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
}

sub strip_empty_first_line {
Expand All @@ -122,5 +121,49 @@ sub strip_empty_first_line {
shift(@$array); # Throw away the first line
}

return 1;
}

# Remove + or - at the beginning of the lines
sub strip_leading_indicators {
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])?/;

for (my $i = 0; $i < $line_count; $i++) {
$array->[$i] =~ s/^(${ansi_color_regex})[+-]/$1 /;
}

return 1;
}

# Remove the first space so everything aligns left
sub strip_first_column {
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])?/;

for (my $i = 0; $i < $line_count; $i++) {
$array->[$i] =~ s/^(${ansi_color_regex})[[:space:]]/$1/;
}

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);

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

# Remove the first space so everything aligns left
strip_first_column($input_array_ref);
}


return 1;
}

0 comments on commit efc22bb

Please sign in to comment.