Skip to content

Commit

Permalink
py3: add new helper: get_replacements_list
Browse files Browse the repository at this point in the history
  • Loading branch information
lasers committed Apr 15, 2024
1 parent 835e047 commit 04f55e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions py3status/modules/playerctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions py3status/modules/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
"""
Expand Down
22 changes: 22 additions & 0 deletions py3status/py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down

0 comments on commit 04f55e3

Please sign in to comment.