Small refactor to the polybar's mpv script

master
Meliurwen 3 years ago
parent 98f028c007
commit 8737d60d84
Signed by: meliurwen
GPG Key ID: 818A8B35E9F1CE10
  1. 121
      polybar/.config/polybar/scripts/mpv.sh

@ -10,81 +10,78 @@
time_to_human(){ time_to_human(){
if [ "$API_OUTPUT" -gt 3600 ]; then if [ "$API_OUTPUT" -gt 3600 ]; then
API_OUTPUT=$(printf '%d:%02d:%02d' $((API_OUTPUT/3600)) $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) 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 else
if [ "$API_OUTPUT" -gt 60 ]; then API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60)))
API_OUTPUT=$(printf '%02d:%02d' $((API_OUTPUT%3600/60)) $((API_OUTPUT%60)))
else
API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60)))
fi
fi 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" MPV_SOCKET_PATH="${XDG_RUNTIME_DIR:-/tmp/$(id -u)-runtime}/mpv/main.sock"
OUTPUT="" 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 if [ "$(printf "%s" "$TIME" | jq -r .error)" = "success" ]; then
# See: https://github.com/deterenkelt/Nadeshiko/wiki/Known-issues-for-Nadeshiko%E2%80%91mpv#----connection-refused while [ $# -gt 0 ]; do
if ! TIME="$(printf '{ "command": ["get_property", "time-pos"] }\n' | socat - "$MPV_SOCKET_PATH" 2> /dev/null)"; then COMMAND=$1
exit 1 VALID_COMMAND=true
fi case $COMMAND in
if [ "$(printf "%s" "$TIME" | jq -r .error)" = "success" ]; then "time-pos" | "time-remaining" | "duration" | "media-title" | "playlist-pos" | "playlist-pos-1" | "playlist-count")
while [ $# -gt 0 ]; do API_OUTPUT=$(printf '{ "command": ["get_property", "%s"] }\n' "$COMMAND" | socat - "$MPV_SOCKET_PATH")
COMMAND=$1 ;;
VALID_COMMAND=true "core-idle" | "play-pause-btn")
case $COMMAND in API_OUTPUT=$(printf '{ "command": ["get_property", "core-idle"] }\n' | socat - "$MPV_SOCKET_PATH")
"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") *)
;; VALID_COMMAND=false
"core-idle" | "play-pause-btn") ;;
API_OUTPUT=$(printf '{ "command": ["get_property", "core-idle"] }\n' | socat - "$MPV_SOCKET_PATH") esac
;;
*)
VALID_COMMAND=false
;;
esac
if $VALID_COMMAND; then if $VALID_COMMAND; then
if [ "$(printf "%s" "$API_OUTPUT" | jq -r .error)" = "success" ]; then if [ "$(printf "%s" "$API_OUTPUT" | jq -r .error)" = "success" ]; then
case $COMMAND in case $COMMAND in
"time-pos" | "time-remaining" | "duration") "time-pos" | "time-remaining" | "duration")
API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -d'.' -f 1) API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -d'.' -f 1)
time_to_human time_to_human
;; ;;
"media-title") "media-title")
API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -c 1-35) API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -c 1-35)
;; ;;
"playlist-pos" | "playlist-pos-1" | "playlist-count") "playlist-pos" | "playlist-pos-1" | "playlist-count")
API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data) API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data)
;; ;;
"core-idle" | "play-pause-btn") "core-idle" | "play-pause-btn")
shift;
if [ "$(printf "%s" "$API_OUTPUT" | jq -r .data)" = "false" ]; then
API_OUTPUT=$1 #Play icon
shift; shift;
if [ "$(printf "%s" "$API_OUTPUT" | jq -r .data)" = "false" ]; then else
API_OUTPUT=$1 #Play icon shift;
shift; API_OUTPUT=$1 #Pause icon
else fi
shift; ;;
API_OUTPUT=$1 #Pause icon esac
fi
;;
esac
else
API_OUTPUT="API error!"
fi
else else
API_OUTPUT="$COMMAND" API_OUTPUT="API error!"
fi fi
OUTPUT="$OUTPUT$API_OUTPUT" else
shift; API_OUTPUT="$COMMAND"
done fi
OUTPUT="$OUTPUT$API_OUTPUT"
else shift;
OUTPUT="Loading..." done
fi
else else
exit OUTPUT="Loading..."
fi fi
printf %s "$OUTPUT" printf "%s" "$OUTPUT"

Loading…
Cancel
Save