Skip to content

Commit

Permalink
cmus, deadbeef, moc: add self.py3.replace helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lasers committed Dec 23, 2024
1 parent bd2c92e commit 9dd2f21
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
35 changes: 21 additions & 14 deletions py3status/modules/cmus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*(default '[\?if=is_started [\?if=is_playing > ][\?if=is_paused \|\| ]'
'[\?if=is_stopped .. ][[{artist}][\?soft - ][{title}]'
'|\?show cmus: waiting for user input]]')*
replacements: specify a list/dict of string placeholders to modify (default None)
sleep_timeout: sleep interval for this module. when cmus is not running,
this interval will be used. this allows some flexible timing where one
might want to refresh constantly with some placeholders... or to refresh
Expand Down Expand Up @@ -100,6 +101,7 @@ class Py3status:
r"[\?if=is_stopped .. ][[{artist}][\?soft - ][{title}]"
r"|\?show cmus: waiting for user input]]"
)
replacements = None
sleep_timeout = 20

def post_config_hook(self):
Expand All @@ -109,6 +111,7 @@ def post_config_hook(self):
self.color_stopped = self.py3.COLOR_STOPPED or self.py3.COLOR_BAD
self.color_paused = self.py3.COLOR_PAUSED or self.py3.COLOR_DEGRADED
self.color_playing = self.py3.COLOR_PLAYING or self.py3.COLOR_GOOD
self.replacements_init = self.py3.get_replacements_list(self.format)

def _seconds_to_time(self, value):
m, s = divmod(int(value), 60)
Expand Down Expand Up @@ -165,14 +168,14 @@ def cmus(self):
cached_until = self.sleep_timeout
color = self.py3.COLOR_BAD

is_started, data = self._get_cmus_data()
is_started, cmus_data = self._get_cmus_data()

if is_started:
cached_until = self.cache_timeout
data = self._organize_data(data)
data = self._manipulate_data(data)
cmus_data = self._organize_data(cmus_data)
cmus_data = self._manipulate_data(cmus_data)

status = data.get("status")
status = cmus_data.get("status")
if status == "playing":
is_playing = True
color = self.color_playing
Expand All @@ -183,19 +186,23 @@ def cmus(self):
is_stopped = True
color = self.color_stopped

for x in self.replacements_init:
if x in cmus_data:
cmus_data[x] = self.py3.replace(cmus_data[x], x)

cmus_data.update(
{
"is_paused": is_paused,
"is_playing": is_playing,
"is_started": is_started,
"is_stopped": is_stopped,
}
)

return {
"cached_until": self.py3.time_in(cached_until),
"color": color,
"full_text": self.py3.safe_format(
self.format,
dict(
is_paused=is_paused,
is_playing=is_playing,
is_started=is_started,
is_stopped=is_stopped,
**data,
),
),
"full_text": self.py3.safe_format(self.format, cmus_data),
}

def on_click(self, event):
Expand Down
7 changes: 7 additions & 0 deletions py3status/modules/deadbeef.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Configuration parameters:
cache_timeout: refresh interval for this module (default 5)
format: display format for this module (default '[{artist} - ][{title}]')
replacements: specify a list/dict of string placeholders to modify (default None)
sleep_timeout: when deadbeef is not running, this interval will be used
to allow faster refreshes with time-related placeholders and/or
to refresh few times per minute rather than every few seconds
Expand Down Expand Up @@ -58,6 +59,7 @@ class Py3status:
# available configuration parameters
cache_timeout = 5
format = "[{artist} - ][{title}]"
replacements = None
sleep_timeout = 20

class Meta:
Expand Down Expand Up @@ -89,6 +91,7 @@ def post_config_hook(self):
self.color_paused = self.py3.COLOR_PAUSED or self.py3.COLOR_DEGRADED
self.color_playing = self.py3.COLOR_PLAYING or self.py3.COLOR_GOOD
self.color_stopped = self.py3.COLOR_STOPPED or self.py3.COLOR_BAD
self.replacements_init = self.py3.get_replacements_list(self.format)

def _is_running(self):
try:
Expand Down Expand Up @@ -124,6 +127,10 @@ def deadbeef(self):
else:
color = self.color_paused

for x in self.replacements_init:
if x in beef_data:
beef_data[x] = self.py3.replace(beef_data[x], x)

return {
"cached_until": self.py3.time_in(cached_until),
"full_text": self.py3.safe_format(self.format, beef_data),
Expand Down
27 changes: 17 additions & 10 deletions py3status/modules/moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
format: display format for this module
*(default '\?if=is_started [\?if=is_stopped \[\] moc|'
'[\?if=is_paused \|\|][\?if=is_playing >] {title}]')*
replacements: specify a list/dict of string placeholders to modify (default None)
sleep_timeout: when moc is not running, this interval will be used to
allow one to refresh constantly with time placeholders and/or
to refresh once every minute rather than every few seconds
Expand Down Expand Up @@ -88,6 +89,7 @@ class Py3status:
r"\?if=is_started [\?if=is_stopped \[\] moc|"
r"[\?if=is_paused \|\|][\?if=is_playing >] {title}]"
)
replacements = None
sleep_timeout = 20

def post_config_hook(self):
Expand All @@ -97,6 +99,7 @@ def post_config_hook(self):
self.color_stopped = self.py3.COLOR_STOPPED or self.py3.COLOR_BAD
self.color_paused = self.py3.COLOR_PAUSED or self.py3.COLOR_DEGRADED
self.color_playing = self.py3.COLOR_PLAYING or self.py3.COLOR_GOOD
self.replacements_init = self.py3.get_replacements_list(self.format)

def _get_moc_data(self):
try:
Expand Down Expand Up @@ -133,19 +136,23 @@ def moc(self):
is_stopped = True
color = self.color_stopped

for x in self.replacements_init:
if x in moc_data:
moc_data[x] = self.py3.replace(moc_data[x], x)

moc_data.update(
{
"is_paused": is_paused,
"is_playing": is_playing,
"is_started": is_started,
"is_stopped": is_stopped,
}
)

return {
"cached_until": self.py3.time_in(cached_until),
"color": color,
"full_text": self.py3.safe_format(
self.format,
dict(
is_paused=is_paused,
is_playing=is_playing,
is_started=is_started,
is_stopped=is_stopped,
**data,
),
),
"full_text": self.py3.safe_format(self.format, moc_data),
}

def on_click(self, event):
Expand Down

0 comments on commit 9dd2f21

Please sign in to comment.