Skip to content

Commit

Permalink
Detect gsed and use it if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
anshul committed Feb 10, 2016
1 parent 11213ba commit e0fa502
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions diff-so-fancy
Expand Up @@ -4,31 +4,33 @@

diff_highlight="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/third_party/diff-highlight/diff-highlight"

hash gsed 2>/dev/null && SED=gsed || SED=sed

color_code_regex=$'(\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)[m|K])?'
reset_color="\x1B\[m"
dim_magenta="\x1B\[38;05;146m"

format_diff_header () {
# simplify the unified patch diff header
sed -E "s/^($color_code_regex)diff --git .*$//g" | \
sed -E "s/^($color_code_regex)index .*$/\
$SED -E "s/^($color_code_regex)diff --git .*$//g" | \
$SED -E "s/^($color_code_regex)index .*$/\
\1$(print_horizontal_rule)/g" | \
sed -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
$SED -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
\1$(print_horizontal_rule)/g"
}

colorize_context_line () {
# extra color for @@ context line
sed -E "s/@@$reset_color $reset_color(.*$)/@@ $dim_magenta\1/g"
$SED -E "s/@@$reset_color $reset_color(.*$)/@@ $dim_magenta\1/g"
}

strip_leading_symbols () {
# strip the + and -
sed -E "s/^($color_code_regex)[\+\-]/\1 /g"
$SED -E "s/^($color_code_regex)[\+\-]/\1 /g"
}

print_horizontal_rule () {
printf "%$(tput cols)s\n"| sed 's/ /─/g'
printf "%$(tput cols)s\n"| $SED 's/ /─/g'
}

# run it.
Expand Down

0 comments on commit e0fa502

Please sign in to comment.