From 8737d60d844d5a886245ba77545c59bea839082c Mon Sep 17 00:00:00 2001 From: meliurwen Date: Sat, 9 Apr 2022 10:03:06 +0200 Subject: [PATCH] Small refactor to the polybar's mpv script --- polybar/.config/polybar/scripts/mpv.sh | 121 ++++++++++++------------- 1 file changed, 59 insertions(+), 62 deletions(-) diff --git a/polybar/.config/polybar/scripts/mpv.sh b/polybar/.config/polybar/scripts/mpv.sh index 08c15cb..5010d7a 100755 --- a/polybar/.config/polybar/scripts/mpv.sh +++ b/polybar/.config/polybar/scripts/mpv.sh @@ -10,81 +10,78 @@ time_to_human(){ if [ "$API_OUTPUT" -gt 3600 ]; then API_OUTPUT=$(printf '%d:%02d:%02d' $((API_OUTPUT/3600)) $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) + elif [ "$API_OUTPUT" -gt 60 ]; then + API_OUTPUT=$(printf '%02d:%02d' $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) else - if [ "$API_OUTPUT" -gt 60 ]; then - API_OUTPUT=$(printf '%02d:%02d' $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) - else - API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60))) - fi + API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60))) fi } +if ! pgrep -u "$(id -u)" -x mpv > /dev/null; then + exit 1 +fi + MPV_SOCKET_PATH="${XDG_RUNTIME_DIR:-/tmp/$(id -u)-runtime}/mpv/main.sock" OUTPUT="" -if pgrep -u "$(id -u)" -x mpv > /dev/null; then +# The "16) Connection refused" error happens at the row below +# See: https://github.com/deterenkelt/Nadeshiko/wiki/Known-issues-for-Nadeshiko%E2%80%91mpv#----connection-refused +if ! TIME="$(printf '{ "command": ["get_property", "time-pos"] }\n' | socat - "$MPV_SOCKET_PATH" 2> /dev/null)"; then + exit 1 +fi - # The "16) Connection refused" error happens at the row below - # See: https://github.com/deterenkelt/Nadeshiko/wiki/Known-issues-for-Nadeshiko%E2%80%91mpv#----connection-refused - if ! TIME="$(printf '{ "command": ["get_property", "time-pos"] }\n' | socat - "$MPV_SOCKET_PATH" 2> /dev/null)"; then - exit 1 - fi - if [ "$(printf "%s" "$TIME" | jq -r .error)" = "success" ]; then - while [ $# -gt 0 ]; do - COMMAND=$1 - VALID_COMMAND=true - case $COMMAND in - "time-pos" | "time-remaining" | "duration" | "media-title" | "playlist-pos" | "playlist-pos-1" | "playlist-count") - API_OUTPUT=$(printf '{ "command": ["get_property", "%s"] }\n' "$COMMAND" | socat - "$MPV_SOCKET_PATH") - ;; - "core-idle" | "play-pause-btn") - API_OUTPUT=$(printf '{ "command": ["get_property", "core-idle"] }\n' | socat - "$MPV_SOCKET_PATH") - ;; - *) - VALID_COMMAND=false - ;; - esac +if [ "$(printf "%s" "$TIME" | jq -r .error)" = "success" ]; then + while [ $# -gt 0 ]; do + COMMAND=$1 + VALID_COMMAND=true + case $COMMAND in + "time-pos" | "time-remaining" | "duration" | "media-title" | "playlist-pos" | "playlist-pos-1" | "playlist-count") + API_OUTPUT=$(printf '{ "command": ["get_property", "%s"] }\n' "$COMMAND" | socat - "$MPV_SOCKET_PATH") + ;; + "core-idle" | "play-pause-btn") + API_OUTPUT=$(printf '{ "command": ["get_property", "core-idle"] }\n' | socat - "$MPV_SOCKET_PATH") + ;; + *) + VALID_COMMAND=false + ;; + esac - if $VALID_COMMAND; then - if [ "$(printf "%s" "$API_OUTPUT" | jq -r .error)" = "success" ]; then - case $COMMAND in - "time-pos" | "time-remaining" | "duration") - API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -d'.' -f 1) - time_to_human - ;; - "media-title") - API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -c 1-35) - ;; - "playlist-pos" | "playlist-pos-1" | "playlist-count") - API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data) - ;; - "core-idle" | "play-pause-btn") + if $VALID_COMMAND; then + if [ "$(printf "%s" "$API_OUTPUT" | jq -r .error)" = "success" ]; then + case $COMMAND in + "time-pos" | "time-remaining" | "duration") + API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -d'.' -f 1) + time_to_human + ;; + "media-title") + API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -c 1-35) + ;; + "playlist-pos" | "playlist-pos-1" | "playlist-count") + API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data) + ;; + "core-idle" | "play-pause-btn") + shift; + if [ "$(printf "%s" "$API_OUTPUT" | jq -r .data)" = "false" ]; then + API_OUTPUT=$1 #Play icon shift; - if [ "$(printf "%s" "$API_OUTPUT" | jq -r .data)" = "false" ]; then - API_OUTPUT=$1 #Play icon - shift; - else - shift; - API_OUTPUT=$1 #Pause icon - fi - ;; - esac - else - API_OUTPUT="API error!" - fi + else + shift; + API_OUTPUT=$1 #Pause icon + fi + ;; + esac else - API_OUTPUT="$COMMAND" + API_OUTPUT="API error!" fi - OUTPUT="$OUTPUT$API_OUTPUT" - shift; - done - - else - OUTPUT="Loading..." - fi + else + API_OUTPUT="$COMMAND" + fi + OUTPUT="$OUTPUT$API_OUTPUT" + shift; + done else - exit + OUTPUT="Loading..." fi -printf %s "$OUTPUT" +printf "%s" "$OUTPUT"