#!/bin/bash # UnitForge Color Utility Library # Centralized ANSI color definitions and utility functions for consistent terminal output # ANSI Color Codes export RED='\033[0;31m' export GREEN='\033[0;32m' export YELLOW='\033[1;33m' export BLUE='\033[0;34m' export PURPLE='\033[0;35m' export CYAN='\033[0;36m' export WHITE='\033[0;37m' export GRAY='\033[0;90m' # Bright colors export BRIGHT_RED='\033[1;31m' export BRIGHT_GREEN='\033[1;32m' export BRIGHT_YELLOW='\033[1;33m' export BRIGHT_BLUE='\033[1;34m' export BRIGHT_PURPLE='\033[1;35m' export BRIGHT_CYAN='\033[1;36m' export BRIGHT_WHITE='\033[1;37m' # Background colors export BG_RED='\033[41m' export BG_GREEN='\033[42m' export BG_YELLOW='\033[43m' export BG_BLUE='\033[44m' export BG_PURPLE='\033[45m' export BG_CYAN='\033[46m' export BG_WHITE='\033[47m' # Text formatting export BOLD='\033[1m' export DIM='\033[2m' export ITALIC='\033[3m' export UNDERLINE='\033[4m' export BLINK='\033[5m' export REVERSE='\033[7m' export STRIKETHROUGH='\033[9m' # Reset export NC='\033[0m' # No Color export RESET='\033[0m' # Utility functions for colored output color_echo() { local color="$1" shift echo -e "${color}$*${NC}" } # Convenience functions red() { color_echo "$RED" "$@"; } green() { color_echo "$GREEN" "$@"; } yellow() { color_echo "$YELLOW" "$@"; } blue() { color_echo "$BLUE" "$@"; } purple() { color_echo "$PURPLE" "$@"; } cyan() { color_echo "$CYAN" "$@"; } white() { color_echo "$WHITE" "$@"; } gray() { color_echo "$GRAY" "$@"; } # Bright variants bright_red() { color_echo "$BRIGHT_RED" "$@"; } bright_green() { color_echo "$BRIGHT_GREEN" "$@"; } bright_yellow() { color_echo "$BRIGHT_YELLOW" "$@"; } bright_blue() { color_echo "$BRIGHT_BLUE" "$@"; } bright_purple() { color_echo "$BRIGHT_PURPLE" "$@"; } bright_cyan() { color_echo "$BRIGHT_CYAN" "$@"; } bright_white() { color_echo "$BRIGHT_WHITE" "$@"; } # Formatted output functions info() { echo -e "${BLUE}ℹ${NC} $*" } success() { echo -e "${GREEN}āœ“${NC} $*" } warning() { echo -e "${YELLOW}⚠${NC} $*" } error() { echo -e "${RED}āœ—${NC} $*" >&2 } debug() { if [[ "${DEBUG:-}" == "1" ]]; then echo -e "${GRAY}šŸ›${NC} $*" >&2 fi } # Header functions header() { echo echo -e "${BOLD}${BLUE}$*${NC}" echo -e "${BLUE}$(printf '%.0s=' $(seq 1 ${#1}))${NC}" } subheader() { echo echo -e "${BOLD}$*${NC}" echo -e "$(printf '%.0s-' $(seq 1 ${#1}))" } # Box drawing functions box_header() { local text="$1" local width=${2:-50} local padding=$(( (width - ${#text} - 2) / 2 )) echo -e "${BLUE}ā•”$(printf '%.0s═' $(seq 1 $((width-2))))ā•—${NC}" printf "${BLUE}ā•‘${NC}%*s${BOLD}%s${NC}%*s${BLUE}ā•‘${NC}\n" $padding "" "$text" $padding "" echo -e "${BLUE}ā•š$(printf '%.0s═' $(seq 1 $((width-2))))ā•${NC}" } box_message() { local text="$1" local color="${2:-$BLUE}" local width=${3:-50} local padding=$(( (width - ${#text} - 2) / 2 )) echo -e "${color}ā”Œ$(printf '%.0s─' $(seq 1 $((width-2))))┐${NC}" printf "${color}│${NC}%*s%s%*s${color}│${NC}\n" $padding "" "$text" $padding "" echo -e "${color}ā””$(printf '%.0s─' $(seq 1 $((width-2))))ā”˜${NC}" } # Progress indicators spinner() { local pid=$! local delay=0.1 local spinstr='|/-\' while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do local temp=${spinstr#?} printf " [%c] " "$spinstr" local spinstr=$temp${spinstr%"$temp"} sleep $delay printf "\b\b\b\b\b\b" done printf " \b\b\b\b" } # Progress bar progress_bar() { local current=$1 local total=$2 local width=${3:-40} local percent=$((current * 100 / total)) local filled=$((current * width / total)) local empty=$((width - filled)) printf "\r${BLUE}[" printf "%*s" $filled | tr ' ' 'ā–ˆ' printf "%*s" $empty | tr ' ' 'ā–‘' printf "] %d%% (%d/%d)${NC}" $percent $current $total } # Status functions step() { local step_num="$1" local total_steps="$2" local description="$3" echo -e "${CYAN}[${step_num}/${total_steps}]${NC} ${BOLD}${description}${NC}" } status() { local status="$1" shift case "$status" in "ok"|"success"|"done") echo -e "${GREEN}[ OK ]${NC} $*" ;; "fail"|"error"|"failed") echo -e "${RED}[ FAIL ]${NC} $*" ;; "warn"|"warning") echo -e "${YELLOW}[ WARN ]${NC} $*" ;; "info") echo -e "${BLUE}[ INFO ]${NC} $*" ;; "skip"|"skipped") echo -e "${GRAY}[ SKIP ]${NC} $*" ;; *) echo -e "${WHITE}[ ]${NC} $*" ;; esac } # Color detection and graceful degradation supports_color() { # Check if we're in a terminal that supports colors [[ -t 1 ]] && [[ "${TERM:-}" != "dumb" ]] && [[ "${NO_COLOR:-}" != "1" ]] } # Disable colors if not supported if ! supports_color; then # Redefine all color variables as empty RED='' GREEN='' YELLOW='' BLUE='' PURPLE='' CYAN='' WHITE='' GRAY='' BRIGHT_RED='' BRIGHT_GREEN='' BRIGHT_YELLOW='' BRIGHT_BLUE='' BRIGHT_PURPLE='' BRIGHT_CYAN='' BRIGHT_WHITE='' BG_RED='' BG_GREEN='' BG_YELLOW='' BG_BLUE='' BG_PURPLE='' BG_CYAN='' BG_WHITE='' BOLD='' DIM='' ITALIC='' UNDERLINE='' BLINK='' REVERSE='' STRIKETHROUGH='' NC='' RESET='' fi # Usage examples (commented out) # info "This is an info message" # success "Operation completed successfully" # warning "This is a warning" # error "Something went wrong" # header "Main Section" # subheader "Subsection" # box_header "UnitForge Setup" # step 1 5 "Installing dependencies" # status ok "Package installed"