From c1b333988fa1648a72087cf40c6b91b5f9aacf35 Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 8 Oct 2015 10:24:31 +0800 Subject: [PATCH 1/3] git-scp: use portable terminal escape sequences --- bin/git-scp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/git-scp b/bin/git-scp index 7481b6087..e12c3c9a3 100755 --- a/bin/git-scp +++ b/bin/git-scp @@ -1,10 +1,10 @@ #!/usr/bin/env bash -COLOR_RED() { echo -en "\033[31m"; } -COLOR_GREEN() { echo -en "\033[32m"; } -COLOR_YELLOW(){ echo -en "\033[33m"; } -COLOR_BLUE() { echo -en "\033[34m"; } -COLOR_RESET() { echo -en "\033[0m"; } +COLOR_RED() { test -t 1 && echo -n "$(tput setaf 1)"; } +COLOR_GREEN() { test -t 1 && echo -n "$(tput setaf 2)"; } +COLOR_YELLOW(){ test -t 1 && echo -n "$(tput setaf 3)"; } +COLOR_BLUE() { test -t 1 && echo -n "$(tput setaf 4)"; } +COLOR_RESET() { test -t 1 && echo -n "$(tput sgr 0)"; } function _test_git_scp() { From 6a1e4017271548aa3ceca5eca383252519a9e7bd Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 8 Oct 2015 14:06:46 +0800 Subject: [PATCH 2/3] replace wildcard '?' to literal '?' Previously the `?` in the case statement will match any single letter. As a result, `git scp a` will act as the same as `git scp -h`. What we actually need is a literal '?'. --- bin/git-scp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-scp b/bin/git-scp index e12c3c9a3..c9c2f2142 100755 --- a/bin/git-scp +++ b/bin/git-scp @@ -170,7 +170,7 @@ function _error() case $(basename $0) in git-scp) case $1 in - ''|-h|?|help|--help) shift; _test_git_scp; _usage $@;; + ''|-h|'?'|help|--help) shift; _test_git_scp; _usage $@;; *) scp_and_stage $@;; esac ;; From 1d7e07e0d84eca2ca821fea5553c1f475b9d843d Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 8 Oct 2015 16:45:53 +0800 Subject: [PATCH 3/3] disable color if the output is not printed to tty --- bin/git-effort | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/bin/git-effort b/bin/git-effort index e594f7187..0e79ed3f0 100755 --- a/bin/git-effort +++ b/bin/git-effort @@ -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= # @@ -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 } @@ -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 @@ -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