Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bonelifer/0cf841e3fb5b8c3c4a2c24eefca612c1 to your computer and use it in GitHub Desktop.
Save bonelifer/0cf841e3fb5b8c3c4a2c24eefca612c1 to your computer and use it in GitHub Desktop.
mpc-control - full function
#!/usr/bin/env bash
# Icon Definitions (Nerd Font Icons)
# Define custom icons for different playback states (play, pause, stop, etc.)
PLAY_ICON="󰐊"
PAUSE_ICON="󰏤"
STOP_ICON="󰓛"
NEXT_ICON="󰒭"
PREV_ICON="󰒮"
VOLUME_UP_ICON="󰝝"
VOLUME_DOWN_ICON="󰝞"
VOLUME_MUTE_ICON="󰝟"
MUSIC_ICON="󱍙"
ERROR_ICON="󰀩"
# Notification Timeout (in milliseconds)
# Set default notification timeouts for different types of notifications
NOTIFY_TIMEOUT_NORMAL=6000 # Normal notifications
NOTIFY_TIMEOUT_SHORT=1500 # Volume change notifications
NOTIFY_TIMEOUT_LONG=5000 # Error notifications
# Color Definitions for Output
# Define terminal colors for output formatting
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if required commands are available
# Ensure that 'mpc' (Music Player Command) and 'notify-send' (for notifications) are installed
command -v mpc >/dev/null 2>&1 || { echo -e "${RED}mpc is not installed!${NC}"; exit 1; }
command -v notify-send >/dev/null 2>&1 || { echo -e "${RED}notify-send is not installed!${NC}"; exit 1; }
# Format song information (removes .mp3 extension)
# This function retrieves the current song information and removes the '.mp3' extension
format() {
local info=$(mpc status -f "[[%artist% - ]%title%]|[%file%]" | head -n1)
# Remove .mp3 extension if present in filename
echo "${info%.mp3}"
}
# Create a progress bar showing current time and total time
# This function calculates the current playback time, total time, and progress percentage
create_progress_bar() {
local current_time=$(mpc status | awk 'NR==2 {print $3}' | cut -d'/' -f1)
local total_time=$(mpc status | awk 'NR==2 {print $3}' | cut -d'/' -f2)
local percentage=$(mpc status | awk 'NR==2 {print $4}' | tr -d '()%')
echo "$current_time / $total_time ($percentage%)"
}
# Send a notification with the given parameters
# This function sends notifications using 'notify-send' with custom title, message, icon, and urgency level
send_notification() {
local title="$1"
local message="$2"
local icon="$3"
local timeout="$4"
local urgency="$5"
notify-send -t "${timeout:-$NOTIFY_TIMEOUT_NORMAL}" \
-h string:x-canonical-private-synchronous:mpd \
-u "${urgency:-normal}" \
"$title" \
"$message" \
-i "$icon"
}
# Display the current status (playing, paused, or stopped)
# This function shows the current playback status and sends a notification accordingly
status() {
local state=$(mpc status | sed -n 2p | awk '{print $1}' | tr -d '[]')
local current_song="$(format)"
local progress="$(create_progress_bar)"
case $state in
"playing")
echo -e "${GREEN}$PLAY_ICON${NC} $current_song"
send_notification \
"$PLAY_ICON Now Playing" \
"$current_song\n$progress" \
"media-playback-start" \
"$NOTIFY_TIMEOUT_NORMAL"
;;
"paused")
echo -e "${BLUE}$PAUSE_ICON${NC} $current_song"
send_notification \
"$PAUSE_ICON Paused" \
"$current_song\n$progress" \
"media-playback-pause" \
"$NOTIFY_TIMEOUT_NORMAL"
;;
*)
echo -e "${RED}$STOP_ICON${NC} Stopped"
send_notification \
"$STOP_ICON Stopped" \
"MPD has been stopped" \
"media-playback-stop" \
"$NOTIFY_TIMEOUT_SHORT"
;;
esac
}
# Volume control: adjust volume and display appropriate icon
# This function adjusts the volume and assigns an icon based on the volume level
volume() {
local vol=$(mpc volume | cut -d':' -f2 | tr -d ' %')
local vol_icon
# Define volume levels based on the volume percentage
if [ "$vol" -ge 80 ]; then
vol_icon="󰕾" # High volume icon
elif [ "$vol" -ge 50 ]; then
vol_icon="󰖀" # Medium volume icon
elif [ "$vol" -ge 30 ]; then
vol_icon="󰕿" # Low volume icon
else
vol_icon="󰖁" # Mute icon
fi
# Output the current volume percentage
echo "$vol%"
return "$vol"
}
# Handle next and previous song actions to reduce code repetition
# This function handles actions for skipping to the next or previous song
change_song() {
local direction="$1"
mpc "$direction" >/dev/null
song_info="$(format)"
progress="$(create_progress_bar)"
# Set appropriate icon and action based on the direction
if [ "$direction" == "next" ]; then
icon="$NEXT_ICON"
action="Next Track"
notification_icon="media-skip-forward"
else
icon="$PREV_ICON"
action="Previous Track"
notification_icon="media-skip-backward"
fi
# Output song information and send notification
echo -e "$icon $song_info"
send_notification \
"$icon $action" \
"$song_info\n$progress" \
"$notification_icon" \
"$NOTIFY_TIMEOUT_NORMAL"
}
# Display usage instructions for the script
# This function displays a help message on how to use the script with available commands
usage() {
echo "Usage: $(basename "$0") [COMMAND]"
echo
echo "Commands:"
echo " toggle - Toggle play/pause"
echo " play - Play"
echo " pause - Pause"
echo " stop - Stop playback"
echo " next - Next track"
echo " prev - Previous track"
echo " status - Show current status"
echo " vol up - Increase volume"
echo " vol down - Decrease volume"
echo " help - Show this message"
exit 1
}
# Main function to handle commands
# This section processes the provided command-line argument and takes appropriate action
case "$1" in
"toggle")
mpc toggle >/dev/null
status
;;
"play")
mpc play >/dev/null
status
;;
"pause")
mpc pause >/dev/null
status
;;
"stop")
mpc stop >/dev/null
status
;;
"next")
change_song "next"
;;
"prev")
change_song "prev"
;;
"status")
status
;;
"vol")
case "$2" in
"up")
mpc volume +5 >/dev/null
vol=$(volume)
send_notification \
"$VOLUME_UP_ICON Volume Increased" \
"Volume Level: $vol" \
"audio-volume-high" \
"$NOTIFY_TIMEOUT_SHORT"
echo "$vol"
;;
"down")
mpc volume -5 >/dev/null
vol=$(volume)
send_notification \
"$VOLUME_DOWN_ICON Volume Decreased" \
"Volume Level: $vol" \
"audio-volume-low" \
"$NOTIFY_TIMEOUT_SHORT"
echo "$vol"
;;
*)
volume
;;
esac
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment