diff options
-rwxr-xr-x | bin/player | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/bin/player b/bin/player new file mode 100755 index 0000000..b6e66d7 --- /dev/null +++ b/bin/player @@ -0,0 +1,136 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + player ACTION + player -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -h, --help show this message + + ACTION one of: + - backward: go back 5 seconds + - forward: go forward 5 seconds + - previous: go to the previous track + - next: go to the next track + - play-pause: play/pause + - rotate: rotate across available MPRIS players + - current: show the current MPRIS player + + + Manipulate the MPRIS audio player. + + + Examples: + + Change the current MPRIS player: + + $ player current + + + Play/pause: + + $ player play-pause + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'P:h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) +ACTION="${1:-}" + +eval "$(assert-arg "$ACTION" 'ACTION')" + + + +CURRENT_PLAYER_PATH="$XDG_CACHE_HOME"/euandreh-mpris-player.txt +CURRENT_PLAYER="$(cat "$CURRENT_PLAYER_PATH" ||:)" +AVAILABLE_PLAYERS="$(playerctl --list-all | LANG=POSIX.UTF-8 sort)" + +pick_first() { + echo "$AVAILABLE_PLAYERS" | head -n1 +} + +next_player() { + if [ -z "$CURRENT_PLAYER" ]; then + pick_first + elif ! echo "$AVAILABLE_PLAYERS" | grep -q "$CURRENT_PLAYER"; then + # Unknown $CURRENT_PLAYER, pick anyone + pick_first + else + INDEX="$(echo "$AVAILABLE_PLAYERS" | grep -n "$CURRENT_PLAYER" | cut -d: -f1)" + LENGTH="$(echo "$AVAILABLE_PLAYERS" | wc -l)" + if [ "$INDEX" = "$LENGTH" ]; then + # Reached the end of the $AVAILABLE_PLAYERS list, wrapping + pick_first + else + # Get the next player instead + echo "$AVAILABLE_PLAYERS" | awk -v idx="$INDEX" 'NR == idx+1 {print}' + fi + fi +} + +case "$ACTION" in + backward) + playerctl --player="$CURRENT_PLAYER" position 5- + ;; + forward) + playerctl --player="$CURRENT_PLAYER" position 5+ + ;; + previous) + playerctl --player="$CURRENT_PLAYER" previous + ;; + next) + playerctl --player="$CURRENT_PLAYER" next + ;; + play-pause) + playerctl --player="$CURRENT_PLAYER" play-pause + ;; + rotate) + PLAYER="$(next_player)" + echo "$PLAYER" > "$CURRENT_PLAYER_PATH" + notify-send -t 1000 "$PLAYER" 'current MPRIS target' + ;; + current) + printf '%s\n' "$CURRENT_PLAYER" + ;; + *) + printf 'Bad ACTION: "%s".\n\n' "$ACTION" >&2 + usage >&2 + exit 2 + ;; +esac |