feat: Add color support toggle for improved output in plex.sh and update.sh

This commit is contained in:
Peter Wood
2025-06-08 20:47:23 -04:00
parent 6b90447b30
commit 16dd496c31
2 changed files with 150 additions and 41 deletions

View File

@@ -64,6 +64,16 @@ readonly BOLD='\033[1m'
readonly DIM='\033[2m' readonly DIM='\033[2m'
readonly RESET='\033[0m' readonly RESET='\033[0m'
# 🌈 Function to check if colors should be used
use_colors() {
# Check if stdout is a terminal and colors are supported
if [[ -t 1 ]] && [[ "${TERM:-}" != "dumb" ]] && [[ "${NO_COLOR:-}" != "1" ]]; then
return 0
else
return 1
fi
}
# 🔧 Configuration # 🔧 Configuration
readonly PLEX_SERVICE="plexmediaserver" readonly PLEX_SERVICE="plexmediaserver"
SCRIPT_NAME="$(basename "$0")" SCRIPT_NAME="$(basename "$0")"
@@ -76,19 +86,32 @@ readonly ROCKET="▶"
readonly STOP_SIGN="■" readonly STOP_SIGN="■"
readonly RECYCLE="↻" readonly RECYCLE="↻"
readonly INFO="" readonly INFO=""
readonly HOURGLASS="⌛"
readonly SPARKLES="✦" readonly SPARKLES="✦"
# 📊 Function to print fancy headers # 📊 Function to print fancy headers
print_header() { print_header() {
echo -e "\n${PURPLE}${BOLD}+==============================================================+${RESET}" if use_colors; then
echo -e "${PURPLE}${BOLD}| ${SPARKLES} PLEX MEDIA SERVER ${SPARKLES} |${RESET}" echo -e "\n${PURPLE}${BOLD}+==============================================================+${RESET}"
echo -e "${PURPLE}${BOLD}+==============================================================+${RESET}\n" echo -e "${PURPLE}${BOLD}| ${SPARKLES} PLEX MEDIA SERVER ${SPARKLES} |${RESET}"
echo -e "${PURPLE}${BOLD}+==============================================================+${RESET}\n"
else
echo ""
echo "+=============================================================="
echo "| ${SPARKLES} PLEX MEDIA SERVER ${SPARKLES} |"
echo "+=============================================================="
echo ""
fi
} }
# 🎉 Function to print completion footer # 🎉 Function to print completion footer
print_footer() { print_footer() {
echo -e "\n${DIM}${CYAN}\\--- Operation completed ${SPARKLES} ---/${RESET}\n" if use_colors; then
echo -e "\n${DIM}${CYAN}\\--- Operation completed ${SPARKLES} ---/${RESET}\n"
else
echo ""
echo "\\--- Operation completed ${SPARKLES} ---/"
echo ""
fi
} }
# 🎯 Function to print status with style # 🎯 Function to print status with style
@@ -96,7 +119,12 @@ print_status() {
local status="$1" local status="$1"
local message="$2" local message="$2"
local color="$3" local color="$3"
echo -e "${color}${BOLD}[${status}]${RESET} ${message}"
if use_colors; then
echo -e "${color}${BOLD}[${status}]${RESET} ${message}"
else
echo "[${status}] ${message}"
fi
} }
# ⏱️ Function to show loading animation # ⏱️ Function to show loading animation
@@ -106,13 +134,23 @@ show_loading() {
local spin='-\|/' local spin='-\|/'
local i=0 local i=0
echo -ne "${CYAN}${HOURGLASS} ${message}${RESET}" # For non-interactive terminals or when called from other scripts,
# use a simpler approach
if ! use_colors || [[ "${PLEX_SIMPLE_OUTPUT:-}" == "1" ]]; then
echo "${message}..."
wait "$pid"
echo "${message}"
return
fi
# Full interactive mode with colors
echo -n "${message}"
while kill -0 "$pid" 2>/dev/null; do while kill -0 "$pid" 2>/dev/null; do
i=$(( (i+1) %4 )) i=$(( (i+1) %4 ))
printf "\r%s%s %s %s%s" "${CYAN}" "${HOURGLASS}" "${message}" "${spin:$i:1}" "${RESET}" echo -ne "\r⌛ ${message} ${spin:$i:1}"
sleep 0.1 sleep 0.1
done done
printf "\r%s%s %s %s%s\n" "${CYAN}" "${HOURGLASS}" "${message}" "${CHECKMARK}" "${RESET}" echo -e "\r⌛ ${message}"
} }
# 🚀 Enhanced start function # 🚀 Enhanced start function
@@ -181,13 +219,24 @@ show_detailed_status() {
local service_status local service_status
service_status=$(systemctl is-active "$PLEX_SERVICE" 2>/dev/null || echo "inactive") service_status=$(systemctl is-active "$PLEX_SERVICE" 2>/dev/null || echo "inactive")
echo -e "\n${BOLD}${BLUE}+==============================================================+${RESET}" if use_colors; then
echo -e "${BOLD}${BLUE}| SERVICE STATUS |${RESET}" echo -e "\n${BOLD}${BLUE}+==============================================================+${RESET}"
echo -e "${BOLD}${BLUE}+==============================================================+${RESET}" echo -e "${BOLD}${BLUE}| SERVICE STATUS |${RESET}"
echo -e "${BOLD}${BLUE}+==============================================================+${RESET}"
else
echo ""
echo "+=============================================================="
echo "| SERVICE STATUS |"
echo "+=============================================================="
fi
case "$service_status" in case "$service_status" in
"active") "active")
print_status "${CHECKMARK}" "Service Status: ${GREEN}${BOLD}ACTIVE${RESET}" "${GREEN}" if use_colors; then
print_status "${CHECKMARK}" "Service Status: ${GREEN}${BOLD}ACTIVE${RESET}" "${GREEN}"
else
print_status "${CHECKMARK}" "Service Status: ACTIVE" ""
fi
# Get additional info # Get additional info
local uptime local uptime
@@ -201,61 +250,121 @@ show_detailed_status() {
memory_usage="Unknown" memory_usage="Unknown"
fi fi
echo -e "${DIM}${CYAN} Started: ${WHITE}${uptime}${RESET}" if use_colors; then
echo -e "${DIM}${CYAN} Memory Usage: ${WHITE}${memory_usage}${RESET}" echo -e "${DIM}${CYAN} Started: ${WHITE}${uptime}${RESET}"
echo -e "${DIM}${CYAN} Service Name: ${WHITE}${PLEX_SERVICE}${RESET}" echo -e "${DIM}${CYAN} Memory Usage: ${WHITE}${memory_usage}${RESET}"
echo -e "${DIM}${CYAN} Service Name: ${WHITE}${PLEX_SERVICE}${RESET}"
else
echo " Started: ${uptime}"
echo " Memory Usage: ${memory_usage}"
echo " Service Name: ${PLEX_SERVICE}"
fi
;; ;;
"inactive") "inactive")
print_status "${CROSS}" "Service Status: ${RED}${BOLD}INACTIVE${RESET}" "${RED}" if use_colors; then
echo -e "${DIM}${YELLOW} Use '${SCRIPT_NAME} start' to start the service${RESET}" print_status "${CROSS}" "Service Status: ${RED}${BOLD}INACTIVE${RESET}" "${RED}"
echo -e "${DIM}${YELLOW} Use '${SCRIPT_NAME} start' to start the service${RESET}"
else
print_status "${CROSS}" "Service Status: INACTIVE" ""
echo " Use '${SCRIPT_NAME} start' to start the service"
fi
;; ;;
"failed") "failed")
print_status "${CROSS}" "Service Status: ${RED}${BOLD}FAILED${RESET}" "${RED}" if use_colors; then
echo -e "${DIM}${RED} Check logs with: ${WHITE}journalctl -u ${PLEX_SERVICE}${RESET}" print_status "${CROSS}" "Service Status: ${RED}${BOLD}FAILED${RESET}" "${RED}"
echo -e "${DIM}${RED} Check logs with: ${WHITE}journalctl -u ${PLEX_SERVICE}${RESET}"
else
print_status "${CROSS}" "Service Status: FAILED" ""
echo " Check logs with: journalctl -u ${PLEX_SERVICE}"
fi
;; ;;
*) *)
print_status "${INFO}" "Service Status: ${YELLOW}${BOLD}${service_status^^}${RESET}" "${YELLOW}" if use_colors; then
print_status "${INFO}" "Service Status: ${YELLOW}${BOLD}${service_status^^}${RESET}" "${YELLOW}"
else
print_status "${INFO}" "Service Status: ${service_status^^}" ""
fi
;; ;;
esac esac
# Show recent logs # Show recent logs
echo -e "\n${DIM}${CYAN}+--- Recent Service Logs (24h) ---+${RESET}" if use_colors; then
echo -e "\n${DIM}${CYAN}+--- Recent Service Logs (24h) ---+${RESET}"
else
echo ""
echo "+--- Recent Service Logs (24h) ---+"
fi
# Try to get logs with sudo, fall back to user permissions # Try to get logs with sudo, fall back to user permissions
local logs local logs
if logs=$(sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" --output=short 2>/dev/null); then if logs=$(sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" --output=short 2>/dev/null); then
if [[ -n "$logs" && "$logs" != "-- No entries --" ]]; then if [[ -n "$logs" && "$logs" != "-- No entries --" ]]; then
echo -e "${DIM}${logs}${RESET}" if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
else else
echo -e "${DIM}${YELLOW}No recent log entries found${RESET}" if use_colors; then
echo -e "${DIM}${YELLOW}No recent log entries found${RESET}"
else
echo "No recent log entries found"
fi
fi fi
else else
# Fallback: try without sudo # Fallback: try without sudo
logs=$(journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" 2>/dev/null || echo "Unable to access logs") logs=$(journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" 2>/dev/null || echo "Unable to access logs")
if [[ "$logs" == "Unable to access logs" || "$logs" == "-- No entries --" ]]; then if [[ "$logs" == "Unable to access logs" || "$logs" == "-- No entries --" ]]; then
echo -e "${DIM}${YELLOW}Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})${RESET}" if use_colors; then
echo -e "${DIM}${YELLOW}Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})${RESET}"
else
echo "Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})"
fi
else else
echo -e "${DIM}${logs}${RESET}" if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
fi fi
fi fi
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}" if use_colors; then
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
else
echo "+----------------------------------+"
fi
} }
# 🔧 Show available commands # 🔧 Show available commands
show_help() { show_help() {
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}" if use_colors; then
echo "" echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
echo -e "${BOLD}${WHITE}Available Commands:${RESET}" echo ""
echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server" echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server" echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server"
echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server" echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server"
echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status" echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server"
echo -e " ${PURPLE}${BOLD}help${RESET} ${SPARKLES} Show this help message" echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status"
echo "" echo -e " ${PURPLE}${BOLD}help${RESET} ${SPARKLES} Show this help message"
echo -e "${DIM}${WHITE}Examples:${RESET}" echo ""
echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}" echo -e "${DIM}${WHITE}Examples:${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} status # Show current status${RESET}" echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} status # Show current status${RESET}"
else
echo "Usage: ${SCRIPT_NAME} <command>"
echo ""
echo "Available Commands:"
echo " start ${ROCKET} Start Plex Media Server"
echo " stop ${STOP_SIGN} Stop Plex Media Server"
echo " restart ${RECYCLE} Restart Plex Media Server"
echo " status ${INFO} Show detailed service status"
echo " help ${SPARKLES} Show this help message"
echo ""
echo "Examples:"
echo " ${SCRIPT_NAME} start # Start the Plex service"
echo " ${SCRIPT_NAME} status # Show current status"
fi
echo "" echo ""
} }

View File

@@ -452,7 +452,7 @@ manage_plex_service() {
if systemctl is-active --quiet plexmediaserver.service 2>/dev/null; then if systemctl is-active --quiet plexmediaserver.service 2>/dev/null; then
log_message "INFO" "Stopping Plex Media Server for system update" log_message "INFO" "Stopping Plex Media Server for system update"
if [[ -x "$plex_script" ]]; then if [[ -x "$plex_script" ]]; then
sudo "$plex_script" stop PLEX_SIMPLE_OUTPUT=1 "$plex_script" stop
else else
sudo systemctl stop plexmediaserver.service sudo systemctl stop plexmediaserver.service
fi fi
@@ -465,7 +465,7 @@ manage_plex_service() {
if systemctl is-enabled --quiet plexmediaserver.service 2>/dev/null; then if systemctl is-enabled --quiet plexmediaserver.service 2>/dev/null; then
log_message "INFO" "Starting Plex Media Server after system update" log_message "INFO" "Starting Plex Media Server after system update"
if [[ -x "$plex_script" ]]; then if [[ -x "$plex_script" ]]; then
sudo "$plex_script" start PLEX_SIMPLE_OUTPUT=1 "$plex_script" start
else else
sudo systemctl start plexmediaserver.service sudo systemctl start plexmediaserver.service
fi fi