From 3b6c49d6bd34c79e2c7ce9a05a2426867a2c61b3 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 18 May 2015 18:41:45 +0800 Subject: [PATCH] bash-colors: add portable version --- .bin/bash-colors | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/.bin/bash-colors b/.bin/bash-colors index a2bdff8..ed61f8b 100755 --- a/.bin/bash-colors +++ b/.bin/bash-colors @@ -1,14 +1,21 @@ #!/bin/bash # Author: Mike Frysinger ; released into the public domain; suck it. +# This is for bash. +BASH_PREFIX="\e" +# And this is the portable one. +POSIX_PREFIX="\033" +# Default to bash. +PREFIX=${BASH_PREFIX} + show_env() { local c colors=( - GOOD="\$'\e[32;01m'" - WARN="\$'\e[33;01m'" - BAD="\$'\e[31;01m'" - HILITE="\$'\e[36;01m'" - BRACKET="\$'\e[34;01m'" - NORMAL="\$'\e[0m'" + GOOD="\$'${PREFIX}[32;01m'" + WARN="\$'${PREFIX}[33;01m'" + BAD="\$'${PREFIX}[31;01m'" + HILITE="\$'${PREFIX}[36;01m'" + BRACKET="\$'${PREFIX}[34;01m'" + NORMAL="\$'${PREFIX}[0m'" ) for c in "${colors[@]}" ; do [[ -t 0 ]] || c="${c%=*}=" @@ -26,10 +33,15 @@ check256() { } showit() { - echo -e '\e['"${1};${2}mthis color is: \\\\e[${1};${2}m \e[0m" + local col="${PREFIX}[${1};${2}m" + printf '%bthis color is: %s %b\n' "${col}" "${col}" "${PREFIX}[0m" } main() { + if [[ $1 == "--posix" ]] ; then + PREFIX=${POSIX_PREFIX} + shift + fi case $1 in --env) show_env @@ -86,7 +98,7 @@ main() { ;; *) cat <<-EOF - usage: $0 [colors] + usage: $0 [--posix] [colors] Colors: - --env - show env you can eval in a shell -- 2.39.5