#!/bin/bash truncate_string() { local input="$1" local max_length="$2" if [ "${#input}" -gt "$max_length" ]; then echo "${input:0:$((max_length - 2))}.." else echo "$input" fi } filter() { if ps -C mpd >/dev/null 2>&1; then screen_width=$(xrandr | awk '/\*/ {split($1, res, "x"); print res[1]; exit}') artist=$(mpc current -f %artist%) title=$(mpc current -f %title%) if [ "$screen_width" -le 2048 ]; then max_length=$((screen_width / 100)) artist=$(truncate_string "$artist" "$max_length") title=$(truncate_string "$title" "$max_length") else artist="$(mpc current -f %artist%)" title="$(mpc current -f %title%)" fi case "$(mpc status %state%)" in "playing") prefix="🎵" ;; "paused") prefix="⏸" ;; *) return ;; esac indicators="" [ "$(mpc status %single%)" = "on" ] && indicators="${indicators}🔀" [ "$(mpc status %random%)" = "on" ] && indicators="${indicators}🔂" [ "$(mpc status %repeat%)" = "on" ] && indicators="${indicators}🔁" case "$(awk -F',' -v name="${0##*/}" '$0 ~ name {gsub(/[[:space:]]/, "", $3); print $3}' "${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless/dwmblocks/config.def.h")" in 0) echo "$prefix$artist - $title${indicators:+$indicators}" ;; [1-9]*) current_time=$(mpc status %currenttime%) total_time=$(mpc status %totaltime%) echo "$prefix$artist - $title ${current_time:+$current_time/}$total_time${indicators:+$indicators}" ;; *) return ;; esac fi } [ "$(awk -F',' -v name="${0##*/}" '$0 ~ name {print $3; exit}' "${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless/dwmblocks/config.def.h")" -eq 0 ] && { pidof -x sb-mpdup >/dev/null 2>&1 || sb-mpdup >/dev/null 2>&1 & } # Handling interaction based on button press case $BLOCK_BUTTON in 1) # left click, opens ncmpcpp mpc status | filter setsid -f "$TERMINAL" -e ncmpcpp ;; 2) mpc toggle | filter ;; # middle click, pause/unpause 3) # right click notify-send "🎵 Music module" "\- Shows mpd song playing and status - 🎵 if playing - ⏸ if paused - 🔂 if single on - 🔁 if repeat on - 🔀 if random on - Left click opens ncmpcpp - Middle click pauses/unpause - Scroll changes track" { [ -n "$(mpc current)" ] && [ -n "$(mpc queued)" ]; } && notify-send "🎵 $(mpc current)" "⏭️ $(mpc queued)" ;; 4) mpc prev | filter ;; # scroll up, previous 5) mpc next | filter ;; # scroll down, next 6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; *) mpc status | filter ;; # default, show current status esac