From 51f68addcdb3dbf266a89c1510935ad230eacb5a Mon Sep 17 00:00:00 2001 From: Yufan You Date: Fri, 11 Jun 2021 17:34:46 +0800 Subject: [PATCH] [modules/playerctl]: BREAKING: use `playerctl -f` and add `playerctl.args` 1. Use `playerctl -f` to format, which is more powerful. This also fixes #767, which is caused by missing a few fields of the metadata. 2. Add `playerctl.args`, so that users can choose a specific player, etc. 3. Display nothing when there's no running player. This is a breaking change. Users need to change `{title}` to `{{title}}`. --- bumblebee_status/modules/contrib/playerctl.py | 65 +++++++++---------- docs/modules.rst | 8 ++- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/bumblebee_status/modules/contrib/playerctl.py b/bumblebee_status/modules/contrib/playerctl.py index 3a0d7e5..d370df4 100755 --- a/bumblebee_status/modules/contrib/playerctl.py +++ b/bumblebee_status/modules/contrib/playerctl.py @@ -6,12 +6,14 @@ Requires the following executable: * playerctl Parameters: - * playerctl.format: Format string (defaults to '{artist} - {title}') - Available values are: {album}, {title}, {artist}, {trackNumber} + * playerctl.format: Format string (defaults to '{{artist}} - {{title}} {{duration(position)}}/{{duration(mpris:length)}}') + The format string is passed to 'playerctl -f' as an argument. See the 'Format Strings' section of 'man playerctl' for more information. * playerctl.layout: Comma-separated list to change order of widgets (defaults to song, previous, pause, next) Widget names are: playerctl.song, playerctl.prev, playerctl.pause, playerctl.next + * playerctl.args: The arguments added to playerctl. + You can check 'playerctl --help' or `its README `_. For example, it could be '-p vlc,%any'. -Parameters are inherited from `spotify` module, many thanks to its developers! +Parameters are inspired by the `spotify` module, many thanks to its developers! contributed by `smitajit `_ - many thanks! """ @@ -36,9 +38,8 @@ class Module(core.module.Module): ) ) - self.__song = "" - self.__cmd = "playerctl " - self.__format = self.parameter("format", "{artist} - {title}") + self.__cmd = "playerctl " + self.parameter("args", "") + " " + self.__format = self.parameter("format", "{{artist}} - {{title}} {{duration(position)}}/{{duration(mpris:length)}}") widget_map = {} for widget_name in self.__layout: @@ -48,7 +49,6 @@ class Module(core.module.Module): "button": core.input.LEFT_MOUSE, "cmd": self.__cmd + "previous", } - widget.set("state", "prev") elif widget_name == "playerctl.pause": widget_map[widget] = { "button": core.input.LEFT_MOUSE, @@ -59,7 +59,6 @@ class Module(core.module.Module): "button": core.input.LEFT_MOUSE, "cmd": self.__cmd + "next", } - widget.set("state", "next") elif widget_name == "playerctl.song": widget_map[widget] = [ { @@ -86,32 +85,32 @@ class Module(core.module.Module): def update(self): try: - self.__get_song() - - for widget in self.widgets(): - if widget.name == "playerctl.pause": - playback_status = str(util.cli.execute(self.__cmd + "status")).strip() - if playback_status != "": - if playback_status == "Playing": - widget.set("state", "playing") - else: - widget.set("state", "paused") - elif widget.name == "playerctl.song": - widget.set("state", "song") - widget.full_text(self.__song) + playback_status = str(util.cli.execute(self.__cmd + "status")).strip() except Exception as e: logging.exception(e) - self.__song = "" + playback_status = None + for widget in self.widgets(): + if playback_status: + if widget.name == "playerctl.pause": + if playback_status == "Playing": + widget.set("state", "playing") + elif playback_status == "Paused": + widget.set("state", "paused") + else: + widget.set("state", "") + elif widget.name == "playerctl.next": + widget.set("state", "next") + elif widget.name == "playerctl.prev": + widget.set("state", "prev") + elif widget.name == "playerctl.song": + widget.full_text(self.__get_song()) + else: + widget.set("state", "") + widget.full_text(" ") def __get_song(self): - album = str(util.cli.execute(self.__cmd + "metadata xesam:album")).strip() - title = str(util.cli.execute(self.__cmd + "metadata xesam:title")).strip() - artist = str(util.cli.execute(self.__cmd + "metadata xesam:albumArtist")).strip() - track_number = str(util.cli.execute(self.__cmd + "metadata xesam:trackNumber")).strip() - - self.__song = self.__format.format( - album = album, - title = title, - artist = artist, - trackNumber = track_number - ) + try: + return str(util.cli.execute(self.__cmd + "metadata -f '" + self.__format + "'")).strip() + except Exception as e: + logging.exception(e) + return " " diff --git a/docs/modules.rst b/docs/modules.rst index 0e166e6..25952d8 100644 --- a/docs/modules.rst +++ b/docs/modules.rst @@ -1060,12 +1060,14 @@ Requires the following executable: * playerctl Parameters: - * playerctl.format: Format string (defaults to '{artist} - {title}') - Available values are: {album}, {title}, {artist}, {trackNumber} + * playerctl.format: Format string (defaults to '{{artist}} - {{title}} {{duration(position)}}/{{duration(mpris:length)}}') + The format string is passed to 'playerctl -f' as an argument. See the 'Format Strings' section of 'man playerctl' for more information. * playerctl.layout: Comma-separated list to change order of widgets (defaults to song, previous, pause, next) Widget names are: playerctl.song, playerctl.prev, playerctl.pause, playerctl.next + * playerctl.args: The arguments added to playerctl. + You can check 'playerctl --help' or `its readme `_. For example, it could be '-p vlc,%any'. -Parameters are inherited from `spotify` module, many thanks to its developers! +Parameters are inspired by the `spotify` module, many thanks to its developers! contributed by `smitajit `_ - many thanks!