dotfiles/.i3blocks/mpd

141 lines
3 KiB
Text
Raw Normal View History

2019-06-13 20:12:29 +02:00
#!/bin/bash
2020-01-09 21:17:01 +01:00
runtime="${XDG_RUNTIME_DIR}/i3blocks"
[[ ! -d "$runtime" ]] && mkdir -p "$runtime"
2019-09-22 03:31:29 +02:00
if ! systemctl --user is-active mpd &>/dev/null
then
origstatus='off'
else
origstatus="$(mpc status | sed -n '2p' | grep -Eo '^\[.*\]' | tr -d '[]')"
[[ -z "$origstatus" ]] && origstatus='stop'
fi
2021-06-05 22:49:18 +02:00
LONG=15
2020-01-09 21:17:01 +01:00
SHORT=15
2019-06-14 09:42:47 +02:00
2019-09-22 03:31:29 +02:00
case $BLOCK_BUTTON in
1) mpc -q prev ;;
2) mpc -q toggle ;;
3) mpc -q next ;;
2021-01-26 19:45:45 +01:00
#4) mpc -q volume +2 ;;
#5) mpc -q volume -2 ;;
2019-09-22 03:31:29 +02:00
#?*) notify-send "Test" "$BLOCK_BUTTON" ;;
esac
2019-06-14 09:42:47 +02:00
human_time() {
local seconds="$1"
if [[ "$seconds" -lt 0 ]]
then
echo "--:--"
return
fi
local minutes=$(( $seconds / 60 ))
seconds=$(( $seconds % 60 ))
if [[ "$minutes" -ge 90 ]]
then
printf "%5s\n" "${minutes}m"
else
printf "%02d:%02d\n" "$minutes" "$seconds"
fi
return
}
2019-06-13 20:12:29 +02:00
status="<span foreground=\"#6699CC\">"
case $origstatus in
stop ) status="<span foreground=\"#F2777A\">" ;;
paused ) status="<span foreground=\"#FFCC66\">" ;;
playing ) status="<span foreground=\"#99CC99\">" ;;
off ) status="<span foreground=\"#515151\">" ;;
esac
if [[ "$origstatus" != "stop" ]] && [[ "$origstatus" != "off" ]]
then
2020-01-09 21:17:01 +01:00
if [[ ! -f $runtime/mpd ]]
2019-06-13 20:12:29 +02:00
then
2021-04-05 17:09:57 +02:00
mpc -f '[[%artist%§|%name%§]%title%|[%file%]' current > ${runtime}/mpd
2019-06-13 20:12:29 +02:00
fi
2020-01-09 21:17:01 +01:00
song=$(<${runtime}/mpd)
2019-06-13 20:12:29 +02:00
2019-06-14 09:42:47 +02:00
if [[ "${#song}" -gt $LONG ]]
then
scrolling=long
elif [[ "${#song}" -gt $SHORT ]]
2019-06-13 20:12:29 +02:00
then
2019-06-14 09:42:47 +02:00
scrolling=short
2019-06-13 20:12:29 +02:00
else
2019-06-14 09:42:47 +02:00
scrolling=none
2019-06-13 20:12:29 +02:00
fi
2019-06-14 09:42:47 +02:00
if [[ "$scrolling" != "none" ]]
2019-06-13 20:12:29 +02:00
then
2020-01-09 21:17:01 +01:00
scroll="$(<${runtime}/mpd-scroll)"
2019-06-14 09:42:47 +02:00
if [[ -z "${scroll// /}" ]]
then
2020-01-09 21:17:01 +01:00
printf "%-${LONG}s" "$(<${runtime}/mpd)" > ${runtime}/mpd-scroll
song="$(<${runtime}/mpd-scroll)"
2019-06-14 09:42:47 +02:00
else
2020-01-09 21:17:01 +01:00
song="$(<${runtime}/mpd-scroll)"
printf "%-${LONG}s" "${song:1}" > ${runtime}/mpd-scroll
2019-06-14 09:42:47 +02:00
fi
2019-06-13 20:12:29 +02:00
fi
2019-06-14 09:42:47 +02:00
2019-09-09 20:18:01 +02:00
time="$(echo -e 'status\nclose' | nc -U $MPD_HOST | grep time \
2019-06-14 09:42:47 +02:00
| cut -d' ' -f2)"
2019-06-13 20:12:29 +02:00
2019-06-14 09:42:47 +02:00
time_elapsed=$(human_time "${time%:*}")
time_total=$(human_time "${time#*:}")
time_left=$(human_time "$(( ${time#*:} - ${time%:*} ))")
longtime="[$time_elapsed $time_left ${time_total/00:00/--:--}]"
shorttime="$time_elapsed $time_left"
case $scrolling in
long)
2021-06-05 22:49:18 +02:00
longsong="${song}"
shortsong="${song}"
2019-06-14 09:42:47 +02:00
;;
short)
2021-06-05 22:49:18 +02:00
longsong="$(<${runtime}/mpd)"
shortsong="${song}"
2019-06-14 09:42:47 +02:00
;;
none)
2021-06-05 22:49:18 +02:00
longsong="$(<${runtime}/mpd)"
2019-06-14 09:42:47 +02:00
shortsong="$longsong"
;;
esac
longsong="${longsong:0:$LONG}"
shortsong="${shortsong:0:$SHORT}"
longsong="${longsong//&/&amp;}"
shortsong="${shortsong//&/&amp;}"
if [[ "$longsong" =~ § ]]
then
2020-04-18 01:44:01 +02:00
longsong="<b>${longsong/§/</b> }"
2019-06-14 09:42:47 +02:00
elif [[ "$song" =~ § ]]
2019-06-13 20:12:29 +02:00
then
2020-04-18 01:44:01 +02:00
longsong="<b>${longsong}</b>"
2019-06-13 20:12:29 +02:00
fi
2019-06-14 09:42:47 +02:00
if [[ "$shortsong" =~ § ]]
then
2020-04-18 01:44:01 +02:00
shortsong="<b>${shortsong/§/</b> }"
2019-06-14 09:42:47 +02:00
elif [[ "$song" =~ § ]]
2019-06-13 20:12:29 +02:00
then
2020-04-18 01:44:01 +02:00
shortsong="<b>${shortsong}</b>"
2019-06-13 20:12:29 +02:00
fi
2019-06-14 09:42:47 +02:00
echo "${longsong} $status$longtime</span>"
echo "${shortsong} $status$shorttime</span>"
2019-06-13 20:12:29 +02:00
else
2020-01-09 21:17:01 +01:00
[[ -f ${runtime}/mpd ]] && rm ${runtime}/mpd
[[ -f ${runtime}/mpd-scroll ]] && rm ${runtime}/mpd-scroll
2019-06-14 09:42:47 +02:00
printf "%${LONG}s $status[--:-- --:-- --:--]</span>\n" " "
printf "%${SHORT}s $status--:-- --:--</span>\n" " "
2019-06-13 20:12:29 +02:00
fi