dotfiles/.i3blocks/mpd

193 lines
4.5 KiB
Text
Raw Normal View History

#!/bin/bash -x
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>"
2022-08-20 20:23:03 +02:00
sleep 1
2022-08-10 18:37:33 +02:00
exec "$0"
}
trap self-reset PIPE
# Initialisation de la connexion au serveur
2022-09-19 21:23:33 +02:00
# coproc nc -U /home/breizh/.mpd/socket
2022-12-04 20:44:55 +01:00
coproc socat -T 5 stdio /home/breizh/.mpd/socket
2022-08-10 18:37:33 +02:00
IN=${COPROC[1]}
OUT=${COPROC[0]}
2022-08-13 01:38:52 +02:00
#echo "password xxx" >&"${IN}"
2022-08-10 18:37:33 +02:00
# Initialisation des valeurs par défaut et des fonctions
2022-08-13 01:38:52 +02:00
declare -i SCROLL=50 I=0
2022-08-13 02:28:54 +02:00
declare -- artist song
declare -A old
2019-09-22 03:31:29 +02:00
2019-06-14 09:42:47 +02:00
human_time() {
2022-08-13 01:59:25 +02:00
local seconds="${1}"
if [[ -z "$seconds" || "$seconds" -lt 0 ]]
2019-06-14 09:42:47 +02:00
then
echo "--:--"
return
fi
2022-08-13 01:38:52 +02:00
local minutes=$(( seconds / 60 ))
seconds=$(( seconds % 60 ))
2023-04-12 02:34:24 +02:00
if [[ "$minutes" -ge 90 ]]
2019-06-14 09:42:47 +02:00
then
2022-08-13 01:38:52 +02:00
local hours=$(( minutes / 60 ))
minutes=$(( minutes % 60 ))
2023-04-12 02:34:24 +02:00
printf "%2dh%02d\n" "$hours" "$minutes"
2019-06-14 09:42:47 +02:00
else
2023-04-12 02:34:24 +02:00
printf "%2d:%02d\n" "$minutes" "$seconds"
2019-06-14 09:42:47 +02:00
fi
return
}
2022-08-10 18:37:33 +02:00
# Initialisation de létat
2022-08-13 01:38:52 +02:00
echo "status" >&"$IN"
2022-08-20 20:23:03 +02:00
while read -t 0.1 -u "$OUT" output
2022-08-10 18:37:33 +02:00
do
[[ "$output" =~ : ]] && o[${output%%:*}]="${output#*: }"
2022-08-10 18:37:33 +02:00
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 0.85 BLOCK_BUTTON
2022-08-20 20:23:03 +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
2022-08-13 01:38:52 +02:00
echo "play" >&"${IN}" || self-reset
sed '/^OK$/q' <&"$OUT" &>/dev/null
2022-08-13 00:44:56 +02:00
fi
2022-08-13 00:54:00 +02:00
;;&
2022-08-13 00:44:56 +02:00
2)
if [[ "${o["state"]}" == stop ]]
then
2022-08-13 01:38:52 +02:00
echo "play" >&"${IN}" || self-reset
2022-08-13 00:44:56 +02:00
else
2022-08-13 01:38:52 +02:00
echo "pause" >&"${IN}" || self-reset
2022-08-13 00:44:56 +02:00
fi
2022-08-13 00:54:00 +02:00
;;&
2022-08-13 01:38:52 +02:00
1) echo "previous" >&"${IN}" || self-reset ;;&
3) echo "next" >&"${IN}" || self-reset ;;&
1|2|3) sed '/^OK$/q' <&"$OUT" &>/dev/null ;;
4) I="I-2"; [[ "$I" -lt 0 ]] && I=0 ;;
2022-08-10 18:37:33 +02:00
esac
2022-08-20 20:23:03 +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
2022-08-20 20:23:03 +02:00
# Récupération de létat et des informations du morceau en cours
2022-08-13 01:38:52 +02:00
echo "status" >&"$IN" || self-reset
echo "currentsong" >&"$IN" || self-reset
2022-08-20 20:23:03 +02:00
while read -t 0.1 -u "$OUT" output
2022-08-10 18:37:33 +02:00
do
[[ "$output" =~ : ]] && o[${output%%:*}]="${output#*: }"
2022-08-10 18:37:33 +02:00
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
2022-08-13 02:28:54 +02:00
if [[ "${o["file"]}" != "${old["file"]}" || "${o["Title"]}" != "${old["Title"]}" || "${o["Name"]}" != "${old["Name"]}" ]]
2019-06-13 20:12:29 +02:00
then
2022-08-10 18:37:33 +02:00
artist="${o["Artist"]:-${o["Name"]}}"
song="${o["Title"]}"
if [[ -z "$artist" ]]
then
if [[ -z "$song" ]]
then
song="$(basename "${o["file"]}")"
else
song="$song $(basename "${o["file"]}")"
fi
fi
2022-08-10 18:37:33 +02:00
if [[ "$(( ${#song} + ${#artist} + 1 ))" -gt "$SCROLL" ]]
then
scroll=true
song=" $song "
else
2022-08-20 20:23:03 +02:00
unset scroll
2022-08-10 18:37:33 +02:00
song=" $song"
fi
2022-08-13 02:28:54 +02:00
old["file"]="${o["file"]}"
old["Title"]="${o["Title"]}"
old["Name"]="${o["Name"]}"
2022-08-10 18:37:33 +02:00
I=0
2019-06-13 20:12:29 +02:00
fi
2022-08-10 18:37:33 +02:00
# Gestion du défilement
2022-08-20 20:23:03 +02:00
if [[ -n "$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
else
unset bloc1 bloc2 bloc3 bloc4
bloc1="$artist"
bloc2="$song"
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_total="${o["time"]#*:}"
[[ "$time_total" -eq 0 ]] && time_total="${o["Time"]}"
[[ "$time_total" -eq 0 ]] && time_total=-1
2022-08-13 01:59:25 +02:00
if [[ "${o["state"]}" == "stop" ]]
then
time_elapsed=-1
time_left=-1
else
time_elapsed="${o["time"]%:*}"
time_left="${time_total} - ${time_elapsed}"
fi
2022-08-13 01:12:58 +02:00
2022-08-13 01:38:52 +02:00
time="[$(human_time "${time_elapsed}") $(human_time "${time_left}") $(human_time "${time_total}")]"
2019-06-13 20:12:29 +02:00
2022-08-13 01:59:25 +02:00
# Nettoyage des chaînes de caractère pour pango
2022-08-13 01:17:28 +02:00
bloc1="${bloc1//&/&amp;}"
bloc2="${bloc2//&/&amp;}"
bloc3="${bloc3//&/&amp;}"
bloc4="${bloc4//&/&amp;}"
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
2022-08-20 20:23:03 +02:00
self-reset