Removed some code smell in polybar's mpv script and aligned it to POSIX compliancy removing the need of bash as a dependency

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

@ -1,62 +1,43 @@
#!/bin/bash #!/bin/sh
## 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:
## `mpv --input-ipc-server=~/.cache/mpvsocket Your.Video.File.mkv` # `mpv --input-ipc-server=~/.cache/mpvsocket Your.Video.File.mkv`
## 2) Add the line below when in the `mpv.conf` file: # 2) Add the line below when in the `mpv.conf` file:
## `input-ipc-server=~/.cache/mpvsocket` # `input-ipc-server=~/.cache/mpvsocket`
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 else
if [ $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 fi
} }
MPV_SOCKET_PATH="$HOME/.cache/mpvsocket"
OUTPUT="" OUTPUT=""
pgrep -u "$UID" -x mpv > /dev/null if pgrep -u "$(id -u)" -x mpv > /dev/null; then
if [ $? -eq 0 ]; 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
TIME=$(echo '{ "command": ["get_property", "time-pos"] }' | socat - ~/.cache/mpvsocket) TIME=$(echo '{ "command": ["get_property", "time-pos"] }' | socat - "$MPV_SOCKET_PATH")
if [[ $(echo $TIME | jq -r .error) == "success" ]]; then if [ "$(echo "$TIME" | jq -r .error)" = "success" ]; then
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
COMMAND=$1 COMMAND=$1
VALID_COMMAND=true VALID_COMMAND=true
case $COMMAND in case $COMMAND in
"time-pos") "time-pos" | "time-remaining" | "duration" | "media-title" | "playlist-pos" | "playlist-pos-1" | "playlist-count")
API_OUTPUT=$(echo '{ "command": ["get_property", "time-pos"] }' | socat - ~/.cache/mpvsocket) API_OUTPUT=$(echo "{ \"command\": [\"get_property\", \"$COMMAND\"] }" | socat - "$MPV_SOCKET_PATH")
;;
"time-remaining")
API_OUTPUT=$(echo '{ "command": ["get_property", "time-remaining"] }' | socat - ~/.cache/mpvsocket)
;;
"duration")
API_OUTPUT=$(echo '{ "command": ["get_property", "duration"] }' | socat - ~/.cache/mpvsocket)
;;
"media-title")
API_OUTPUT=$(echo '{ "command": ["get_property", "media-title"] }' | socat - ~/.cache/mpvsocket)
;;
"playlist-pos")
API_OUTPUT=$(echo '{ "command": ["get_property", "playlist-pos"] }' | socat - ~/.cache/mpvsocket)
;;
"playlist-pos-1")
API_OUTPUT=$(echo '{ "command": ["get_property", "playlist-pos-1"] }' | socat - ~/.cache/mpvsocket)
;;
"playlist-count")
API_OUTPUT=$(echo '{ "command": ["get_property", "playlist-count"] }' | socat - ~/.cache/mpvsocket)
;; ;;
"core-idle" | "play-pause-btn") "core-idle" | "play-pause-btn")
API_OUTPUT=$(echo '{ "command": ["get_property", "core-idle"] }' | socat - ~/.cache/mpvsocket) API_OUTPUT=$(echo '{ "command": ["get_property", "core-idle"] }' | socat - "$MPV_SOCKET_PATH")
;; ;;
*) *)
VALID_COMMAND=false VALID_COMMAND=false
@ -64,38 +45,21 @@ if [ $? -eq 0 ]; then
esac esac
if $VALID_COMMAND; then if $VALID_COMMAND; then
if [[ $(echo $API_OUTPUT | jq -r .error) == "success" ]]; then if [ "$(echo "$API_OUTPUT" | jq -r .error)" = "success" ]; then
case $COMMAND in case $COMMAND in
"time-pos") "time-pos" | "time-remaining" | "duration")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data | cut -d'.' -f 1) API_OUTPUT=$(echo "$API_OUTPUT" | jq -r .data | cut -d'.' -f 1)
time_to_human
;;
"time-remaining")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data | cut -d'.' -f 1)
time_to_human
;;
"duration")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data | cut -d'.' -f 1)
time_to_human time_to_human
;; ;;
"media-title") "media-title")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data | cut -c 1-35) API_OUTPUT=$(echo "$API_OUTPUT" | jq -r .data | cut -c 1-35)
;; ;;
"playlist-pos") "playlist-pos" | "playlist-pos-1" | "playlist-count")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data) API_OUTPUT=$(echo "$API_OUTPUT" | jq -r .data)
API_OUTPUT="$API_OUTPUT"
;; ;;
"playlist-pos-1") "core-idle" | "play-pause-btn")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data)
API_OUTPUT="$API_OUTPUT"
;;
"playlist-count")
API_OUTPUT=$(echo $API_OUTPUT | jq -r .data)
API_OUTPUT="$API_OUTPUT"
;;
"core-idle"| "play-pause-btn")
shift; shift;
if [[ $(echo $API_OUTPUT | jq -r .data) == "false" ]]; then if [ "$(echo "$API_OUTPUT" | jq -r .data)" = "false" ]; then
API_OUTPUT=$1 #Play icon API_OUTPUT=$1 #Play icon
shift; shift;
else else
@ -121,4 +85,4 @@ else
exit exit
fi fi
printf "$OUTPUT" printf %s "$OUTPUT"

Loading…
Cancel
Save