lib: Add _has_colors function and use it in log output

pull/1623/head
Noah Gorny 2020-06-23 15:42:22 +03:00
parent 862c154793
commit 3e7a95660f
2 changed files with 16 additions and 5 deletions

View File

@ -1,5 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function _has_colors()
{
# Check that stdout is a terminal
test -t 1 || return 1
ncolors=$(tput colors)
test -n "$ncolors" && test "$ncolors" -ge 8 || return 1
return 0
}
# colored ls # colored ls
export LSCOLORS='Gxfxcxdxdxegedabagacad' export LSCOLORS='Gxfxcxdxdxegedabagacad'

View File

@ -7,10 +7,11 @@ export BASH_IT_LOG_LEVEL_ALL=3
function _log_general() function _log_general()
{ {
_about 'Internal function used for logging' _about 'Internal function used for logging'
_param '1: message to log' _param '1: color of the message'
_param '2: message to log'
_group 'log' _group 'log'
echo -e "$1${echo_normal}" _has_colors && echo -e "$1$2${echo_normal}" || echo -e "$2"
} }
function _log_debug() function _log_debug()
@ -21,7 +22,7 @@ function _log_debug()
_group 'log' _group 'log'
[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return
_log_general "${echo_green}DEBUG: $1" _log_general "${echo_green}" "DEBUG: $1"
} }
function _log_warning() function _log_warning()
@ -32,7 +33,7 @@ function _log_warning()
_group 'log' _group 'log'
[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return
_log_general "${echo_yellow} WARN: $1" _log_general "${echo_yellow}" " WARN: $1"
} }
function _log_error() function _log_error()
@ -43,5 +44,5 @@ function _log_error()
_group 'log' _group 'log'
[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return [[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return
_log_general "${echo_red}ERROR: $1" _log_general "${echo_red}" "ERROR: $1"
} }