From d16d66e520c5f80870957c63694708118d6f9f69 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 00:46:53 +0200 Subject: [PATCH] Add module for MOC (music on console) --- i3pystatus/moc.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 i3pystatus/moc.py diff --git a/i3pystatus/moc.py b/i3pystatus/moc.py new file mode 100644 index 0000000..2c8377f --- /dev/null +++ b/i3pystatus/moc.py @@ -0,0 +1,99 @@ +import re + +from i3pystatus import IntervalModule +from i3pystatus import formatp +from i3pystatus.core.command import run_through_shell +from i3pystatus.core.util import TimeWrapper + + +class Moc(IntervalModule): + """ + Display various information from MOC (musci on console) + + .. rubric:: Available formatters + + * `{status}` — current status icon (paused/playing/stopped) + * `{song_elapsed}` — song elapsed time (mm:ss format) + * `{song_length}` — total song duration (mm:ss format) + * `{artist}` — artist + * `{title}` — title + * `{album}` — album + * `{tracknumber}` — tracknumber + * `{file}` — file or url name + """ + + settings = ( + ('format', 'formatp string'), + ('format_not_running', 'Text to show if cmus is not running'), + ('color', 'The color of the text'), + ('color_not_running', 'The color of the text, when cmus is not running'), + ('status', 'Dictionary mapping status to output'), + ) + + color = '#ffffff' + color_not_running = '#ffffff' + format = '{status} {song_elapsed}/{song_length} {artist} - {title}' + format_not_running = 'Not running' + interval = 1 + status = { + 'pause': '▷', + 'play': '▶', + 'stop': '◾', + } + + on_leftclick = 'toggle_pause' + on_rightclick = 'next_song' + on_upscroll = 'next_song' + on_downscroll = 'previous_song' + + def _moc_command(self, command): + cmdline = 'mocp --{command}'.format(command=command) + return run_through_shell(cmdline, enable_shell=True) + + def _query_cmus(self): + response = {} + + # Get raw information + cmd = self._moc_command('info') + + # Now we make it useful + if not cmd.rc: + for line in cmd.out.splitlines(): + key, _, value = line.partition(': ') + response[key] = value + + return response + + def run(self): + response = self._query_cmus() + + if response: + fdict = { + 'album': response.get('Album', ''), + 'artist': response.get('Artist', ''), + 'file': response.get('File', ''), + 'song_elapsed': TimeWrapper(response.get('CurrentSec', 0)), + 'song_length': TimeWrapper(response.get('TotalSec', 0)), + 'status': self.status[response['State'].lower()], + 'title': response.get('SongTitle', ''), + 'tracknumber': re.match(r'(\d*).*', response.get('Title', '')).group(1) or 0, + } + + self.output = { + 'full_text': formatp(self.format, **fdict), + 'color': self.color, + } + else: + self.output = { + 'full_text': self.format_not_running, + 'color': self.color_not_running, + } + + def toggle_pause(self): + self._moc_command('toggle-pause') + + def next_song(self): + self._moc_command('next') + + def previous_song(self): + self._moc_command('previous')