From 4d1035e241ff075f50076d1763a178db6125ba9f Mon Sep 17 00:00:00 2001 From: Jason Phan Date: Wed, 14 Nov 2018 23:09:17 -0600 Subject: [PATCH] added: colors (#26) Support Colors #12 * added: colors No more echo with variables in them. Think of it like the Holocaust... Never. Again. * update: removed color codes from tests --- .hastest.bats | 20 ++++++++++---------- has | 26 +++++++++++--------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/.hastest.bats b/.hastest.bats index 39132a8..e4e1f69 100644 --- a/.hastest.bats +++ b/.hastest.bats @@ -3,28 +3,28 @@ @test "works with single command check" { run bash has git - [ "$status" -eq 0 ] - [[ $output == *"✔ git"* ]] + [[ "$status" -eq 0 ]] + [[ "$(echo "${output}" | grep "✔" | grep "git")" ]] } @test "safely tells about tools not configured" { run bash has foobar - [ "$status" -eq 1 ] - [[ $output == *"✘ foobar not understood"* ]] + [[ "$status" -eq 1 ]] + [[ "$(echo "${output}" | grep "✘" | grep "foobar not understood")" ]] } @test "env var lets override safety check" { HAS_ALLOW_UNSAFE=y run bash has foobar - [ "$status" -eq 1 ] - [[ $output == *"✘ foobar"* ]] + [[ "$status" -eq 1 ]] + [[ "$(echo "${output}" | grep "✘" | grep "foobar")" ]] } @test "status code reflects number of failed commands" { - HAS_ALLOW_UNSAFE=y run bash has foobar make git barbaz + HAS_ALLOW_UNSAFE=y run bash has foobar bc git barbaz - [ "$status" -eq 2 ] - [[ $output == *"✘ foobar"* ]] - [[ $output == *"✘ barbaz"* ]] + [[ "$status" -eq 2 ]] + [[ "$(echo "${output}" | grep "✘" | grep "foobar")" ]] + [[ "$(echo "${output}" | grep "✘" | grep "barbaz")" ]] } diff --git a/has b/has index ea23b87..d573c39 100755 --- a/has +++ b/has @@ -6,8 +6,8 @@ set -o pipefail ## constant - symbols for success failure -PASS='✔' -FAIL='✘' +PASS="\e[1m\e[38;5;2m✔\e[m" +FAIL="\e[1m\e[38;5;1m✘\e[m" ## These variables are used to keep track of passed and failed commands OK=0 @@ -143,23 +143,19 @@ __detect(){ if [ "$status" -eq "-1" ]; then ## When unsafe processing is not allowed, the -1 signifies - - echo ${FAIL} ${command} "not understood" + printf "${FAIL} ${command} not understood\n" KO=$(($KO+1)) elif [ ${status} -eq 127 ]; then ## command not installed - - echo ${FAIL} ${command} + printf "${FAIL} ${command}\n" KO=$(($KO+1)) elif [ ${status} -eq 0 ] || [ ${status} -eq 141 ]; then ## successfully executed - - echo ${PASS} ${command} ${version} + printf "${PASS} ${command} ${version}\n" OK=$(($OK+1)) else ## as long as its not 127, command is there, but we might not have been able to extract version - - echo ${PASS} ${command} + printf "${PASS} ${command}\n" OK=$(($OK+1)) fi @@ -171,17 +167,17 @@ if [ "$#" -eq 0 ]; then # print help BINARY_NAME="has" VERSION="v1.2.1" - echo "${BINARY_NAME} ${VERSION}" - echo "USAGE: ${BINARY_NAME} .." - echo "EXAMPLE: ${BINARY_NAME} git curl node" + printf "${BINARY_NAME} ${VERSION}\n" + printf "USAGE: ${BINARY_NAME} ..\n" + printf "EXAMPLE: ${BINARY_NAME} git curl node\n" else - # for each arg for cmd in "$@"; do __detect $cmd done # echo ${OK} / $(($OK+$KO)) - exit ${KO} + exit "${KO}" fi +