Made scripts more readable by moving most logic to functions.

This commit is contained in:
cronyakatsuki 2022-11-04 14:54:08 +01:00
parent 86e3a3a79c
commit 5c738fdc77
11 changed files with 234 additions and 133 deletions

View file

@ -1,40 +1,28 @@
#!/bin/bash
#!/bin/env bash
# script to run a menu prompt when having more than 2 players since
# playerctl is bad at managing more than one player at the same time
source $HOME/.config/dmenu/config
load_config () {
source $HOME/.config/dmenu/config
}
command="$@"
[ -z "$command" ] && exit
instances=$(playerctl -l | wc -l)
main () {
load_config
slock=$(ps -C slock | sed -n '1!p' | awk '{ print $4 }' | sed 's/slock/running/')
command="$@"
[ -z "$command" ] && exit
instances=$(playerctl -l | wc -l)
if [ "$instances" = "0" ]; then
exit
elif [ "$instances" -lt "2" ]; then
playerctl $command
elif [ "$slock" = "running" ]; then
if [ "$command" = "play-pause" ]; then
if playerctl status -a | grep -i "Playing" > /dev/null; then
playerctl pause -a
else
playerctl -p mpd $command
fi
if [ "$instances" = "0" ]; then
exit
elif [ "$instances" -lt "2" ]; then
playerctl $command
else
choice=$(playerctl -l | $DMENU -p 'Manage:')
[ -z "$choice" ] && exit
playerctl -p $choice $command
fi
}
players=$(paste <(playerctl -l) <(playerctl status -a) | grep -v -i mpd | awk '{print $1}')
if [ ! -z "$players" ]; then
for player in $players; do
playerctl -p $player pause
done
fi
playerctl -p mpd $command
else
choice=$(playerctl -l | $DMENU -p 'Manage:')
[ -z "$choice" ] && exit
playerctl -p $choice $command
fi
main $@