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 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
readonly PLEX_SERVICE="plexmediaserver"
SCRIPT_NAME="$(basename "$0")"
@@ -76,19 +86,32 @@ readonly ROCKET="▶"
readonly STOP_SIGN="■"
readonly RECYCLE="↻"
readonly INFO=""
readonly HOURGLASS="⌛"
readonly SPARKLES="✦"
# 📊 Function to print fancy headers
print_header() {
echo -e "\n${PURPLE}${BOLD}+==============================================================+${RESET}"
echo -e "${PURPLE}${BOLD}| ${SPARKLES} PLEX MEDIA SERVER ${SPARKLES} |${RESET}"
echo -e "${PURPLE}${BOLD}+==============================================================+${RESET}\n"
if use_colors; then
echo -e "\n${PURPLE}${BOLD}+==============================================================+${RESET}"
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
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
@@ -96,7 +119,12 @@ print_status() {
local status="$1"
local message="$2"
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
@@ -106,13 +134,23 @@ show_loading() {
local spin='-\|/'
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
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
done
printf "\r%s%s %s %s%s\n" "${CYAN}" "${HOURGLASS}" "${message}" "${CHECKMARK}" "${RESET}"
echo -e "\r⌛ ${message}"
}
# 🚀 Enhanced start function
@@ -181,13 +219,24 @@ show_detailed_status() {
local service_status
service_status=$(systemctl is-active "$PLEX_SERVICE" 2>/dev/null || echo "inactive")
echo -e "\n${BOLD}${BLUE}+==============================================================+${RESET}"
echo -e "${BOLD}${BLUE}| SERVICE STATUS |${RESET}"
echo -e "${BOLD}${BLUE}+==============================================================+${RESET}"
if use_colors; then
echo -e "\n${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
"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
local uptime
@@ -201,61 +250,121 @@ show_detailed_status() {
memory_usage="Unknown"
fi
echo -e "${DIM}${CYAN} Started: ${WHITE}${uptime}${RESET}"
echo -e "${DIM}${CYAN} Memory Usage: ${WHITE}${memory_usage}${RESET}"
echo -e "${DIM}${CYAN} Service Name: ${WHITE}${PLEX_SERVICE}${RESET}"
if use_colors; then
echo -e "${DIM}${CYAN} Started: ${WHITE}${uptime}${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")
print_status "${CROSS}" "Service Status: ${RED}${BOLD}INACTIVE${RESET}" "${RED}"
echo -e "${DIM}${YELLOW} Use '${SCRIPT_NAME} start' to start the service${RESET}"
if use_colors; then
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")
print_status "${CROSS}" "Service Status: ${RED}${BOLD}FAILED${RESET}" "${RED}"
echo -e "${DIM}${RED} Check logs with: ${WHITE}journalctl -u ${PLEX_SERVICE}${RESET}"
if use_colors; then
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
# 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
local logs
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
echo -e "${DIM}${logs}${RESET}"
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
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
else
# 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")
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
echo -e "${DIM}${logs}${RESET}"
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
fi
fi
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
if use_colors; then
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
else
echo "+----------------------------------+"
fi
}
# 🔧 Show available commands
show_help() {
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
echo ""
echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server"
echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server"
echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server"
echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status"
echo -e " ${PURPLE}${BOLD}help${RESET} ${SPARKLES} Show this help message"
echo ""
echo -e "${DIM}${WHITE}Examples:${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} status # Show current status${RESET}"
if use_colors; then
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
echo ""
echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server"
echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server"
echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server"
echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status"
echo -e " ${PURPLE}${BOLD}help${RESET} ${SPARKLES} Show this help message"
echo ""
echo -e "${DIM}${WHITE}Examples:${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 ""
}

View File

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