Skip to content

Commit

Permalink
Clean up the headers to indicate a file was added/removed/modified
Browse files Browse the repository at this point in the history
Also makes the file name "clickable" by removing the leading a/ and /b
  • Loading branch information
Scott Baker committed Feb 29, 2016
1 parent 626363f commit 68de5d2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ print_horizontal_rule () {
printf "%$(tput cols)s\n"| $SED 's/ /─/g'
}

print_header_clean () {
"$(get_script_dir)/libs/header_clean/header_clean.pl"
}

# run it.
# shellcheck disable=SC2002
cat $input \
Expand All @@ -79,4 +83,5 @@ cat $input \
| mark_empty_lines \
| remove_first_empty_line \
| strip_leading_symbols \
| strip_first_column
| strip_first_column \
| print_header_clean
43 changes: 43 additions & 0 deletions libs/header_clean/header_clean.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/perl

use strict;
use warnings;

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

my ($f1,$f2);
while (my $l = <>) {
# Find the first file: --- a/README.md
if ($l =~ /^$ansi_sequence_regex--- (a\/)?(.+?)(\e|$)/) {
$f1 = $5;
# Find the second file: +++ b/README.md
} elsif ($l =~ /^$ansi_sequence_regex\+\+\+ (b\/)?(.+?)(\e|$)/) {
if ($1) {
print $1; # Print out whatever color we're using
}
$f2 = $5;

# If they're the same it's a modify
if ($f1 eq $f2) {
print "modified: $f1\n";
# If the first is /dev/null it's a new file
} elsif ($f1 eq "/dev/null") {
print "added: $f2\n";
# If the second is /dev/null it's a deletion
} elsif ($f2 eq "/dev/null") {
print "deleted: $f1\n";
# If the files aren't the same it's a rename
} elsif ($f1 ne $f2) {
print "renamed: $f1 to $f2\n";
# Something we haven't thought of yet
} else {
print "$f1 -> $f2\n";
}

# Reset the vars
my ($f1,$f2);
# Just a regular line, print it out
} else {
print $l;
}
}

0 comments on commit 68de5d2

Please sign in to comment.