Archived
0
0
Fork 0

added polybar music module

This commit is contained in:
Daryl Ronningen 2022-07-11 11:20:24 -07:00
parent 83b4203efa
commit 5bdc4fef2d
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
2 changed files with 95 additions and 0 deletions

View file

@ -41,12 +41,15 @@ font-0 = PowerlineSymbols:size=14;4
font-1 = Font Awesome 5 Free Solid:pixelsize=12;2
font-2 = FiraCode Nerd Font Mono:pixelsize=10;2
modules-left = music music-prev music-play-pause music-next
modules-center = windows
modules-right = tray
cursor-click = pointer
cursor-scroll = ns-resize
enable-ipc = true
[colors]
background = ${xrdb:background}
@ -143,6 +146,37 @@ label =  RAM: %percentage_used%%
label-foreground = ${colors.text}
[module/music]
type = custom/script
tail = true
interval = 1
format-prefix = " "
format = <label>
exec = $HOME/.config/polybar/music.sh
[module/music-prev]
type = custom/script
exec = echo "  "
format = <label>
click-left = $HOME/.config/polybar/music.sh --prev
[module/music-play-pause]
type = custom/ipc
hook-0 = echo "  "
hook-1 = echo "  "
initial = 1
click-left = $HOME/.config/polybar/music.sh --toggle
[module/music-next]
type = custom/script
exec = echo "  "
format = <label>
click-left = $HOME/.config/polybar/music.sh --next
[module/pulseaudio]
type = internal/pulseaudio

61
.config/polybar/music.sh Executable file
View file

@ -0,0 +1,61 @@
#!/bin/bash
PARENT_BAR="bottom"
PARENT_BAR_PID=$(pgrep -a "polybar" | grep "$PARENT_BAR" | cut -d" " -f1)
PLAYERS=$(playerctl -l)
readarray -t PLAYERS_ARRAY <<< "$PLAYERS"
PLAYER=
for CHECK_PLAYER in "${PLAYERS_ARRAY[@]}"; do
PLAYERCTL_STATUS=$(playerctl status -p "$CHECK_PLAYER")
if [ "$PLAYERCTL_STATUS" = "Stopped" ]; then
continue
elif [ "$PLAYERCTL_STATUS" = "Playing" ]; then
PLAYER="$CHECK_PLAYER"
break
else
PLAYER="$CHECK_PLAYER"
fi
done
FORMAT="{{ title }} - {{ artist }}"
update_hooks() {
while IFS= read -r id
do
polybar-msg -p "$id" hook music-play-pause $2 1>/dev/null 2>&1
done < <(echo "$1")
}
PLAYERCTL_STATUS=$(playerctl --player=$PLAYER status 2>/dev/null)
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
STATUS=$PLAYERCTL_STATUS
else
STATUS="No player is running"
fi
if [ "$1" == "--status" ]; then
echo "$STATUS"
elif [ "$1" == "--toggle" ]; then
playerctl --player=$PLAYER play-pause
elif [ "$1" == "--next" ]; then
playerctl --player=$PLAYER next
elif [ "$1" == "--prev" ]; then
playerctl --player=$PLAYER previous
else
if [ "$STATUS" = "Stopped" ]; then
echo "No music is playing"
elif [ "$STATUS" = "Paused" ]; then
update_hooks "$PARENT_BAR_PID" 2
playerctl --player=$PLAYER metadata --format "$FORMAT"
elif [ "$STATUS" = "No player is running" ]; then
echo "$STATUS"
else
update_hooks "$PARENT_BAR_PID" 1
playerctl --player=$PLAYER metadata --format "$FORMAT"
fi
fi