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. 17
      polybar/.config/polybar/scripts/mpv.sh

@ -10,26 +10,27 @@
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)))
else elif [ "$API_OUTPUT" -gt 60 ]; then
if [ "$API_OUTPUT" -gt 60 ]; then
API_OUTPUT=$(printf '%02d:%02d' $((API_OUTPUT%3600/60)) $((API_OUTPUT%60))) API_OUTPUT=$(printf '%02d:%02d' $((API_OUTPUT%3600/60)) $((API_OUTPUT%60)))
else else
API_OUTPUT=$(printf '%02d' $((API_OUTPUT%60))) 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 # 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 # 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 if ! TIME="$(printf '{ "command": ["get_property", "time-pos"] }\n' | socat - "$MPV_SOCKET_PATH" 2> /dev/null)"; then
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 while [ $# -gt 0 ]; do
COMMAND=$1 COMMAND=$1
@ -79,12 +80,8 @@ if pgrep -u "$(id -u)" -x mpv > /dev/null; then
OUTPUT="$OUTPUT$API_OUTPUT" OUTPUT="$OUTPUT$API_OUTPUT"
shift; shift;
done done
else else
OUTPUT="Loading..." OUTPUT="Loading..."
fi fi
else
exit
fi
printf %s "$OUTPUT" printf "%s" "$OUTPUT"

Loading…
Cancel
Save