Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with titles or other strings containing special characters (" or ') #2

Open
k0ruy opened this issue Sep 1, 2023 · 0 comments

Comments

@k0ruy
Copy link

k0ruy commented Sep 1, 2023

With the original code, inside spotify-panel.sh, when sp.sh eval is called with the eval bash function, the title or album may not be displayed because of quotes that end the string unexpectedly. For this, I changed how the metadata is parsed inside spotify-panel.sh, adding a function and changing how the title is trimmed.

#!/usr/bin/env bash

readonly DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
declare -A SPOTIFY_METADATA

# ... existing code (encode function)

function fetch_spotify_metadata {
	while read -r line; do
		key="${line%%=*}"
		value="${line#*=}"
		value="${value%\"}"
		value="${value#\"}"
		SPOTIFY_METADATA["$key"]="$value"
	done < <( ${DIR}/sp.sh eval )
}

if pidof spotify &> /dev/null; then
	# call this function to populate SPOTIFY_METADATA
	fetch_spotify_metadata

	SPOTIFY_TITLE=${SPOTIFY_METADATA["SPOTIFY_TITLE"]}
	SPOTIFY_ARTIST=${SPOTIFY_METADATA["SPOTIFY_ARTIST"]}
	SPOTIFY_ALBUM=${SPOTIFY_METADATA["SPOTIFY_ALBUM"]}

    	# grab window id 
	WINDOW_ID=$(wmctrl -l | grep "${SPOTIFY_ARTIST} - ${SPOTIFY_TITLE}\|Spotify" | awk '{print $1}')
    
	# trim the title
	MAX_LENGTH=20
	DISPLAY_TITLE=$(python3 -c "import sys; title=sys.argv[1]; print(title[:$MAX_LENGTH] + '...' if len(title) > $MAX_LENGTH else title)" "$SPOTIFY_TITLE")

    echo "<img>${ICON}</img>"
    # ...rest of code 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant