diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 63292c6..66e1301 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -24,6 +24,7 @@ gacekjk Georg Sieber Goran Mekić Gordon Schulz +Holden Salomon Hugo Osvaldo Barrera Iliyas Jorio Ismael diff --git a/README.rst b/README.rst index 94e2350..cee6975 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,7 @@ Changelog Contributors ------------ -A list of all contributors can be found in `CONTRIBUTORS `_. +A list of all contributors can be found in `CONTRIBUTORS `_. Particular noteworthy contributors are former maintainer Jan Oliver Oelerich and current maintainer enkore. diff --git a/i3pystatus/cpu_usage.py b/i3pystatus/cpu_usage.py index 5df3dc7..ad1ff2f 100644 --- a/i3pystatus/cpu_usage.py +++ b/i3pystatus/cpu_usage.py @@ -28,12 +28,14 @@ class CpuUsage(IntervalModule): format_all = "{core}:{usage:02}%" exclude_average = False interval = 1 + color = None settings = ( ("format", "format string."), ("format_all", ("format string used for {usage_all} per core. " "Available formaters are {core} and {usage}. ")), ("exclude_average", ("If True usage average of all cores will " - "not be in format_all.")) + "not be in format_all.")), + ("color", "HTML color code #RRGGBB") ) def init(self): @@ -117,5 +119,6 @@ class CpuUsage(IntervalModule): usage['usage'] = usage['usage_cpu'] self.output = { - "full_text": self.format.format_map(usage) + "full_text": self.format.format_map(usage), + "color": self.color } diff --git a/i3pystatus/moon.py b/i3pystatus/moon.py new file mode 100644 index 0000000..ad22f21 --- /dev/null +++ b/i3pystatus/moon.py @@ -0,0 +1,111 @@ +from i3pystatus import IntervalModule, formatp + +import datetime +import math + +import decimal +import os + +from i3pystatus.core.util import TimeWrapper + +dec = decimal.Decimal + + +class MoonPhase(IntervalModule): + """ + Available Formatters + + status: Allows for mapping of current moon phase + - New Moon: + - Waxing Crescent: + - First Quarter: + - Waxing Gibbous: + - Full Moon: + - Waning Gibbous: + - Last Quarter: + - Waning Crescent: + + """ + + settings = ( + "format", + ("status", "Current moon phase"), + ("illum", "Percentage that is illuminated"), + ("color", "Set color"), + ) + + format = "{illum} {status}" + + interval = 60 * 60 * 2 # every 2 hours + + status = { + "New Moon": "NM", + "Waxing Crescent": "WaxCres", + "First Quarter": "FQ", + "Waxing Gibbous": "WaxGib", + "Full Moon": "FM", + "Waning Gibbous": "WanGib", + "Last Quarter": "LQ", + "Waning Cresent": "WanCres", + } + + color = { + "New Moon": "#00BDE5", + "Waxing Crescent": "#138DD8", + "First Quarter": "#265ECC", + "Waxing Gibbous": "#392FBF", + "Full Moon": "#4C00B3", + "Waning Gibbous": "#871181", + "Last Quarter": "#C32250", + "Waning Crescent": "#FF341F", + } + + def pos(now=None): + days_in_second = 86400 + + now = datetime.datetime.now() + difference = now - datetime.datetime(2001, 1, 1) + + days = dec(difference.days) + (dec(difference.seconds) / dec(days_in_second)) + lunarCycle = dec("0.20439731") + (days * dec("0.03386319269")) + + return lunarCycle % dec(1) + + def current_phase(self): + + lunarCycle = self.pos() + + index = (lunarCycle * dec(8)) + dec("0.5") + index = math.floor(index) + + return { + 0: "New Moon", + 1: "Waxing Crescent", + 2: "First Quarter", + 3: "Waxing Gibbous", + 4: "Full Moon", + 5: "Waning Gibbous", + 6: "Last Quarter", + 7: "Waning Crescent", + }[int(index) & 7] + + def illum(self): + phase = 0 + lunarCycle = float(self.pos()) * 100 + + if lunarCycle > 50: + phase = 100 - lunarCycle + else: + phase = lunarCycle * 2 + + return phase + + def run(self): + fdict = { + "status": self.status[self.current_phase()], + "illum": self.illum(), + } + self.output = { + "full_text": formatp(self.format, **fdict), + "color": self.color[self.current_phase()], + } diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index 5e7a547..ad0c006 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -38,7 +38,7 @@ class MPD(IntervalModule): ("max_field_len", "Defines max length for in truncate_fields defined fields, if truncated, ellipsis are appended as indicator. It's applied *before* max_len. Value of 0 disables this."), ("max_len", "Defines max length for the hole string, if exceeding fields specefied in truncate_fields are truncated equaly. If truncated, ellipsis are appended as indicator. It's applied *after* max_field_len. Value of 0 disables this."), ("truncate_fields", "fields that will be truncated if exceeding max_field_len or max_len."), - + ("hide_inactive", "Hides status information when MPD is not running"), ) host = "localhost" @@ -54,6 +54,7 @@ class MPD(IntervalModule): max_field_len = 25 max_len = 100 truncate_fields = ("title", "album", "artist") + hide_inactive = False on_leftclick = "switch_playpause" on_rightclick = "next_song" on_upscroll = on_rightclick @@ -78,8 +79,16 @@ class MPD(IntervalModule): return None def run(self): - status = self._mpd_command(self.s, "status") - currentsong = self._mpd_command(self.s, "currentsong") + try: + status = self._mpd_command(self.s, "status") + currentsong = self._mpd_command(self.s, "currentsong") + except Exception: + if self.hide_inactive: + self.output = { + "full_text": "" + } + return + fdict = { "pos": int(status.get("song", 0)) + 1, "len": int(status["playlistlength"]), diff --git a/i3pystatus/spotify.py b/i3pystatus/spotify.py index a4a97bb..4404fe8 100644 --- a/i3pystatus/spotify.py +++ b/i3pystatus/spotify.py @@ -1,78 +1,103 @@ -import threading import math - -from i3pystatus import Module -from gi.repository import Playerctl, GLib +from i3pystatus import formatp +from i3pystatus import IntervalModule +from gi.repository import Playerctl -class Spotify(Module): +class Spotify(IntervalModule): """ - This class shows information from Spotify. + Gets Spotify info using playerctl - Left click will toggle pause/play of the current song. - Right click will skip the song. + .. rubric:: Available formatters - Dependent on Playerctl ( https://github.com/acrisci/playerctl ) and GLib + * `{status}` — current status icon (paused/playing) + * `{length}` — total song duration (mm:ss format) + * `{artist}` — artist + * `{title}` — title + * `{album}` — album """ - format = "{artist} - {title}" - color = "#ffffff" - settings = ( - ("format", "Format string. {artist}, {title}, {album}, {volume}, and {length} are available for output."), - ("color", "color of the output"), + ('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'), ) - on_leftclick = "switch_playpause" - on_rightclick = "next_song" + # default settings + color = '#ffffff' + color_not_running = '#ffffff' + format = '{status} {length} {artist} - {title}' + format_not_running = 'Not running' + interval = 1 + status = { + 'paused': '▷', + 'playing': '▶', + } - def main_loop(self): - """ Mainloop blocks so we thread it.""" - self.player = Playerctl.Player() - self.player.on('metadata', self.set_status) + on_leftclick = 'playpause' + on_rightclick = 'next_song' + on_upscroll = 'next_song' - if self.player.props.status != "": - self.set_status(self.player) + def get_info(self, player): + """gets spotify track info from playerctl""" - main = GLib.MainLoop() - main.run() - - def init(self): - try: - t = threading.Thread(target=self.main_loop) - t.daemon = True - t.start() - except Exception as e: - self.output = { - "full_text": "Error creating new thread!", - "color": "#FF0000" - } - - def set_status(self, player, e=None): artist = player.get_artist() title = player.get_title() album = player.get_album() - volume = player.props.volume + status = player.props.status + # gets the length of spotify through the metadata command length = "" - if e is not None: - time = e["mpris:length"] / 60.0e6 + + # stores the metadata and checks if it is valid + metadata = player.props.metadata + if metadata is not None: + # math to convert the number stored in mpris:length to a human readable format + time = dict(metadata)["mpris:length"] / 60.0e6 minutes = math.floor(time) seconds = round(time % 1 * 60) if seconds < 10: seconds = "0" + str(seconds) length = "{}:{}".format(minutes, seconds) - self.output = { - "full_text": self.format.format( - artist=artist, title=title, - album=album, length=length, - volume=volume), - "color": self.color - } + # sets length to an empty string if it does not exist for whatever reason. This should usually not happen + else: + length = "" - def switch_playpause(self): + # returns a dictionary of all spotify data + return {"artist": artist, "title": title, "album": album, "status": status, "length": length} + + def run(self): + """Main statement, executes all code every interval""" + + # tries to create player object and get data from player + try: + self.player = Playerctl.Player() + + response = self.get_info(self.player) + + # creates a dictionary of the spotify data captured + fdict = { + 'status': self.status[response['status'].lower()], + 'title': response["title"], + 'album': response.get('album', ''), + 'artist': response.get('artist', ''), + 'length': response.get('length', 0), + } + self.output = {"full_text": formatp(self.format, **fdict), + "color": self.color} + + # outputs the not running string if spotify is closed + except: + self.output = {"full_text": self.format_not_running, + "color": self.color_not_running} + + def playpause(self): + """Pauses and plays spotify""" self.player.play_pause() def next_song(self): + """skips to the next song""" self.player.next()