dotfiles/.i3blocks/mpd

174 lines
4 KiB
Plaintext
Raw Normal View History

2022-08-13 00:56:52 +02:00
#!/bin/bash
2019-06-13 20:12:29 +02:00
2022-08-10 18:37:33 +02:00
# Fonction de réinitialisation en cas de perte de la connexion
self-reset() {
echo "<span foreground=\"#515151\">[--:-- --:-- --:--]</span>"
sleep 5
exec "$0"
}
trap self-reset PIPE
# Initialisation de la connexion au serveur
coproc nc -U /home/breizh/.mpd/socket
IN=${COPROC[1]}
OUT=${COPROC[0]}
#echo "password xxx" >&${IN}
2022-08-13 00:34:09 +02:00
sed '/^OK/q' <&$OUT &>/dev/null
2022-08-10 18:37:33 +02:00
# Initialisation des valeurs par défaut et des fonctions
2022-08-13 01:12:58 +02:00
declare -i SCROLL=4time_left0 I=0
2022-08-10 18:37:33 +02:00
declare -- artist song oldfile
2019-09-22 03:31:29 +02:00
2019-06-14 09:42:47 +02:00
human_time() {
2022-08-13 01:12:58 +02:00
local seconds="${1:-0}"
if [[ "$seconds" -le 0 ]]
2019-06-14 09:42:47 +02:00
then
echo "--:--"
return
fi
local minutes=$(( $seconds / 60 ))
seconds=$(( $seconds % 60 ))
2022-03-15 00:18:30 +01:00
if [[ "$minutes" -ge 60 ]]
2019-06-14 09:42:47 +02:00
then
2022-03-15 00:18:30 +01:00
local hours=$(( $minutes / 60 ))
minutes=$(( $minutes % 60 ))
printf "%02dh%02d\n" "$hours" "$minutes"
2019-06-14 09:42:47 +02:00
else
printf "%02d:%02d\n" "$minutes" "$seconds"
fi
return
}
2022-08-10 18:37:33 +02:00
# Initialisation de létat
echo "status" >&$IN
while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
# Boucle principale
while [[ -n "$COPROC_PID" ]]
do
# Gestion du clic, fait office de délai si aucune action (une seconde).
# En cas de clic, lactualisation de laffichage sera instantanné.
read -t 1 BLOCK_BUTTON
2019-06-13 20:12:29 +02:00
2022-08-10 18:37:33 +02:00
case $BLOCK_BUTTON in
2022-08-13 00:44:56 +02:00
1|3)
if [[ "${o["state"]}" == stop ]]
then
echo "play" >&${IN} || self-reset
sed '/^OK$/q' <&$OUT &>/dev/null
fi
2022-08-13 00:54:00 +02:00
;;&
2022-08-13 00:44:56 +02:00
2)
if [[ "${o["state"]}" == stop ]]
then
echo "play" >&${IN} || self-reset
else
echo "pause" >&${IN} || self-reset
fi
2022-08-13 00:54:00 +02:00
;;&
2022-08-13 00:34:09 +02:00
1) echo "previous" >&${IN} || self-reset ;;&
3) echo "next" >&${IN} || self-reset ;;&
2022-08-10 18:37:33 +02:00
1|2|3) sed '/^OK$/q' <&$OUT &>/dev/null ;;
esac
2019-06-13 20:12:29 +02:00
2022-08-10 18:37:33 +02:00
# Réinitialisation de létat
2022-08-13 00:48:35 +02:00
unset o output
2022-08-10 18:37:33 +02:00
declare -A o
2019-06-14 09:42:47 +02:00
2022-08-10 18:37:33 +02:00
# Récupération de létat
2022-08-13 00:34:09 +02:00
echo "status" >&$IN || self-reset
2022-08-10 18:37:33 +02:00
while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
2022-08-13 00:48:35 +02:00
2022-08-10 18:37:33 +02:00
# Récupération des informations du morceau en cours
2022-08-13 00:34:09 +02:00
echo "currentsong" >&$IN || self-reset
2022-08-10 18:37:33 +02:00
while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
# Choix des couleurs selon létat
case "${o["state"]}" in
play ) status="<span foreground=\"#99CC99\">" ;;
pause ) status="<span foreground=\"#FFCC66\">" ;;
stop ) status="<span foreground=\"#F2777A\">" ;;
2019-06-14 09:42:47 +02:00
esac
2022-08-10 18:37:33 +02:00
# En cas de changement de morceau,
# on réinitialise laffichage défilant
if [[ "${o["file"]}" != "$oldfile" ]]
2019-06-13 20:12:29 +02:00
then
2022-08-10 18:37:33 +02:00
unset bloc1 bloc2 bloc3 bloc4
artist="${o["Artist"]:-${o["Name"]}}"
song="${o["Title"]}"
2022-08-13 00:34:09 +02:00
[[ -z "$artist$song" ]] && song="$(basename "${o["file"]}")"
2022-08-10 18:37:33 +02:00
if [[ "$(( ${#song} + ${#artist} + 1 ))" -gt "$SCROLL" ]]
then
scroll=true
song=" $song "
else
scroll=false
song=" $song"
fi
bloc1="$artist"
bloc2="$song"
oldfile="${o["file"]}"
I=0
2019-06-13 20:12:29 +02:00
fi
2022-08-10 18:37:33 +02:00
# Gestion du défilement
if "$scroll"
2019-06-13 20:12:29 +02:00
then
2022-08-10 18:37:33 +02:00
unset bloc1 bloc2 bloc3 bloc4
declare -- bloc1 bloc2 bloc3 bloc4
bloc1="${artist:$I:$SCROLL}"
if [[ "${#bloc1}" -eq 0 ]]
then
bloc2="${song:$(( I - ${#artist} )):$SCROLL}"
if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]]
then
bloc3="${artist:0:$(( SCROLL - ${#bloc2} ))}"
if [[ "$(( ${#bloc1} + ${#bloc2} + ${#bloc3} ))" -lt "$SCROLL" ]]
then
bloc4="${song:0:$(( SCROLL - ${#bloc1} - ${#bloc2} - ${#bloc3} ))}"
fi
fi
else
bloc2="${song:0:$(( SCROLL - ${#bloc1} ))}"
if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]]
then
bloc3="${artist:0:$(( SCROLL - ${#bloc2} - ${#bloc1} ))}"
fi
fi
I+=1
[[ "$I" -ge "$(( ${#artist} + ${#song} ))" ]] && I=0
2019-06-13 20:12:29 +02:00
fi
2022-08-10 18:37:33 +02:00
# Gestion du temps
2022-08-13 01:12:58 +02:00
declare -i time_elapsed time_total time_left
time_elapsed="${o["time"]%:*}"
time_total="${o["time"]#*:}"
time_left="${time_total:-${o["duration"]%.*}} - ${time_elapsed:-${o["Time"]}}"
time="[$(human_time ${time_elapsed}) $(human_time ${time_left}) $(human_time ${time_total})]"
2019-06-13 20:12:29 +02:00
2022-08-10 18:37:33 +02:00
# DEBUG
printf '<b>%s</b>%s<b>%s</b>%s%s\n' "$bloc1" "$bloc2" "$bloc3" "$bloc4" " $status$time</span>"
done