diff --git a/polybar/.config/polybar/scripts/mpv.sh b/polybar/.config/polybar/scripts/mpv.sh index 5010d7a..29bedcd 100755 --- a/polybar/.config/polybar/scripts/mpv.sh +++ b/polybar/.config/polybar/scripts/mpv.sh @@ -1,5 +1,6 @@ #!/bin/sh +# Dependencies: mpv jq socat # Note: In order to use this script you have to tell to mpv to enable a socket. # Two ways are possible to achieve this: # 1) Use the argument --input-ipc-server when you launch it. For example: @@ -8,17 +9,18 @@ # `input-ipc-server=mpvsocket` 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 - API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60))) - fi + seconds=$1 + if [ "$seconds" -gt 3600 ]; then + printf '%d:%02d:%02d' $((seconds/3600)) $((seconds%3600/60)) $((seconds%60)) + elif [ "$seconds" -gt 60 ]; then + printf '%02d:%02d' $((seconds%3600/60)) $((seconds%60)) + else + printf '%02d' $((seconds%60)) + fi } if ! pgrep -u "$(id -u)" -x mpv > /dev/null; then - exit 1 + exit 1 fi MPV_SOCKET_PATH="${XDG_RUNTIME_DIR:-/tmp/$(id -u)-runtime}/mpv/main.sock" @@ -28,60 +30,58 @@ OUTPUT="" # 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 + 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 $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; - else - shift; - API_OUTPUT=$1 #Pause icon - fi - ;; - esac - else - API_OUTPUT="API error!" - fi - else - API_OUTPUT="$COMMAND" - fi - OUTPUT="$OUTPUT$API_OUTPUT" - shift; - done -else - OUTPUT="Loading..." +if [ "$(printf "%s" "$TIME" | jq -r .error)" != "success" ]; then + printf "Loading..." + exit 0 fi +while [ $# -gt 0 ]; do + COMMAND=$1 + case $COMMAND in + "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") + ;; + *) + OUTPUT="$OUTPUT$COMMAND" + shift + continue + ;; + esac + + 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 + "time-pos" | "time-remaining" | "duration") + API_OUTPUT="$(time_to_human "$(printf "%s" "$JSON_DATA" | cut -d'.' -f 1)")" + ;; + "media-title") + API_OUTPUT=$(printf "%s" "$JSON_DATA" | cut -c 1-35) + ;; + "playlist-pos" | "playlist-pos-1" | "playlist-count") + API_OUTPUT="$JSON_DATA" + ;; + "core-idle") + shift + if [ "$JSON_DATA" = "false" ]; then + API_OUTPUT=$1 #Play icon + shift + else + shift + API_OUTPUT=$1 #Pause icon + fi + ;; + esac + OUTPUT="$OUTPUT$API_OUTPUT" + shift +done + printf "%s" "$OUTPUT"