From 67ea2735e60be9acdcf9be213265ec7f16930077 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Sun, 24 Mar 2019 17:17:36 +0100 Subject: [PATCH] nux: Improved color handling in terminals. Signed-off-by: Tony Tkacik --- inc/nux/color.nuxsh.sh | 5 +++++ inc/nux/log.inc.sh | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/inc/nux/color.nuxsh.sh b/inc/nux/color.nuxsh.sh index 9f0cf45..fa69967 100644 --- a/inc/nux/color.nuxsh.sh +++ b/inc/nux/color.nuxsh.sh @@ -1,3 +1,5 @@ +nux.use nux/log + if [ -t 1 ] { readonly nc_bold=`tput setaf 0` readonly nc_bg_bold=`tput setab 0` @@ -39,4 +41,7 @@ if [ -t 1 ] { readonly NC_White=$nc_white readonly NC_error=$NC_Red + } + +nux.log trace Terminal colors enabled diff --git a/inc/nux/log.inc.sh b/inc/nux/log.inc.sh index d49ef8b..3e8532c 100644 --- a/inc/nux/log.inc.sh +++ b/inc/nux/log.inc.sh @@ -1,16 +1,20 @@ ## ##Logging # Color for message levels -NC_LOG_color_info=$NC_LightGray -NC_LOG_color_error=$NC_LightRed -NC_LOG_color_warning=$NC_Yellow -NC_LOG_color_debug=$NC_White +if [ -t 1 ]; then + NC_LOG_color_1='\033[1;31m' + NC_LOG_color_2=`tput setaf 3` + NC_LOG_color_3='\033[0;37m' + NC_LOG_color_4=`tput setaf 7` + NC_LOG_No=`tput sgr0` +fi NC_LOG_current=${NC_LOG_current:=3} NC_LOG_id_none=0 NC_LOG_id_error=1 NC_LOG_id_warning=2 +NC_LOG_id_warn=2 NC_LOG_id_info=3 NC_LOG_id_debug=4 NC_LOG_id_trace=5 @@ -33,11 +37,11 @@ NC_LOG_id_trace=5 function nux.log { local level=$1 local message=$2 - local color=NC_LOG_color_$level local level_num=NC_LOG_id_$level + local color=NC_LOG_color_${!level_num} shift; if [ ${!level_num} -le $NC_LOG_current ]; then - echo -e "${!color}[$level]$NC_No $*$NC_No" >&2 + echo -e "${!color}[$level]$NC_LOG_No $*$NC_LOG_No" >&2 fi } @@ -50,3 +54,5 @@ function nux.log.level { local level_id=NC_LOG_id_$level NC_LOG_current=${!level_id} } + +nux.log trace Nux Logger included