My OCD Struck Again: Fixing a Stray Hyphen in My Status Bar

(edited)

Hey everyone,

You know those tiny, insignificant things that just get under your skin and drive you insane? Well, my OCD struck again.
Wikimedia Commons
The other day, I explained how I have my SiriusXM setup configured to play through MPD. I wanted to see the "now playing" status in my Qtile bar, and I decided to use the Mpris2 widget, instead of just using the mpd widget. This is great because it picks up the status from any media player that uses the standard DBus interface, whether it's MPD, Spotify, or even MPV.

I went with the classic setup: {artist} - {title}.

The problem is, when I'm listening to one of my SiriusXM streams, the song information isn't available in the stream's metadata. (Which is why I had a hard time getting the now-playing feature in the sxm-client lib to work)

When the title field is empty, my bar was showing a stray hyphen.

Like this:

with.png

I was stuck looking at that stray -, and it was driving me crazy.


The Fix: A Custom Widget

So, I wrote a custom version of the Mpris2 widget that dynamically changes the output format if a piece of metadata is missing.

from libqtile.widget import Mpris2


class CustomMpris2(Mpris2):
    """
    A custom Mpris2 widget that dynamically hides the artist/title and
    separator when artist or title metadata is missing.
    """

    def get_track_info(self, metadata) -> str:
        # First, populate `self.metadata` just as the parent class does.
        super().get_track_info(metadata)

        # Now, apply the conditional formatting logic.
        format_string = self.format

        # Check if the artist or title field is missing or empty.
        has_artist = self.metadata.get("xesam:artist")
        has_title = self.metadata.get("xesam:title")

        if not has_artist and has_title:
            format_string = "{xesam:title}"
        elif not has_title and has_artist:
            format_string = "{xesam:artist}"

        # Format the string using the selected format.
        track = self._formatter.format(format_string, **self.metadata)
        return track.replace("\n", "")

This simple subclass checks if the artist or title is missing and, if so, it switches to a format string that only shows the data that's actually there. No more stray hyphens.

without.png

So much better.

The widget is available in my custom Qtile widgets repository on GitHub:

This is a perfect example of exactly why I love using a desktop window manager that is so easily hackable. I can fix the tiny little things that annoy me.

As always,
Michael Garcia a.k.a. TheCrazyGM

0.10587386 BEE
2 comments

ODC ? I call it CDO. So much tidier if the letters are in the right order 😜

0.00024948 BEE

I very much understand, and you make a good case for tiling window managers. I love being able to tweak everything. 😁🙏💚✨🤙

0.00024704 BEE