Waybar icon indicating copy to clipboard operation
Waybar copied to clipboard

modules/custom: Allow json fields as format specifiers

Open rad4day opened this issue 2 years ago • 2 comments

This PR changes the custom module in a (hopefully) backwards compatible way to allow using JSON fields as format specifiers.

As #404 and #676 have both been closed as non-working and I reeeaaally wanted this feature I spent a bit of time trying to figure out how to implement it. That's what I've come up with. There are probably some weird bugs caused by this code. Disclaimer: I am not a C++ developer.

For example

    "custom/spotify": {
        "format-alt": "{status_icon}",
        "format-alt-click": "click-middle",
        "exec": "/usr/bin/python3 ~/.config/waybar/scripts/mediaplayer.py --player spotify",
        "format": "{status_icon} {artist} - {title}",
        "return-type": "json",
        "escape": true,
    },

which currently outputs the following JSON as a single line, where text is only provided as a fallback and not necessary.

{
  "player_name": "spotify",
  "player_icon": "\uf1bc",
  "artist": "Demoartist",
  "title": "totally a real song",
  "status": "Playing",
  "status_icon": "\uf144",
  "class": [
    "spotify",
    "playing"
  ],
  "text": "Demoartist - totally a real song"
}

What has been tested:

  • classes still work
  • alt format works
  • tooltips work

Percentage Icons are currently untested as I don't have a use case for those. I'd enjoy if somebody could test if they still work or provide a working example.

resolves #432

rad4day avatar Aug 30 '23 06:08 rad4day

I'm really interested, this feature could open the doors to printing different "format" outputs like format-tooltip or format-alt in the future, isn't it?

alohl669 avatar Jan 19 '24 20:01 alohl669

@alohl669 With a small change to L140 (adding the formatting engine there) this should be possible I think.

rad4day avatar Jan 23 '24 22:01 rad4day