Simplified some logic, removed useless nesting and reduced tabulation from 4 to 2 spaces in polybar's mpv script

master
Meliurwen 3 years ago
parent 92afae2bc7
commit c7956a9c1c
Signed by: meliurwen
GPG Key ID: 818A8B35E9F1CE10
  1. 70
      polybar/.config/polybar/scripts/mpv.sh

@ -1,5 +1,6 @@
#!/bin/sh #!/bin/sh
# Dependencies: mpv jq socat
# Note: In order to use this script you have to tell to mpv to enable a socket. # Note: In order to use this script you have to tell to mpv to enable a socket.
# Two ways are possible to achieve this: # Two ways are possible to achieve this:
# 1) Use the argument --input-ipc-server when you launch it. For example: # 1) Use the argument --input-ipc-server when you launch it. For example:
@ -8,12 +9,13 @@
# `input-ipc-server=mpvsocket` # `input-ipc-server=mpvsocket`
time_to_human(){ time_to_human(){
if [ "$API_OUTPUT" -gt 3600 ]; then seconds=$1
API_OUTPUT=$(printf '%d:%02d:%02d' $((API_OUTPUT/3600)) $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) if [ "$seconds" -gt 3600 ]; then
elif [ "$API_OUTPUT" -gt 60 ]; then printf '%d:%02d:%02d' $((seconds/3600)) $((seconds%3600/60)) $((seconds%60))
API_OUTPUT=$(printf '%02d:%02d' $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) elif [ "$seconds" -gt 60 ]; then
printf '%02d:%02d' $((seconds%3600/60)) $((seconds%60))
else else
API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60))) printf '%02d' $((seconds%60))
fi fi
} }
@ -31,57 +33,55 @@ if ! TIME="$(printf '{ "command": ["get_property", "time-pos"] }\n' | socat - "$
exit 1 exit 1
fi fi
if [ "$(printf "%s" "$TIME" | jq -r .error)" = "success" ]; then if [ "$(printf "%s" "$TIME" | jq -r .error)" != "success" ]; then
while [ $# -gt 0 ]; do printf "Loading..."
exit 0
fi
while [ $# -gt 0 ]; do
COMMAND=$1 COMMAND=$1
VALID_COMMAND=true
case $COMMAND in case $COMMAND in
"time-pos" | "time-remaining" | "duration" | "media-title" | "playlist-pos" | "playlist-pos-1" | "playlist-count") "time-pos" | "time-remaining" | "duration" | "media-title" | "playlist-pos" | "playlist-pos-1" | "playlist-count" | "core-idle" | "play-pause-btn")
if [ "$COMMAND" = "play-pause-btn" ]; then
COMMAND="core-idle"
fi
API_OUTPUT=$(printf '{ "command": ["get_property", "%s"] }\n' "$COMMAND" | socat - "$MPV_SOCKET_PATH") 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 OUTPUT="$OUTPUT$COMMAND"
shift
continue
;; ;;
esac esac
if $VALID_COMMAND; then if [ "$(printf "%s" "$API_OUTPUT" | jq -r .error)" != "success" ]; then
if [ "$(printf "%s" "$API_OUTPUT" | jq -r .error)" = "success" ]; then printf "API error!"
exit 1
fi
JSON_DATA="$(printf "%s" "$API_OUTPUT" | jq -r .data)"
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="$(time_to_human "$(printf "%s" "$JSON_DATA" | cut -d'.' -f 1)")"
time_to_human
;; ;;
"media-title") "media-title")
API_OUTPUT=$(printf "%s" "$API_OUTPUT" | jq -r .data | cut -c 1-35) API_OUTPUT=$(printf "%s" "$JSON_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="$JSON_DATA"
;; ;;
"core-idle" | "play-pause-btn") "core-idle")
shift; shift
if [ "$(printf "%s" "$API_OUTPUT" | jq -r .data)" = "false" ]; then if [ "$JSON_DATA" = "false" ]; then
API_OUTPUT=$1 #Play icon API_OUTPUT=$1 #Play icon
shift; shift
else else
shift; shift
API_OUTPUT=$1 #Pause icon API_OUTPUT=$1 #Pause icon
fi fi
;; ;;
esac esac
else
API_OUTPUT="API error!"
fi
else
API_OUTPUT="$COMMAND"
fi
OUTPUT="$OUTPUT$API_OUTPUT" OUTPUT="$OUTPUT$API_OUTPUT"
shift; shift
done done
else
OUTPUT="Loading..."
fi
printf "%s" "$OUTPUT" printf "%s" "$OUTPUT"

Loading…
Cancel
Save