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

Loading…
Cancel
Save