Refactor usage of qtile object and improvements to media widget

This commit is contained in:
Kevin Alberts 2021-11-22 20:47:16 +01:00
parent 8f4f08e3bf
commit 0f4ef9190a
Signed by: Kurocon
GPG key ID: BCD496FEBA0C6BC1
4 changed files with 62 additions and 36 deletions

View file

@ -260,6 +260,7 @@ class MediaWidget(base.InLoopPollText):
('on_text_pause', '{}', 'The pattern for the text if music is paused.'),
('on_text_stop', '{}', 'The pattern for the text if music is stopped.'),
('update_interval', 1, 'The update interval.'),
('max_chars_per_player', 50, 'Maximum characters of text per player.'),
]
player_icons = {
@ -333,7 +334,7 @@ class MediaWidget(base.InLoopPollText):
def cmd_update_custom_player(self, player_name, data):
# Update firefox player
if player_name == "firefox":
if player_name.startswith("firefox"):
if data['playing'] and data['muted']:
self.custom_player_data['firefox']['showing'] = True
self.custom_player_data['firefox']['state'] = MediaWidget.Status.PAUSED
@ -355,8 +356,11 @@ class MediaWidget(base.InLoopPollText):
players = []
# Playerctl players
command = ["playerctl", "-l"]
result = self.call_process(command)
try:
result = self.call_process(["playerctl", "-l"])
except subprocess.CalledProcessError:
result = None
if result:
players.extend([x for x in result.split("\n") if x])
@ -398,8 +402,14 @@ class MediaWidget(base.InLoopPollText):
text = "Unknown"
if cmd_result in ["Playing", "Paused"]:
artist = self.call_process(['playerctl', '-p', player, 'metadata', 'artist']).strip()
title = self.call_process(['playerctl', '-p', player, 'metadata', 'title']).strip()[:50]
try:
artist = self.call_process(['playerctl', '-p', player, 'metadata', 'artist']).strip()
except subprocess.CalledProcessError:
artist = None
try:
title = self.call_process(['playerctl', '-p', player, 'metadata', 'title']).strip()
except subprocess.CalledProcessError:
title = None
if artist and title:
text = "{} - {}".format(artist, title)
@ -419,13 +429,17 @@ class MediaWidget(base.InLoopPollText):
def _get_formatted_text(self, status):
if status[0] == MediaWidget.Status.PLAYING:
return self.on_text_play.format(status[1])
res = self.on_text_play.format(status[1])
elif status[0] == MediaWidget.Status.PAUSED:
return self.on_text_pause.format(status[1])
res = self.on_text_pause.format(status[1])
elif status[0] == MediaWidget.Status.STOPPED:
return self.on_text_stop.format(status[1])
res = self.on_text_stop.format(status[1])
else:
return "Unknown"
res = "Unknown"
res = pangocffi.markup_escape_text(res)
if len(res) > self.max_chars_per_player:
res = res[:self.max_chars_per_player] + "..."
return res
def draw(self):
super(MediaWidget, self).draw()
@ -437,7 +451,12 @@ class MediaWidget(base.InLoopPollText):
return self.off_text
else:
for player in status.keys():
icon = self.player_icons.get(player, player)
# Shorten firefox.instance[0-9]+ to just firefox for icon finding
if player.startswith("firefox"):
player_icon = "firefox"
else:
player_icon = player
icon = self.player_icons.get(player_icon, player_icon)
text.append("{} {}".format(icon, self._get_formatted_text(status[player])))
return " | ".join(text) if text else self.off_text