Add colored output support with -c/--color flag

When enabled, colors are applied to:
- Headers: Blue/bold for section titles
- Health status: Green for ✓ (passed), Red for ✗ (failed)
- Temperature: Green (<50°C), Yellow (50-59°C), Red (≥60°C)

Added colorize_health, colorize_temp, and colorize_header helper
functions that respect the USE_COLOR flag.

Fixes: #11

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 11:33:43 -05:00
parent 71a4e3b1fb
commit 2befe710d5

View File

@@ -27,12 +27,14 @@ OPTIONS:
-v, --version Show version information
-d, --debug Enable debug output (show drive mappings)
-s, --skip-smart Skip SMART data collection (faster)
-c, --color Enable colored output
--no-ceph Skip Ceph OSD information
--show-pci Show PCI paths in output
EXAMPLES:
$(basename "$0") # Normal run with all features
$(basename "$0") --skip-smart # Fast run without SMART data
$(basename "$0") --color # Run with colored output
$(basename "$0") --debug # Show mapping debug info
ENVIRONMENT VARIABLES:
@@ -48,6 +50,7 @@ EOF
SKIP_SMART=false
SKIP_CEPH=false
SHOW_PCI=false
USE_COLOR=false
while [[ $# -gt 0 ]]; do
case "$1" in
@@ -75,6 +78,10 @@ while [[ $# -gt 0 ]]; do
SHOW_PCI=true
shift
;;
-c|--color)
USE_COLOR=true
shift
;;
*)
echo "Unknown option: $1" >&2
echo "Use --help for usage information." >&2
@@ -83,6 +90,91 @@ while [[ $# -gt 0 ]]; do
esac
done
#------------------------------------------------------------------------------
# Color Definitions
# ANSI escape codes for terminal colors
#------------------------------------------------------------------------------
if [[ "$USE_COLOR" == true ]]; then
COLOR_RESET='\033[0m'
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_YELLOW='\033[0;33m'
COLOR_BLUE='\033[0;34m'
COLOR_CYAN='\033[0;36m'
COLOR_BOLD='\033[1m'
else
COLOR_RESET=''
COLOR_RED=''
COLOR_GREEN=''
COLOR_YELLOW=''
COLOR_BLUE=''
COLOR_CYAN=''
COLOR_BOLD=''
fi
#------------------------------------------------------------------------------
# colorize_health
#
# Returns health indicator with appropriate color
# Args: $1 - health status (✓ or ✗)
#------------------------------------------------------------------------------
colorize_health() {
local health="$1"
if [[ "$USE_COLOR" == true ]]; then
if [[ "$health" == "✓" ]]; then
echo -e "${COLOR_GREEN}${health}${COLOR_RESET}"
else
echo -e "${COLOR_RED}${health}${COLOR_RESET}"
fi
else
echo "$health"
fi
}
#------------------------------------------------------------------------------
# colorize_temp
#
# Returns temperature with color based on value
# Args: $1 - temperature string (e.g., "45°C")
#------------------------------------------------------------------------------
colorize_temp() {
local temp_str="$1"
local temp_val
if [[ "$USE_COLOR" != true || "$temp_str" == "-" ]]; then
echo "$temp_str"
return
fi
# Extract numeric value
temp_val="${temp_str%°C}"
if [[ "$temp_val" =~ ^[0-9]+$ ]]; then
if [[ "$temp_val" -ge 60 ]]; then
echo -e "${COLOR_RED}${temp_str}${COLOR_RESET}"
elif [[ "$temp_val" -ge 50 ]]; then
echo -e "${COLOR_YELLOW}${temp_str}${COLOR_RESET}"
else
echo -e "${COLOR_GREEN}${temp_str}${COLOR_RESET}"
fi
else
echo "$temp_str"
fi
}
#------------------------------------------------------------------------------
# colorize_header
#
# Returns header text in blue/bold
# Args: $1 - header text
#------------------------------------------------------------------------------
colorize_header() {
if [[ "$USE_COLOR" == true ]]; then
echo -e "${COLOR_BLUE}${COLOR_BOLD}$1${COLOR_RESET}"
else
echo "$1"
fi
}
#------------------------------------------------------------------------------
# Dependency Checks
# Verifies required commands are available before running
@@ -597,7 +689,8 @@ if [[ "$SKIP_CEPH" != true ]]; then
build_ceph_cache
fi
printf "\n=== Drive Details with SMART Status (by Bay Position) ===\n"
printf "\n"
echo -e "$(colorize_header '=== Drive Details with SMART Status (by Bay Position) ===')"
if [[ "$SHOW_PCI" == true ]]; then
printf "%-5s %-15s %-10s %-8s %-8s %-8s %-30s %-20s %-12s %-10s %-10s %-40s\n" "BAY" "DEVICE" "SIZE" "TYPE" "TEMP" "HEALTH" "MODEL" "SERIAL" "CEPH OSD" "STATUS" "USAGE" "PCI PATH"
echo "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
@@ -669,11 +762,15 @@ for bay in $all_bays; do
fi
fi
# Apply colors if enabled
colored_temp="$(colorize_temp "$temp")"
colored_health="$(colorize_health "$health")"
if [[ "$SHOW_PCI" == true ]]; then
pci_path="${BAY_TO_PCI_PATH[$bay]:-}"
printf "%-5s %-15s %-10s %-8s %-8s %-8s %-30s %-20s %-12s %-10s %-10s %-40s\n" "$bay" "/dev/$device" "$size" "$type" "$temp" "$health" "$model" "$serial" "$osd_id" "$ceph_status" "$usage" "$pci_path"
printf "%-5s %-15s %-10s %-8s %-8b %-8b %-30s %-20s %-12s %-10s %-10s %-40s\n" "$bay" "/dev/$device" "$size" "$type" "$colored_temp" "$colored_health" "$model" "$serial" "$osd_id" "$ceph_status" "$usage" "$pci_path"
else
printf "%-5s %-15s %-10s %-8s %-8s %-8s %-30s %-20s %-12s %-10s %-10s\n" "$bay" "/dev/$device" "$size" "$type" "$temp" "$health" "$model" "$serial" "$osd_id" "$ceph_status" "$usage"
printf "%-5s %-15s %-10s %-8s %-8b %-8b %-30s %-20s %-12s %-10s %-10s\n" "$bay" "/dev/$device" "$size" "$type" "$colored_temp" "$colored_health" "$model" "$serial" "$osd_id" "$ceph_status" "$usage"
fi
fi
done
@@ -690,7 +787,8 @@ if [[ -n "$nvme_devices" ]]; then
done <<< "$nvme_devices"
if [[ -n "$unmapped_nvme" ]]; then
printf "\n=== Unmapped NVMe Drives ===\n"
printf "\n"
echo -e "$(colorize_header '=== Unmapped NVMe Drives ===')"
printf "%-15s %-10s %-10s %-40s %-25s\n" "DEVICE" "SIZE" "TYPE" "MODEL" "SERIAL"
echo "------------------------------------------------------------------------------------------------------"
echo "$unmapped_nvme" | while read -r name size; do
@@ -714,7 +812,8 @@ fi
# Ceph RBD Devices
rbd_devices=$(lsblk -d -n -o NAME,SIZE,TYPE 2>/dev/null | grep "rbd" | sort -V)
if [ -n "$rbd_devices" ]; then
echo -e "\n=== Ceph RBD Devices ==="
printf "\n"
echo -e "$(colorize_header '=== Ceph RBD Devices ===')"
printf "%-15s %-10s %-10s %-30s\n" "DEVICE" "SIZE" "TYPE" "MOUNTPOINT"
echo "------------------------------------------------------------"
echo "$rbd_devices" | while read -r name size type; do
@@ -727,7 +826,8 @@ fi
# Show mapping diagnostic info if DEBUG is set
if [[ -n "$DEBUG" ]]; then
echo -e "\n=== DEBUG: Drive Mappings ==="
printf "\n"
echo -e "$(colorize_header '=== DEBUG: Drive Mappings ===')"
for key in "${!DRIVE_MAP[@]}"; do
echo "Bay $key: ${DRIVE_MAP[$key]}"
done | sort -n