fixes MPD

This commit is contained in:
Breizh 2019-06-14 09:42:47 +02:00
parent e9773bfa93
commit 99b61aee79
6 changed files with 117 additions and 64 deletions
.i3blocks

View file

@ -1,5 +1,26 @@
#!/bin/bash
LONG=30
SHORT=15
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
}
if ! systemctl --user is-active mpd &>/dev/null
then
origstatus='off'
@ -13,8 +34,6 @@ case $BLOCK_BUTTON in
1) mpc -q prev ;;
2) mpc -q toggle ;;
3) mpc -q next ;;
4) mpc -q seek +5 ;;
5) mpc -q seek -5 ;;
esac
status="<span foreground=\"#6699CC\">"
@ -29,53 +48,87 @@ if [[ "$origstatus" != "stop" ]] && [[ "$origstatus" != "off" ]]
then
if [[ ! -f /dev/shm/mpd ]]
then
mpc -f '[[%artist%, ]%title%|[%file%]' current > /dev/shm/mpd
mpc -f '[[%artist%§]%title%|[%file%]' current > /dev/shm/mpd
fi
song=$(</dev/shm/mpd)
song="${song//&/and}"
scroll="$(</dev/shm/mpd-scroll)"
if [[ -z "${scroll// /}" ]]
if [[ "${#song}" -gt $LONG ]]
then
cp /dev/shm/mpd /dev/shm/mpd-scroll
scrolling=long
elif [[ "${#song}" -gt $SHORT ]]
then
scrolling=short
else
song="$(</dev/shm/mpd-scroll)"
echo "${song:1} " > /dev/shm/mpd-scroll
scrolling=none
fi
alltime="$(mpc status | column -t -o ' ' | sed -n '2p' | cut -f3)"
elapsed_time="$(dateconv -f "%0H:%0M" "${alltime%/*}" || echo "--:--")"
total_time="$(dateconv -f "%0H:%0M" "${alltime#*/}" || echo "--:--")"
left_time="$(datediff -f "%0H:%0M" "$elapsed_time" "$total_time" || echo "--:--")"
time="[$elapsed_time $left_time $total_time]"
shorttime="$elapsed_time $left_time"
if [[ "$time" = "[ ]" ]] || [[ -z "$time" ]]
if [[ "$scrolling" != "none" ]]
then
time="[--:-- --:-- --:--]"
fi
scroll="$(</dev/shm/mpd-scroll)"
if [[ -z "$shorttime" ]] || [[ "$shorttime" = " " ]]
then
shorttime="--:-- --:--"
fi
if [[ "${#shorttime}" -eq 6 ]]
then
shorttime="${shorttime}--:--"
if [[ -z "${scroll// /}" ]]
then
printf "%-${LONG}s" "$(</dev/shm/mpd)" > /dev/shm/mpd-scroll
song="$(</dev/shm/mpd-scroll)"
else
song="$(</dev/shm/mpd-scroll)"
printf "%-${LONG}s" "${song:1}" > /dev/shm/mpd-scroll
fi
fi
echo "${song:0:25} $status$time</span>"
echo "${song:0:10} $status$shorttime</span>"
time="$(echo -e 'status\nclose' | nc -U .mpd/socket | grep time \
| cut -d' ' -f2)"
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)
longsong="${song} "
shortsong="${song} "
;;
short)
longsong="$(</dev/shm/mpd) "
shortsong="${song} "
;;
none)
longsong="$(</dev/shm/mpd) "
shortsong="$longsong"
;;
esac
longsong="${longsong:0:$LONG}"
shortsong="${shortsong:0:$SHORT}"
longsong="${longsong//&/&amp;}"
shortsong="${shortsong//&/&amp;}"
if [[ "$longsong" =~ § ]]
then
longsong="<i>${longsong/§/</i> }"
elif [[ "$song" =~ § ]]
then
longsong="<i>${longsong}</i>"
fi
if [[ "$shortsong" =~ § ]]
then
shortsong="<i>${shortsong/§/</i> }"
elif [[ "$song" =~ § ]]
then
shortsong="<i>${shortsong}</i>"
fi
echo "${longsong} $status$longtime</span>"
echo "${shortsong} $status$shorttime</span>"
else
[[ -f /dev/shm/mpd ]] && rm /dev/shm/mpd
[[ -f /dev/shm/mpd-scroll ]] && rm /dev/shm/mpd-scroll
echo "$status[--:-- --:-- --:--]</span>"
echo "$status--:-- --:--</span>"
printf "%${LONG}s $status[--:-- --:-- --:--]</span>\n" " "
printf "%${SHORT}s $status--:-- --:--</span>\n" " "
fi