[modules/cmus] Make displayed string configurable

Allow the user to use all tags read by cmus (cmus-remote -Q|grep ^tag)
as part of the displayed data (plus the special 'tags' "duration" and
"position").
This commit is contained in:
Tobi-wan Kenobi 2016-11-12 08:34:21 +01:00
parent 507e0c36cc
commit 418dc1be86
2 changed files with 48 additions and 32 deletions

View file

@ -1,47 +1,56 @@
import string
import datetime
import bumblebee.module
import subprocess
from collections import defaultdict
import bumblebee.util
import bumblebee.module
def description():
return "Displays the current song and artist playing in cmus"
def parameters():
return [
"cmus.format: Format of the displayed song information, arbitrary tags (as available from cmus-remote -Q) can be used (defaults to {artist} - {title} {position}/{duration})"
]
class Module(bumblebee.module.Module):
def __init__(self, output, config, alias):
super(Module, self).__init__(output, config, alias)
self._title = "None"
self._artist = "None"
self._status = 0
self._status = "default"
self._fmt = self._config.parameter("format", "{artist} - {title} {position}/{duration}")
def _loadsong(self):
process = subprocess.Popen(["cmus-remote", "-Q"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self._query, self._error = process.communicate()
self._query = self._query.decode("utf-8").split("\n")
self._status = "default"
def _tags(self):
tags = defaultdict(lambda: '')
for line in self._query:
if line.startswith("status"):
ignore, status = line.split(" ", 2)
self._status = status
if line.startswith("tag"):
ignore, key, value = line.split(" ", 2)
tags.update({ key: value })
if line.startswith("duration"):
ignore, sec = line.split(" ")
tags.update({ "duration": bumblebee.util.durationfmt(int(sec)) })
if line.startswith("position"):
ignore, sec = line.split(" ")
tags.update({ "position": bumblebee.util.durationfmt(int(sec)) })
return tags
def widgets(self):
self._process = subprocess.Popen(["cmus-remote", "-Q"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self._query, self._error = self._process.communicate()
self._query = self._query.decode("utf-8").split("\n")
if b'cmus is not running' in self._error:
return bumblebee.output.Widget(self, "-")
for line in self._query:
if "status playing" in line:
self._status = 1
if "status paused" in line:
self._status = 2
if "status stopped" in line:
self._status = 3
else:
if "tag title" in line:
self._title = line[10:]
if "tag artist" in line:
self._artist = line[11:]
self._loadsong()
tags = self._tags()
return bumblebee.output.Widget(self, string.Formatter().vformat(self._fmt, (), tags))
return bumblebee.output.Widget(self, "{} - {}".format(
self._artist,
self._title)
)
def state(self, widget):
if self._status == 1:
return "playing"
if self._status == 2:
return "paused"
if self._status == 3:
return "stopped"
return "default"
return self._status
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -6,3 +6,10 @@ def bytefmt(num):
num /= 1024.0
return "{:05.2f%}{}GiB".format(num)
def durationfmt(duration):
minutes, seconds = divmod(duration, 60)
hours, minutes = divmod(minutes, 60)
res = "{:02d}:{:02d}".format(minutes, seconds)
if hours > 0: res = "{:02d}:{}".format(hours, res)
return res