This commit is contained in:
leggettc13 2017-06-07 18:10:17 +00:00 committed by GitHub
commit 0160b951c3
3 changed files with 133 additions and 0 deletions

111
bumblebee/modules/moc.py Normal file
View file

@ -0,0 +1,111 @@
# pylint: disable=C0111,R0903
# -*- coding: utf-8 -*-
"""Displays information about the current song in moc.
Requires the following executable:
* mocp
Parameters:
* mocp.format: Format string for the song information. Tag values can be put in curly brackets (i.e. {artist})
"""
from collections import defaultdict
import string
import bumblebee.util
import bumblebee.input
import bumblebee.output
import bumblebee.engine
from bumblebee.output import scrollable
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
widgets = [
bumblebee.output.Widget(name="moc.prev"),
bumblebee.output.Widget(name="moc.main", full_text=self.description),
bumblebee.output.Widget(name="moc.next"),
bumblebee.output.Widget(name="moc.shuffle"),
bumblebee.output.Widget(name="moc.repeat"),
]
super(Module, self).__init__(engine, config, widgets)
engine.input.register_callback(widgets[0], button=bumblebee.input.LEFT_MOUSE,
cmd="mocp -r")
engine.input.register_callback(widgets[1], button=bumblebee.input.LEFT_MOUSE,
cmd="mocp -G")
engine.input.register_callback(widgets[2], button=bumblebee.input.LEFT_MOUSE,
cmd="mocp -f")
engine.input.register_callback(widgets[3], button=bumblebee.input.LEFT_MOUSE,
cmd=self._toggle_shuffle)
engine.input.register_callback(widgets[4], button=bumblebee.input.LEFT_MOUSE,
cmd=self._toggle_repeat)
self._fmt = self.parameter("format", "{artist} - {title} {position}/{duration}")
self._status = None
self._shuffle = False
self._repeat = False
self._tags = defaultdict(lambda: '')
@scrollable
def description(self, widget):
return string.Formatter().vformat(self._fmt, (), self._tags)
def update(self, widgets):
self._load_song()
def state(self, widget):
if widget.name == "moc.shuffle":
return "shuffle-on" if self._shuffle else "shuffle-off"
if widget.name == "moc.repeat":
return "repeat-on" if self._repeat else "repeat-off"
if widget.name == "moc.prev":
return "prev"
if widget.name == "moc.next":
return "next"
return self._status
def _load_song(self):
info = ""
try:
info = bumblebee.util.execute("mocp -i")
except RuntimeError:
pass
self._tags = defaultdict(lambda: '')
for line in info.split("\n"):
if line.startswith("State"):
status = line.split(" ", 2)[1]
if status == "PAUSE":
self._status = "paused"
if status == "PLAY":
self._status = "playing"
if line.startswith("Title"):
value = line.split(" ", 2)[1:]
self._tags.update({ "title": value })
if line.startswith("Artist"):
value = line.split(" ", 2)[1:]
self._tags.update({ "artist": value })
for key in ["TotalSec", "CurrentSec"]:
if line.startswith(key):
dur = int(line.split(" ")[1])
self._tags.update({key:bumblebee.util.durationfmt(dur)})
def _toggle_shuffle(self, widget):
if self._shuffle:
bumblebee.util.execute("mocp -u shuffle")
self._shuffle = False
else:
bumblebee.util.execute("mocp -o shuffle")
self._shuffle = True
def _toggle_repeat(self, widget):
if self._repeat:
bumblebee.util.execute("mocp -u repeat")
self._repeat = False
else:
bumblebee.util.execute("mocp -o repeat")
self._repeat = True
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -18,6 +18,17 @@
"repeat-on": { "prefix": "R" },
"repeat-off": { "prefix": "[r]" }
},
"moc": {
"playing": { "prefix": ">" },
"paused": { "prefix": "||" },
"stopped": { "prefix": "[]" },
"prev": { "prefix": "|<" },
"next": { "prefix": ">|" },
"shuffle-on": { "prefix": "S" },
"shuffle-off": { "prefix": "[s]" },
"repeat-on": { "prefix": "R" },
"repeat-off": { "prefix": "[r]" }
},
"pasink": {
"muted": { "prefix": "audio(mute)" },
"unmuted": { "prefix": "audio" }

View file

@ -24,6 +24,17 @@
"repeat-on": { "prefix": "" },
"repeat-off": { "prefix": "" }
},
"moc": {
"playing": { "prefix": "" },
"paused": { "prefix": "" },
"stopped": { "prefix": "" },
"prev": { "prefix": "" },
"next": { "prefix": "" },
"shuffle-on": { "prefix": "" },
"shuffle-off": { "prefix": "" },
"repeat-on": { "prefix": "" },
"repeat-off": { "prefix": "" }
},
"gpmdp": {
"playing": { "prefix": "" },
"paused": { "prefix": "" },