2016-12-10 07:47:24 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
2017-04-02 08:58:12 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-12-10 07:47:24 +01:00
|
|
|
|
2016-12-10 12:03:58 +01:00
|
|
|
"""Displays information about the current song in cmus.
|
|
|
|
|
2017-01-05 04:55:14 +01:00
|
|
|
Requires the following executable:
|
|
|
|
* cmus-remote
|
|
|
|
|
2016-12-10 12:03:58 +01:00
|
|
|
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
|
|
|
|
|
2017-04-22 13:10:13 +02:00
|
|
|
from bumblebee.output import scrollable
|
|
|
|
|
2016-12-10 07:47:24 +01:00
|
|
|
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
|
|
|
|
2017-08-12 17:03:04 +02:00
|
|
|
def hidden(self):
|
|
|
|
return self._status == None
|
|
|
|
|
2017-04-22 13:10:13 +02:00
|
|
|
@scrollable
|
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):
|
2017-08-19 16:07:52 +02:00
|
|
|
returns = {
|
|
|
|
"cmus.shuffle": "shuffle-on" if self._shuffle else "shuffle-off",
|
|
|
|
"cmus.repeat": "repeat-on" if self._repeat else "repeat-off",
|
|
|
|
"cmus.prev": "prev",
|
|
|
|
"cmus.next": "next",
|
|
|
|
}
|
|
|
|
return returns.get(widget.name, self._status)
|
|
|
|
|
|
|
|
def _eval_line(self, line):
|
2017-08-19 16:33:42 +02:00
|
|
|
# not a typo, use decode detection to see whether we are
|
|
|
|
# dealing with Python2 or Python3
|
|
|
|
if hasattr(line, "decode"):
|
|
|
|
line = line.encode("utf-8", "replace")
|
2017-08-19 16:07:52 +02:00
|
|
|
name, key, value = (line.split(" ", 2) + [None, None])[:3]
|
|
|
|
|
|
|
|
if name == "status":
|
|
|
|
self._status = key
|
|
|
|
if name == "tag":
|
2017-08-19 16:23:38 +02:00
|
|
|
print("tag {}={}".format(key, value))
|
2017-08-19 16:07:52 +02:00
|
|
|
self._tags.update({key: value})
|
|
|
|
if name in ["duration", "position"]:
|
2017-08-19 16:23:38 +02:00
|
|
|
self._tags.update({name:bumblebee.util.durationfmt(int(key))})
|
2017-08-19 16:07:52 +02:00
|
|
|
if name == "set" and key == "repeat":
|
|
|
|
self._repeat = value == "true"
|
|
|
|
if name == "set" and key == "shuffle":
|
|
|
|
self._shuffle = value == "true"
|
2016-12-10 08:16:27 +01:00
|
|
|
|
2016-12-10 07:47:24 +01:00
|
|
|
def _load_song(self):
|
|
|
|
info = ""
|
|
|
|
try:
|
|
|
|
info = bumblebee.util.execute("cmus-remote -Q")
|
|
|
|
except RuntimeError:
|
2017-08-12 17:03:04 +02:00
|
|
|
self._status = None
|
2017-08-19 16:07:52 +02:00
|
|
|
|
2016-12-10 07:47:24 +01:00
|
|
|
self._tags = defaultdict(lambda: '')
|
|
|
|
for line in info.split("\n"):
|
2017-08-19 16:07:52 +02:00
|
|
|
self._eval_line(line)
|
2016-12-10 11:25:02 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|