[polybar] Refactor music display module

Refactored music module to use a constant tail output of the underlying
script, to enable mode-switching, and to allow playing or pausing the
current song.
This commit is contained in:
Marty Oehme 2020-05-11 12:27:52 +02:00
parent 3872eca254
commit f6517df5a2
No known key found for this signature in database
GPG Key ID: 0CCB0526EFB9611A
2 changed files with 56 additions and 16 deletions

View File

@ -54,7 +54,7 @@ padding-right = 0
module-margin-left = 2
modules-left = workspaces
modules-center = mprisdisplay date papersdue
modules-center = music date papersdue
modules-right = networkspeed archupdates cpu temp backlight volume battery
; do not use offsets for the bar, would only work with override-redirect
; and will mess up tray https://github.com/polybar/polybar/issues/1355
@ -207,6 +207,17 @@ date-alt = %A, %d %B %Y (W %V)
time-alt = %H:%M
label = %date% %{T2} %{T-}%time%
; display information on currently playing track, allows simple track manipulation
[module/music]
type = custom/script
exec = $XDG_CONFIG_HOME/polybar/scripts/poly-mprisdisplay
exec-if = type $XDG_CONFIG_HOME/polybar/scripts/poly-mprisdisplay
tail = true
click-left = kill -USR1 %pid%
click-right = kill -USR2 %pid%
; TODO: add album art display (on click?) - retrieved by playerctl metadata mpris:artUrl
; display information on remaining papers to read for the upcoming week
[module/papersdue]
type = custom/script
exec = $XDG_BIN_HOME/bib_due $BIBFILE | wc -l
@ -231,13 +242,6 @@ type = custom/script
exec = $XDG_CONFIG_HOME/polybar/scripts/poly-networkspeed
tail = true
[module/mprisdisplay]
type = custom/script
exec = $XDG_CONFIG_HOME/polybar/scripts/poly-mprisdisplay
interval = 3
click-left = playerctl play-pause &
; TODO: add album art display (on click?) - retrieved by playerctl metadata mpris:artUrl
[module/volume]
type = internal/pulseaudio
; Available tags:

View File

@ -1,11 +1,47 @@
#!/bin/sh
# script to echo the current playback situation
#
# call without args to return playing symbol or empty, depending on current playback status
# call with 1 to return 'artist - title' metadata (verbose mode)
# send SIGUSR2 signal to toggle pause/playing state of song (e.g. kill -USR2 %PID)
#
# depends on playerctl being installed
player_status=$(playerctl status 2>/dev/null)
exist playerctl low "polybar music" || exit 1
if [ "$player_status" = "Playing" ]; then
echo "ﱘ $(playerctl metadata artist) - $(playerctl metadata title)"
elif [ "$player_status" = "Paused" ]; then
echo " $(playerctl metadata artist) - $(playerctl metadata title)"
else
echo ""
fi
mode=${1:-0}
update_time=1
symbol_playing=""
symbol_paused=""
modetoggle() {
mode=$(((mode + 1) % 2))
}
trap "modetoggle" USR1
playpause() {
playerctl play-pause
}
trap "playpause" USR2
decorate() {
if [ "$player_status" = "Playing" ]; then
echo "${symbol_playing}$1"
elif [ "$player_status" = "Paused" ]; then
echo "${symbol_paused}$1"
else
echo ""
fi
}
while true; do
player_status=$(playerctl status 2>/dev/null)
if [ $mode -eq 0 ]; then
decorate ""
elif [ $mode -eq 1 ]; then
decorate " $(playerctl metadata artist) - $(playerctl metadata title)"
fi
sleep $update_time &
wait
done