#!/bin/bash truncate_string() { input="$1" max_length="$2" if [ "${#input}" -gt "$max_length" ]; then echo "${input:0:$((max_length - 2))}.." else echo "$input" fi } signal="$(awk -F', *' -v name="${0##*/}" '$2 ~ name {print $3+0}' "${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless/dwmblocks/config.def.h")" [ "$signal" -eq 0 ] && { pidof -x sb-mpdup >/dev/null 2>&1 || sb-mpdup >/dev/null 2>&1 & } status() { 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 length="$(grep 'MAX_BLOCK_OUTPUT_LENGTH' ~/.local/src/suckless/dwmblocks/config.def.h | awk '{print $3}')" max_length="${length:+$((length / 3))}" max_length="${max_length:-15}" artist=$(truncate_string "$artist" "$max_length") title=$(truncate_string "$title" "$max_length") 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 "$signal" 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 } mpc status %state% | grep -q "stopped" || status case $BLOCK_BUTTON in 1) setsid -f "$TERMINAL" -e ncmpcpp ;; # left click, opens ncmpcpp 2) mpc toggle ;; # middle click, pause/unpause 3) # right click { [ -n "$(mpc current)" ] && [ -n "$(mpc queued)" ]; } && notify-send "🎵 $(mpc current)" "⏭️ $(mpc queued)" 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" ;; 4) mpc prev ;; # scroll up, previous 5) mpc next ;; # scroll down, next 6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; esac