diff --git a/py3status/modules/playerctl.py b/py3status/modules/playerctl.py index e94c6a06b6..2fa1df6e83 100644 --- a/py3status/modules/playerctl.py +++ b/py3status/modules/playerctl.py @@ -129,7 +129,7 @@ class Meta: def post_config_hook(self): self.thresholds_init = self.py3.get_color_names_list(self.format_player) - self.placeholders = self.py3.get_placeholders_list(self.format_player) + self.replacements = self.py3.get_replacements_list(self.format_player) self.position = self.py3.format_contains(self.format_player, "position") self.cache_timeout = getattr(self, "cache_timeout", 1) @@ -294,7 +294,7 @@ def playerctl(self): cached_until = self.cache_timeout # Replace the values - for x in self.placeholders: + for x in self.replacements: if x in player_data: player_data[x] = self.py3.replace(player_data[x], x) diff --git a/py3status/modules/spotify.py b/py3status/modules/spotify.py index 3d9fdf533b..7d650a4b22 100644 --- a/py3status/modules/spotify.py +++ b/py3status/modules/spotify.py @@ -142,8 +142,8 @@ def _get_text(self): "playback": playback_status, } - # Replace the values - for x in self.placeholders: + # replace the values + for x in self.replacements: if x in spotify_data: spotify_data[x] = self.py3.replace(spotify_data[x], x) @@ -153,6 +153,7 @@ def _get_text(self): def post_config_hook(self): self.placeholders = self.py3.get_placeholders_list(self.format) + self.replacements = self.py3.get_replacements_list(self.format) def spotify(self): """ diff --git a/py3status/py3.py b/py3status/py3.py index b60a6e3f93..b0c069c338 100644 --- a/py3status/py3.py +++ b/py3status/py3.py @@ -727,6 +727,28 @@ def get_color_names_list(self, format_string, matches=None): found.add(name) return list(found) + def get_replacements_list(self, format_string): + """ + If possible, returns a list of filtered placeholders in ``format_string``. + """ + replacements = getattr(self._py3status_module, "replacements", None) + if not replacements or not format_string: + return [] + + if format_string not in self._format_placeholders: + placeholders = self._formatter.get_placeholders(format_string) + self._format_placeholders[format_string] = placeholders + else: + placeholders = self._format_placeholders[format_string] + + # filter placeholders + found = set() + for replacement in replacements: + for placeholder in placeholders: + if placeholder == replacement: + found.add(placeholder) + return list(found or placeholders) + def get_placeholders_list(self, format_string, matches=None): """ Returns a list of placeholders in ``format_string``.