From f6517df5a24b8b424f5befacb3a739d1c854d741 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 11 May 2020 12:27:52 +0200 Subject: [PATCH] [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. --- polybar/.config/polybar/config | 20 ++++--- .../.config/polybar/scripts/poly-mprisdisplay | 52 ++++++++++++++++--- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/polybar/.config/polybar/config b/polybar/.config/polybar/config index 5dc014d..6bda321 100644 --- a/polybar/.config/polybar/config +++ b/polybar/.config/polybar/config @@ -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: diff --git a/polybar/.config/polybar/scripts/poly-mprisdisplay b/polybar/.config/polybar/scripts/poly-mprisdisplay index 54d0257..faf53a6 100755 --- a/polybar/.config/polybar/scripts/poly-mprisdisplay +++ b/polybar/.config/polybar/scripts/poly-mprisdisplay @@ -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