From d4ea14b0b34097b79bd203c7d57c459ecaed34d2 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Wed, 18 Mar 2015 11:31:39 +0100 Subject: new online_status module by @obb --- py3status/modules/online_status.py | 70 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 py3status/modules/online_status.py diff --git a/py3status/modules/online_status.py b/py3status/modules/online_status.py new file mode 100644 index 0000000..432ffb0 --- /dev/null +++ b/py3status/modules/online_status.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +""" +Module displaying if a connection to the internet is established. + +@author obb +""" + +from time import time +try: + # python3 + from urllib.request import urlopen +except: + from urllib2 import urlopen + + +class Py3status: + """ + Configuration parameters: + - cache_timeout : how often to run the check + - format_offline : what to display when offline + - format_online : what to display when online + - timeout : how long before deciding we're offline + - url : connect to this url to check the connection status + """ + # available configuration parameters + cache_timeout = 10 + format_offline = '■' + format_online = '●' + timeout = 2 + url = 'http://www.google.com' + + def _connection_present(self): + try: + urlopen(self.url, timeout=self.timeout) + except: + return False + else: + return True + + def online_status(self, i3s_output_list, i3s_config): + self.offline_color = i3s_config['color_bad'] + self.online_color = i3s_config['color_good'] + + response = { + 'cached_until': time() + self.cache_timeout + } + + connected = self._connection_present() + if connected: + response['full_text'] = self.format_online + response['color'] = self.online_color + else: + response['full_text'] = self.format_offline + response['color'] = self.offline_color + + return response + +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + 'color_good': '#00FF00', + 'color_bad': '#FF0000', + } + while True: + print(x.online_status([], config)) + sleep(1) -- cgit v1.3 From 35e1037960e4136af93845743949a78aa4abe4cf Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Wed, 18 Mar 2015 15:02:25 +0100 Subject: remove useless code on online_status module --- py3status/modules/online_status.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/py3status/modules/online_status.py b/py3status/modules/online_status.py index 432ffb0..8cc6eeb 100644 --- a/py3status/modules/online_status.py +++ b/py3status/modules/online_status.py @@ -38,9 +38,6 @@ class Py3status: return True def online_status(self, i3s_output_list, i3s_config): - self.offline_color = i3s_config['color_bad'] - self.online_color = i3s_config['color_good'] - response = { 'cached_until': time() + self.cache_timeout } @@ -48,10 +45,10 @@ class Py3status: connected = self._connection_present() if connected: response['full_text'] = self.format_online - response['color'] = self.online_color + response['color'] = i3s_config['color_good'] else: response['full_text'] = self.format_offline - response['color'] = self.offline_color + response['color'] = i3s_config['color_bad'] return response -- cgit v1.3 From 7b70f50b9bd896792de5aebf7a69af4826117bd3 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Wed, 18 Mar 2015 17:17:58 +0000 Subject: Pomodoro module: display time as a progress bar --- py3status/modules/pomodoro.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/py3status/modules/pomodoro.py b/py3status/modules/pomodoro.py index 1a167b9..bbfae1e 100644 --- a/py3status/modules/pomodoro.py +++ b/py3status/modules/pomodoro.py @@ -6,6 +6,10 @@ Pomodoro countdown on i3bar originally written by @Fandekasp (Adrien Lemaire) from subprocess import call from time import time +# PROGRESS_BAR_ITEMS = u"▁▃▄▅▆▇█" +PROGRESS_BAR_ITEMS = u"▏▎▍▌▋▊▉" +N_PROGRESS_BARS = 5 + class Py3status: @@ -15,10 +19,11 @@ class Py3status: timer_long_break = 15 * 60 timer_pomodoro = 25 * 60 - def __init__(self): + def __init__(self, display_bar=True): self.__setup('stop') self.alert = False self.run = False + self.display_bar = display_bar def on_click(self, i3s_output_list, i3s_config, event): """ @@ -42,9 +47,27 @@ class Py3status: """ Return the response full_text string """ - return { - 'full_text': '{} ({})'.format(self.prefix, self.timer) - } + if self.display_bar and self.status in ('start', 'pause'): + bar = u'' + items_cnt = len(PROGRESS_BAR_ITEMS) + bar = u'' + bar_val = float(self.timer) / self.time_window * N_PROGRESS_BARS + while bar_val > 0: + selector = int(bar_val * items_cnt) + selector = min(selector, items_cnt - 1) + bar += PROGRESS_BAR_ITEMS[selector] + bar_val -= 1 + + bar = bar.ljust(N_PROGRESS_BARS).encode('utf_8') + else: + bar = self.timer + + if self.run: + text = '{} [{}]'.format(self.prefix, bar) + else: + text = '{} ({})'.format(self.prefix, bar) + + return dict(full_text=text) def __setup(self, status): """ @@ -55,20 +78,24 @@ class Py3status: self.prefix = 'Pomodoro' self.status = 'stop' self.timer = self.timer_pomodoro + self.time_window = self.timer self.breaks = 1 elif status == 'start': self.prefix = 'Pomodoro' self.timer = self.timer_pomodoro + self.time_window = self.timer elif status == 'pause': self.prefix = 'Break #%d' % self.breaks if self.breaks > self.max_breaks: self.timer = self.timer_long_break + self.time_window = self.timer self.breaks = 1 else: self.breaks += 1 self.timer = self.timer_break + self.time_window = self.timer def __decrement(self): """ -- cgit v1.3 From c54b3a3d92c25bd5ee28e2d344a9efca86677063 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Wed, 18 Mar 2015 18:34:36 +0000 Subject: Pomodoro module: make progress bar configurable --- py3status/modules/pomodoro.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/py3status/modules/pomodoro.py b/py3status/modules/pomodoro.py index bbfae1e..af42f08 100644 --- a/py3status/modules/pomodoro.py +++ b/py3status/modules/pomodoro.py @@ -8,22 +8,31 @@ from time import time # PROGRESS_BAR_ITEMS = u"▁▃▄▅▆▇█" PROGRESS_BAR_ITEMS = u"▏▎▍▌▋▊▉" -N_PROGRESS_BARS = 5 class Py3status: + """ + Configuration parameters: + - max_breaks: maximum number of breaks + - timer_break: normal break time (seconds) + - timer_long_break: long break time (seconds) + - timer_pomodoro: pomodoro time (seconds) + - display_bar: display time in bars when True, otherwise in seconds + - num_progress_bars: number of progress bars + """ # available configuration parameters max_breaks = 4 timer_break = 5 * 60 timer_long_break = 15 * 60 timer_pomodoro = 25 * 60 + display_bar = True + num_progress_bars = 5 - def __init__(self, display_bar=True): + def __init__(self): self.__setup('stop') self.alert = False self.run = False - self.display_bar = display_bar def on_click(self, i3s_output_list, i3s_config, event): """ @@ -51,14 +60,14 @@ class Py3status: bar = u'' items_cnt = len(PROGRESS_BAR_ITEMS) bar = u'' - bar_val = float(self.timer) / self.time_window * N_PROGRESS_BARS + bar_val = float(self.timer) / self.time_window * self.num_progress_bars while bar_val > 0: selector = int(bar_val * items_cnt) selector = min(selector, items_cnt - 1) bar += PROGRESS_BAR_ITEMS[selector] bar_val -= 1 - bar = bar.ljust(N_PROGRESS_BARS).encode('utf_8') + bar = bar.ljust(self.num_progress_bars).encode('utf_8') else: bar = self.timer -- cgit v1.3 From 786470a09ee1d7736c41dcd89db632657f9f87c7 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Thu, 19 Mar 2015 10:18:03 +0000 Subject: Pomodoro module: disable progress bar by default --- py3status/modules/pomodoro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py3status/modules/pomodoro.py b/py3status/modules/pomodoro.py index af42f08..07e3087 100644 --- a/py3status/modules/pomodoro.py +++ b/py3status/modules/pomodoro.py @@ -26,7 +26,7 @@ class Py3status: timer_break = 5 * 60 timer_long_break = 15 * 60 timer_pomodoro = 25 * 60 - display_bar = True + display_bar = False num_progress_bars = 5 def __init__(self): -- cgit v1.3 From 78f09107102d36f3592a3e69409a2b58101e8704 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Thu, 19 Mar 2015 10:20:46 +0000 Subject: Pomodoro module: sort configuration parameters --- py3status/modules/pomodoro.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py3status/modules/pomodoro.py b/py3status/modules/pomodoro.py index 07e3087..3041c56 100644 --- a/py3status/modules/pomodoro.py +++ b/py3status/modules/pomodoro.py @@ -13,21 +13,21 @@ PROGRESS_BAR_ITEMS = u"▏▎▍▌▋▊▉" class Py3status: """ Configuration parameters: + - display_bar: display time in bars when True, otherwise in seconds - max_breaks: maximum number of breaks + - num_progress_bars: number of progress bars - timer_break: normal break time (seconds) - timer_long_break: long break time (seconds) - timer_pomodoro: pomodoro time (seconds) - - display_bar: display time in bars when True, otherwise in seconds - - num_progress_bars: number of progress bars """ # available configuration parameters + display_bar = False max_breaks = 4 + num_progress_bars = 5 timer_break = 5 * 60 timer_long_break = 15 * 60 timer_pomodoro = 25 * 60 - display_bar = False - num_progress_bars = 5 def __init__(self): self.__setup('stop') -- cgit v1.3 From f8f7069e42061107ac5f3a2619d47bfedff96e55 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Thu, 19 Mar 2015 11:15:16 +0000 Subject: Pomodoro module: add sound support --- py3status/modules/pomodoro.py | 45 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/py3status/modules/pomodoro.py b/py3status/modules/pomodoro.py index 3041c56..63068b5 100644 --- a/py3status/modules/pomodoro.py +++ b/py3status/modules/pomodoro.py @@ -4,8 +4,15 @@ Pomodoro countdown on i3bar originally written by @Fandekasp (Adrien Lemaire) """ from subprocess import call +from syslog import syslog, LOG_INFO from time import time +try: + from pygame import mixer + mixer.init() +except ImportError: + mixer = None + # PROGRESS_BAR_ITEMS = u"▁▃▄▅▆▇█" PROGRESS_BAR_ITEMS = u"▏▎▍▌▋▊▉" @@ -16,15 +23,21 @@ class Py3status: - display_bar: display time in bars when True, otherwise in seconds - max_breaks: maximum number of breaks - num_progress_bars: number of progress bars - - timer_break: normal break time (seconds) - - timer_long_break: long break time (seconds) - - timer_pomodoro: pomodoro time (seconds) + - sound_break_end: break end sound (file path) + - sound_pomodoro_end: pomodoro end sound (file path) + - sound_pomodoro_start: pomodoro start sound (file path) + - timer_break: normal break time (seconds) (requires pygame) + - timer_long_break: long break time (seconds) (requires pygame) + - timer_pomodoro: pomodoro time (seconds) (requires pygame) """ # available configuration parameters display_bar = False max_breaks = 4 num_progress_bars = 5 + sound_break_end = None + sound_pomodoro_end = None + sound_pomodoro_start = None timer_break = 5 * 60 timer_long_break = 15 * 60 timer_pomodoro = 25 * 60 @@ -41,6 +54,7 @@ class Py3status: if event['button'] == 1: if self.status == 'stop': self.status = 'start' + self.__play_sound(self.sound_pomodoro_start) self.run = True elif event['button'] == 2: @@ -60,7 +74,8 @@ class Py3status: bar = u'' items_cnt = len(PROGRESS_BAR_ITEMS) bar = u'' - bar_val = float(self.timer) / self.time_window * self.num_progress_bars + bar_val = float(self.timer) / self.time_window * \ + self.num_progress_bars while bar_val > 0: selector = int(bar_val * items_cnt) selector = min(selector, items_cnt - 1) @@ -118,9 +133,12 @@ class Py3status: if self.status == 'start': self.__setup('pause') self.status = 'pause' + self.__play_sound(self.sound_pomodoro_end) + elif self.status == 'pause': self.__setup('start') self.status = 'start' + self.__play_sound(self.sound_break_end) def __i3_nagbar(self, level='warning'): """ @@ -158,6 +176,25 @@ class Py3status: response['cached_until'] = time() return response + def __play_sound(self, sound_fname): + """Play sound if required + """ + if not sound_fname: + return + + if not mixer: + syslog(LOG_INFO, "pomodoro module: the pygame library is required" + " to play sounds") + return + + try: + mixer.music.load(sound_fname) + except Exception as e: + return + + mixer.music.play() + + if __name__ == "__main__": """ Test this module by calling it directly. -- cgit v1.3