PrayagS / polybar-spotify

:musical_note: Spotify status and controls module for Polybar with text scrolling
MIT License
581 stars 38 forks source link

Is it possible to not output "No players found" and instead output a space? #18

Closed diogofd8 closed 2 years ago

diogofd8 commented 2 years ago

I tried adding

if [ "$STATUS" = "No players found" ]; then
    echo " "
fi

to the end of your script get_spotify_status.sh but it will always display "No players found" everytime I have the spotify client closed. image

This is the goal.

PrayagS commented 2 years ago

The change that you made in the script is correct. However, make sure that the player being tracked is only spotify.

So find this line in the script,

PLAYER="spotify"

and make sure that it is correctly set to spotify. It might be set to playerctld which tracks all the running players, so even if you close Spotify, it will switch over to tracking Chrome for example.

Moreover, when you close Spotify, make sure that it is closed and not just minimized to the tray. I tried the same change that you made and it does show up as blank.

Let me know how it goes for you. Feel free to reach out if the issue still persists.

diogofd8 commented 2 years ago
#!/bin/bash

# The name of polybar bar which houses the main spotify module and the control modules.
PARENT_BAR="mybar"
PARENT_BAR_PID=$(pgrep -a "polybar" | grep "$PARENT_BAR" | cut -d" " -f1)

# Set the source audio player here.
# Players supporting the MPRIS spec are supported.
# Examples: spotify, vlc, chrome, mpv and others.
# Use `playerctld` to always detect the latest player.
# See more here: https://github.com/altdesktop/playerctl/#selecting-players-to-control
PLAYER="spotify"

# Format of the information displayed
# Eg. {{ artist }} - {{ album }} - {{ title }}
# See more attributes here: https://github.com/altdesktop/playerctl/#printing-properties-and-metadata
FORMAT="{{ title }} - {{ artist }}"

# Sends $2 as message to all polybar PIDs that are part of $1
update_hooks() {
    while IFS= read -r id
    do
        polybar-msg -p "$id" hook spotify-play-pause $2 1>/dev/null 2>&1
    done < <(echo "$1")
}

PLAYERCTL_STATUS=$(playerctl --player=$PLAYER status 2>/dev/null)
EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
    STATUS=$PLAYERCTL_STATUS
else
    STATUS="No player is running"
fi

if [ "$1" == "--status" ]; then
    echo "$STATUS"
else
    if [ "$STATUS" = "Stopped" ]; then
        echo "No music is playing"
    elif [ "$STATUS" = "Paused"  ]; then
        update_hooks "$PARENT_BAR_PID" 2
        playerctl --player=$PLAYER metadata --format "$FORMAT"
    elif [ "$STATUS" = "No player is running"  ]; then
        echo "$STATUS"
    else
        update_hooks "$PARENT_BAR_PID" 1
        playerctl --player=$PLAYER metadata --format "$FORMAT"
    fi
fi

if [ "$STATUS" = "No players found" ]; then
    echo "none"
fi

This is the code. If I execute the scroll script (unchanged, copied from this repo), it will print "No players found" on a cycle.

PrayagS commented 2 years ago

Can you try this again with the following script?

#!/bin/bash

# The name of polybar bar which houses the main spotify module and the control modules.
PARENT_BAR="mybar"
PARENT_BAR_PID=$(pgrep -a "polybar" | grep "$PARENT_BAR" | cut -d" " -f1)

# Set the source audio player here.
# Players supporting the MPRIS spec are supported.
# Examples: spotify, vlc, chrome, mpv and others.
# Use `playerctld` to always detect the latest player.
# See more here: https://github.com/altdesktop/playerctl/#selecting-players-to-control
PLAYER="spotify"

# Format of the information displayed
# Eg. {{ artist }} - {{ album }} - {{ title }}
# See more attributes here: https://github.com/altdesktop/playerctl/#printing-properties-and-metadata
FORMAT="{{ title }} - {{ artist }}"

# Sends $2 as message to all polybar PIDs that are part of $1
update_hooks() {
    while IFS= read -r id
    do
        polybar-msg -p "$id" hook spotify-play-pause $2 1>/dev/null 2>&1
    done < <(echo "$1")
}

PLAYERCTL_STATUS=$(playerctl --player=$PLAYER status 2>/dev/null)
EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
    STATUS=$PLAYERCTL_STATUS
else
    STATUS="No player is running"
fi

if [ "$1" == "--status" ]; then
    echo "$STATUS"
else
    if [ "$STATUS" = "Stopped" ]; then
        echo "No music is playing"
    elif [ "$STATUS" = "Paused"  ]; then
        update_hooks "$PARENT_BAR_PID" 2
        playerctl --player=$PLAYER metadata --format "$FORMAT"
    elif [ "$STATUS" = "No player is running"  ]; then
        echo ""
    else
        update_hooks "$PARENT_BAR_PID" 1
        playerctl --player=$PLAYER metadata --format "$FORMAT"
    fi
fi

Works as intended on my system.

diogofd8 commented 2 years ago

For some reason it doesn't on mine. I tried recreating the script on python, a language I'm way more comfortable with and the problem persists. The issue is that when there is no client open it won't say "Stopped" or "Paused" or "Playing", it will say "No players found". That output has a different behaviour, maybe because it is an error code. Executing the command will create that output and then the rest of the script runs. Kinda weird... I don't know what is different about my system that creates this issue.

PrayagS commented 2 years ago

I think you need to debug the script directly and see exactly what's the issue.

If there is no running player found, this is how the script goes about it,

PLAYERCTL_STATUS=$(playerctl --player=$PLAYER status 2>/dev/null)
EXIT_CODE=$?

At this point, PLAYERCTL_STATUS will be null since playerctl returns the message on stderr which in this case is redirected to null so that it doesn't show up on the bar. And the exit code is 1.

if [ $EXIT_CODE -eq 0 ]; then
    STATUS=$PLAYERCTL_STATUS
else
    STATUS="No player is running"
fi

Because the exit code is 1, this block will set STATUS to "No player is running".

elif [ "$STATUS" = "No player is running"  ]; then
        echo ""

And at last, this block echoes the blank output required.

I suggest you place a few echo statements and debug the script to make sure that the variables are having proper values in each case.

diogofd8 commented 2 years ago

image

This is what happens. You are right, STATUS is null there even when the output is "No players found". However, the output of the next command to export the metadata is echo'd on the script without any echo. Can u tell me what is the version of your playerctl?

diogofd8 commented 2 years ago

Yep, it was the playerctl version. I've updated it through their git to the latest version and your script works perfectly. My python version also started working properly.