Skip to content

Commit

Permalink
disable color if the output is not printed to tty
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Oct 8, 2015
1 parent 6a1e401 commit 1d7e07e
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions bin/git-effort
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

tmp=$(git_extra_mktemp)
above=0
# if the output won't be printed to tty, disable the color
test -t 1 && to_tty=true
color=

#
Expand Down Expand Up @@ -56,15 +58,19 @@ active_days() {
#

color_for() {
if [ $1 -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
elif [ $1 -gt 150 ]; then color="$(tputq setaf 1)" # red
elif [ $1 -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
elif [ $1 -gt 100 ]; then color="$(tputq setaf 2)" # green
elif [ $1 -gt 75 ]; then color="$(tputq setaf 93)$(tputq bold)"
elif [ $1 -gt 50 ]; then color="$(tputq setaf 93)" # purplish
elif [ $1 -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
elif [ $1 -gt 10 ]; then color="$(tputq setaf 3)" # yellow
else color="$(tputq sgr0)" # default color
if [ "$to_tty" = true ]; then
if [ $1 -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
elif [ $1 -gt 150 ]; then color="$(tputq setaf 1)" # red
elif [ $1 -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
elif [ $1 -gt 100 ]; then color="$(tputq setaf 2)" # green
elif [ $1 -gt 75 ]; then color="$(tputq setaf 93)$(tputq bold)"
elif [ $1 -gt 50 ]; then color="$(tputq setaf 93)" # purplish
elif [ $1 -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
elif [ $1 -gt 10 ]; then color="$(tputq setaf 3)" # yellow
else color="$(tputq sgr0)" # default color
fi
else
color=""
fi
}

Expand All @@ -76,7 +82,8 @@ effort() {
path=$1
local commit_dates
local color reset_color commits len dot f_dot i msg active
reset_color="$(tputq sgr0)"
reset_color=""
test "$to_tty" = true && reset_color="$(tputq sgr0)"
commit_dates=`dates "$path"`
[ $? -gt 0 ] && exit 255

Expand Down Expand Up @@ -191,6 +198,7 @@ export -f color_for
export -f active_days
export -f dates
export -f tputq
export to_tty
export above
export log_args

Expand Down

0 comments on commit 1d7e07e

Please sign in to comment.