From 325abcfc061eeb5b54eb5cba45f43b91a5a5f4fc Mon Sep 17 00:00:00 2001 From: Dirk Date: Sun, 2 Aug 2015 00:03:30 +0200 Subject: [PATCH] - first shot for szepeviktor's color function maker #27 --- testssl.sh | 121 +++++++++++++++-------------------------------------- 1 file changed, 33 insertions(+), 88 deletions(-) diff --git a/testssl.sh b/testssl.sh index fe63e6e..4b64ecd 100755 --- a/testssl.sh +++ b/testssl.sh @@ -256,100 +256,44 @@ outln() { # color print functions, see also http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html +# color=1 functions pr_off() { [[ "$COLOR" -ne 0 ]] && out "\033[m\c" } - -pr_liteblueln() { pr_liteblue "$1"; outln; } -pr_liteblue() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;34m$1" || out "$1" - pr_off -} - -pr_blueln() { pr_blue "$1"; outln; } -pr_blue() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;34m$1" || out "$1" - pr_off -} - -pr_literedln() { pr_litered "$1"; outln; } -pr_litered() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;31m$1" || pr_bold "$1" - pr_off -} - -pr_redln() { pr_red "$1"; outln; } -pr_red() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;31m$1" || pr_bold "$1" - pr_off -} - -pr_litemagentaln() { pr_litemagenta "$1"; outln; } -pr_litemagenta() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;35m$1" || pr_underline "$1" - pr_off -} - -pr_magentaln() { pr_magenta "$1"; outln; } -pr_magenta() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;35m$1" || pr_underline "$1" - pr_off -} - -pr_litecyanln() { pr_litecyan "$1"; outln; } -pr_litecyan() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;36m$1" || out "$1" - pr_off -} - -pr_cyanln() { pr_cyan "$1"; outln; } -pr_cyan() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;36m$1" || out "$1" - pr_off -} - -pr_greyln() { pr_grey "$1"; outln; } -pr_grey() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;30m$1" || out "$1" - pr_off -} - -pr_litegreyln() { pr_litegrey "$1"; outln; } -pr_litegrey() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;37m$1" || out "$1" - pr_off -} - -pr_litegreenln() { pr_litegreen "$1"; outln; } -pr_litegreen() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;32m$1" || out "$1" - pr_off -} - -pr_greenln() { pr_green "$1"; outln; } -pr_green() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;32m$1" || out "$1" - pr_off -} - -pr_brownln() { pr_brown "$1"; outln; } -pr_brown() { - [[ "$COLOR" -eq 2 ]] && out "\033[0;33m$1" || out "$1" - pr_off -} - -pr_yellowln() { pr_yellow "$1"; outln; } -pr_yellow() { - [[ "$COLOR" -eq 2 ]] && out "\033[1;33m$1" || out "$1" - pr_off -} - -pr_boldln() { pr_bold "$1" ; outln; } pr_bold() { [[ "$COLOR" -ne 0 ]] && out "\033[1m$1" || out "$1" ; pr_off; } +pr_boldln() { pr_bold "$1" ; outln; } pr_underline() { [[ "$COLOR" -ne 0 ]] && out "\033[4m$1" || out "$1" ; pr_off; } -pr_boldandunder() { [[ "$COLOR" -ne 0 ]] && out "\033[1m\033[4m$1" || out "$1" ; pr_off; } pr_reverse() { [[ "$COLOR" -ne 0 ]] && out "\033[7m$1" || out "$1"; pr_off; } +declare -A COLORS=( [pr_green]='0\033[1;32m' [pr_litegreen]='0\033[0;32m' + [pr_yellow]='0\033[1;33m' [pr_brown]='0\033[0;33m' + [pr_blue]='0\033[1;34m' [pr_liteblue]='0\033[0;34m' + [pr_red]='0\033[1;31m' [pr_litered]='0\033[0;31m' + [pr_magenta]='0\033[1;35m' [pr_litemagenta]='0\033[0;35m' + [pr_cyan]='0\033[1;36m' [pr_litecyan]='0\033[0;36m' + [pr_grey]='0\033[1;30m' [pr_litegrey]='0\033[0;37m' ) +# [pr_off]='0\033[m' +# [pr_bold]='0\033[1m' +# [pr_underline]='0\033[4m' +# [pr_reverse]='0\033[7m') + +color_maker() { + local color_name + local color_code + + for color_name in ${!COLORS[*]}; do + color_code="${COLORS[$color_name]:1}" + # generate pr_* names from array + if [[ "$COLOR" -eq 2 ]]; then + eval "${color_name}() { out \"${color_code}\$1\"; pr_off; }" + else + eval "${color_name}() { out \"\$1\"; }" + fi + # generate *ln functions + eval "${color_name}ln() { ${color_name} \"\$1\"; outln; }" + done +} + ### colorswitcher (see e.g. https://linuxtidbits.wordpress.com/2008/08/11/output-color-on-bash-scripts/ ### http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html @@ -4580,6 +4524,7 @@ get_install_dir initialize_globals parse_cmd_line "$@" +color_maker set_color_functions find_openssl_binary maketempf @@ -4652,4 +4597,4 @@ fi exit $ret -# $Id: testssl.sh,v 1.329 2015/08/01 21:11:25 dirkw Exp $ +# $Id: testssl.sh,v 1.330 2015/08/01 21:58:58 dirkw Exp $