Changement du script i3blocks/mpd

This commit is contained in:
Breizh 2022-08-10 18:37:33 +02:00
parent cc6f15e243
commit 8ce08fa228
4 changed files with 149 additions and 117 deletions
.i3blocks

View file

@ -1,27 +1,24 @@
#!/bin/bash
runtime="${XDG_RUNTIME_DIR}/i3blocks"
[[ ! -d "$runtime" ]] && mkdir -p "$runtime"
# Fonction de réinitialisation en cas de perte de la connexion
self-reset() {
echo "<span foreground=\"#515151\">[--:-- --:-- --:--]</span>"
sleep 5
exec "$0"
}
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
trap self-reset PIPE
LONG=40
SHORT=20
# Initialisation de la connexion au serveur
coproc nc -U /home/breizh/.mpd/socket
IN=${COPROC[1]}
OUT=${COPROC[0]}
#echo "password xxx" >&${IN}
#sed '/^OK$/q' <&$OUT &>/dev/null
case $BLOCK_BUTTON in
1) mpc -q prev ;;
2) mpc -q toggle ;;
3) mpc -q next ;;
#4) mpc -q volume +2 ;;
#5) mpc -q volume -2 ;;
#?*) notify-send "Test" "$BLOCK_BUTTON" ;;
esac
# Initialisation des valeurs par défaut et des fonctions
declare -i SCROLL=50 I=0
declare -- artist song oldfile
human_time() {
local seconds="$1"
@ -43,100 +40,123 @@ human_time() {
return
}
# 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
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
if [[ ! -f $runtime/mpd ]]
then
mpc -f '[[%artist%§|%name%§]%title%|[%file%]' current > ${runtime}/mpd
fi
song=$(<${runtime}/mpd)
case $BLOCK_BUTTON in
1|3) [[ "${o["state"]}" == stop ]] \
&& echo "play" >&${IN} \
&& sed '/^OK$/q' <&$OUT &>/dev/null ;;&
2) [[ "${o["state"]}" == stop ]] \
&& echo "play" >&${IN} \
|| echo "pause" >&${IN} ;;&
1) echo "previous" >&${IN} ;;&
3) echo "next" >&${IN} ;;&
1|2|3) sed '/^OK$/q' <&$OUT &>/dev/null ;;
esac
if [[ "${#song}" -gt $LONG ]]
then
scrolling=long
elif [[ "${#song}" -gt $SHORT ]]
then
scrolling=short
else
scrolling=none
fi
if [[ "$scrolling" != "none" ]]
then
scroll="$(<${runtime}/mpd-scroll)"
# Réinitialisation de létat
unset o
declare -A o
if [[ -z "${scroll// /}" ]]
then
printf "%-${LONG}s" "$(<${runtime}/mpd)" > ${runtime}/mpd-scroll
song="$(<${runtime}/mpd-scroll)"
else
song="$(<${runtime}/mpd-scroll)"
printf "%-${LONG}s" "${song:1}" > ${runtime}/mpd-scroll
fi
fi
time="$(echo -e 'status\nclose' | nc -U $MPD_HOST | grep time \
| cut -d' ' -f2)"
# Récupération de létat
echo "status" >&$IN
while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
time_elapsed=$(human_time "${time%:*}")
time_total=$(human_time "${time#*:}")
time_left=$(human_time "$(( ${time#*:} - ${time%:*} ))")
# Récupération des informations du morceau en cours
echo "currentsong" >&$IN
while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
longtime="[$time_elapsed $time_left ${time_total/00:00/--:--}]"
shorttime="$time_elapsed $time_left"
case $scrolling in
long)
longsong="${song}"
shortsong="${song}"
;;
short)
longsong="$(<${runtime}/mpd)"
shortsong="${song}"
;;
none)
longsong="$(<${runtime}/mpd)"
shortsong="$longsong"
;;
# 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\">" ;;
esac
longsong="${longsong:0:$LONG}"
shortsong="${shortsong:0:$SHORT}"
longsong="${longsong//&/&amp;}"
shortsong="${shortsong//&/&amp;}"
if [[ "$longsong" =~ § ]]
# En cas de changement de morceau,
# on réinitialise laffichage défilant
if [[ "${o["file"]}" != "$oldfile" ]]
then
longsong="<b>${longsong/§/</b> }"
elif [[ "$song" =~ § ]]
then
longsong="<b>${longsong}</b>"
fi
if [[ "$shortsong" =~ § ]]
then
shortsong="<b>${shortsong/§/</b> }"
elif [[ "$song" =~ § ]]
then
shortsong="<b>${shortsong}</b>"
unset bloc1 bloc2 bloc3 bloc4
artist="${o["Artist"]:-${o["Name"]}}"
song="${o["Title"]}"
[[ -z "$artist$song" ]] && song="${o["file"]}"
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
fi
echo "${longsong} $status$longtime</span>"
echo "${shortsong} $status$shorttime</span>"
else
[[ -f ${runtime}/mpd ]] && rm ${runtime}/mpd
[[ -f ${runtime}/mpd-scroll ]] && rm ${runtime}/mpd-scroll
printf "$status[--:-- --:-- --:--]</span>\n" " "
printf "$status--:-- --:--</span>\n" " "
fi
# Gestion du défilement
if "$scroll"
then
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
fi
# Gestion du temps
time_elapsed=$(human_time "${o["time"]%:*}")
time_total=$(human_time "${o["time"]#*:}")
time_left=$(human_time "$(( ${o["time"]#*:} - ${o["time"]%:*} ))")
time="[${time_elapsed:---:--} ${time_left:---:--} ${time_total:---:--}]"
# DEBUG
printf '<b>%s</b>%s<b>%s</b>%s%s\n' "$bloc1" "$bloc2" "$bloc3" "$bloc4" " $status$time</span>"
done