From 620abb2ffeadb63ca25e18317abf373b324ab498 Mon Sep 17 00:00:00 2001 From: Menno van Leeuwen Date: Tue, 11 Mar 2025 11:10:22 +0100 Subject: [PATCH] feat: implement colored logging functions for improved visibility and formatting --- setup.sh | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/setup.sh b/setup.sh index 8802ccd..257b632 100755 --- a/setup.sh +++ b/setup.sh @@ -6,30 +6,102 @@ IFS=$'\n\t' # Constants readonly NIXOS_RELEASE="24.11" # Home Manager release version (Must match NixOS version) readonly GIT_REPO="https://git.mvl.sh/vleeuwenmenno/dotfiles.git" # Dotfiles repository URL -readonly DOTFILES_PATH="${HOME}/.dotfiles" # Dotfiles directory +readonly DOTFILES_PATH="${HOME}/.dotfiles" # Dotfiles directory readonly SETUP_MARKER="${HOME}/.dotfiles-setup" # Setup marker file indicates setup has been run -# Color constants -readonly RED='\033[0;31m' -readonly GREEN='\033[0;32m' -readonly YELLOW='\033[0;33m' -readonly NC='\033[0m' # No Color +#Color print function, usage: println "message" "color" +println() { + color=$2 + printfe "%s\n" $color "$1" +} + +# print colored with printf (args: format, color, message ...) +printfe() { + format=$1 + color=$2 + message=$3 + show_time=true + + # Check if $4 is explicitly set to false, otherwise default to true + if [ ! -z "$4" ] && [ "$4" == "false" ]; then + show_time=false + fi + + red=$(tput setaf 1) + green=$(tput setaf 2) + yellow=$(tput setaf 3) + blue=$(tput setaf 4) + magenta=$(tput setaf 5) + cyan=$(tput setaf 6) + normal=$(tput sgr0) + grey=$(tput setaf 8) + + case $color in + "red") + color=$red + ;; + "green") + color=$green + ;; + "yellow") + color=$yellow + ;; + "blue") + color=$blue + ;; + "magenta") + color=$magenta + ;; + "cyan") + color=$cyan + ;; + "grey") + color=$grey + ;; + *) + color=$normal + ;; + esac + + if [ "$show_time" == "false" ]; then + printf "$color$format$normal" "$message" + return + fi + + printf $grey"%s" "$(date +'%H:%M:%S')"$normal + + case $color in + $green | $cyan | $blue | $magenta | $normal) + printf "$green INF $normal" + ;; + $yellow) + printf "$yellow WRN $normal" + ;; + $red) + printf "$red ERR $normal" + ;; + *) + printf "$normal" + ;; + esac + printf "$color$format$normal" "$message" +} # Helper functions log_info() { - echo -e "${YELLOW}$1${NC}" + println "$1" "green" } log_success() { - echo -e "${GREEN}$1${NC}" + println "$1" "green" } log_error() { - echo -e "${RED}$1${NC}" >&2 + println "$1" "red" >&2 } log_warning() { - echo -e "${YELLOW}$1${NC}" >&2 + println "$1" "yellow" >&2 } die() {