Skip to content

Commit

Permalink
Added mpc module
Browse files Browse the repository at this point in the history
  • Loading branch information
thnikk committed Dec 14, 2024
1 parent 6faabec commit 525457a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from modules import toggle
from modules import privacy
from modules import hass_2
from modules import mpc
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib # noqa

Expand Down Expand Up @@ -65,7 +66,8 @@ def module(bar, name, config):
'test': test.module,
'toggle': toggle.module,
'privacy': privacy.module,
'hass_2': hass_2.module
'hass_2': hass_2.module,
'mpc': mpc.module
}

all_widgets = {
Expand Down
57 changes: 57 additions & 0 deletions modules/mpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/python -u
from subprocess import run, DEVNULL, CalledProcessError
import common as c
import threading
import time
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Pango, GLib # noqa


class Mpc(c.Module):
def __init__(self, bar, config):
super().__init__()
self.icon.set_text('')
self.text.set_max_width_chars(20)
self.text.set_ellipsize(Pango.EllipsizeMode.END)

try:
run(['mpc', 'version'], check=True, stdout=DEVNULL, stderr=DEVNULL)
except CalledProcessError:
self.text.set_text('mpd not running')
self.update()

thread = threading.Thread(target=self.listen)
thread.daemon = True
thread.start()

def listen(self):
while True:
try:
run(['mpc', 'idle'], stdout=DEVNULL, stderr=DEVNULL)
GLib.idle_add(self.update)
except CalledProcessError:
time.sleep(5)

def update(self):
try:
output = run(
['mpc', 'status'], capture_output=True, check=True
).stdout.decode('utf-8').splitlines()
except CalledProcessError:
return
artist = output[0].split(' - ')[0].strip()
song = output[0].split(' - ')[-1]
status = output[1].split(']')[0].lstrip('[')
if status == 'playing':
self.icon.set_text('')
elif status == 'paused':
self.icon.set_text('')
else:
self.icon.set_text('')
self.text.set_text(song)


def module(bar, config):
module = Mpc(bar, config)
return module

0 comments on commit 525457a

Please sign in to comment.