2016-12-10 07:47:24 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2016-12-10 12:03:58 +01:00
|
|
|
"""Displays information about the current song in cmus.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* cmus.format: Format string for the song information. Tag values can be put in curly brackets (i.e. {artist})
|
|
|
|
"""
|
2016-12-10 07:47:24 +01:00
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
import string
|
|
|
|
|
|
|
|
import bumblebee.util
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
widgets = [
|
|
|
|
bumblebee.output.Widget(name="cmus.prev"),
|
|
|
|
bumblebee.output.Widget(name="cmus.main", full_text=self.description),
|
|
|
|
bumblebee.output.Widget(name="cmus.next"),
|
|
|
|
bumblebee.output.Widget(name="cmus.shuffle"),
|
|
|
|
bumblebee.output.Widget(name="cmus.repeat"),
|
|
|
|
]
|
|
|
|
super(Module, self).__init__(engine, config, widgets)
|
2016-12-10 08:19:24 +01:00
|
|
|
|
|
|
|
engine.input.register_callback(widgets[0], button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="cmus-remote -r")
|
|
|
|
engine.input.register_callback(widgets[1], button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="cmus-remote -u")
|
|
|
|
engine.input.register_callback(widgets[2], button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="cmus-remote -n")
|
|
|
|
engine.input.register_callback(widgets[3], button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="cmus-remote -S")
|
|
|
|
engine.input.register_callback(widgets[4], button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="cmus-remote -R")
|
|
|
|
|
2016-12-10 07:47:24 +01:00
|
|
|
self._fmt = self.parameter("format", "{artist} - {title} {position}/{duration}")
|
2016-12-10 08:16:27 +01:00
|
|
|
self._status = None
|
|
|
|
self._shuffle = False
|
|
|
|
self._repeat = False
|
2016-12-11 11:37:24 +01:00
|
|
|
self._tags = defaultdict(lambda: '')
|
2016-12-10 07:47:24 +01:00
|
|
|
|
2016-12-11 11:37:24 +01:00
|
|
|
def description(self, widget):
|
2016-12-10 07:47:24 +01:00
|
|
|
return string.Formatter().vformat(self._fmt, (), self._tags)
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
self._load_song()
|
|
|
|
|
2016-12-10 08:16:27 +01:00
|
|
|
def state(self, widget):
|
|
|
|
if widget.name == "cmus.shuffle":
|
|
|
|
return "shuffle-on" if self._shuffle else "shuffle-off"
|
|
|
|
if widget.name == "cmus.repeat":
|
|
|
|
return "repeat-on" if self._repeat else "repeat-off"
|
|
|
|
if widget.name == "cmus.prev":
|
|
|
|
return "prev"
|
|
|
|
if widget.name == "cmus.next":
|
|
|
|
return "next"
|
|
|
|
return self._status
|
|
|
|
|
2016-12-10 07:47:24 +01:00
|
|
|
def _load_song(self):
|
|
|
|
info = ""
|
|
|
|
try:
|
|
|
|
info = bumblebee.util.execute("cmus-remote -Q")
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
self._tags = defaultdict(lambda: '')
|
|
|
|
for line in info.split("\n"):
|
|
|
|
if line.startswith("status"):
|
|
|
|
self._status = line.split(" ", 2)[1]
|
|
|
|
if line.startswith("tag"):
|
|
|
|
key, value = line.split(" ", 2)[1:]
|
|
|
|
self._tags.update({ key: value })
|
2016-12-11 08:25:54 +01:00
|
|
|
for key in ["duration", "position"]:
|
|
|
|
if line.startswith(key):
|
|
|
|
dur = int(line.split(" ")[1])
|
|
|
|
self._tags.update({key:bumblebee.util.durationfmt(dur)})
|
2016-12-10 07:47:24 +01:00
|
|
|
if line.startswith("set repeat "):
|
|
|
|
self._repeat = False if "false" in line else True
|
|
|
|
if line.startswith("set shuffle "):
|
|
|
|
self._shuffle = False if "false" in line else True
|
2016-12-10 11:25:02 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|