From 9fae95832435f6e6b9bdd7e1ac34d86ddc4b252e Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Fri, 5 Jan 2018 21:23:10 +0100 Subject: [PATCH 01/17] added module stopwatch --- py3status/modules/stopwatch.py | 164 +++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 py3status/modules/stopwatch.py diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py new file mode 100644 index 0000000000..fdf80ad289 --- /dev/null +++ b/py3status/modules/stopwatch.py @@ -0,0 +1,164 @@ +# -*- coding: utf-8 -*- +""" +A simple stopwatch. + +This is a very basic stopwatch. You can start, pause and reset the +stopwatch. + +Button 1 starts/pauses the stopwatch. +Button 2 resets stopwatch. + +Configuration parameters: + format: display format for this module (default 'Stopwatch {stopwatch}') + +Format placeholders: + {stopwatch} display hours:minutes:seconds + +@author Jonas Heinrich + +SAMPLE OUTPUT +{'full_text': 'Stopwatch 0:01:00'} + +running +[ + {'full_text': 'Stopwatch '}, + {'color': '#00FF00', 'full_text': '0'}, + {'full_text': ':'}, + {'color': '#00FF00', 'full_text': '00'}, + {'full_text': ':'}, + {'color': '#00FF00', 'full_text': '54'}, +] + +paused +[ + {'full_text': 'Stopwatch '}, + {'color': '#FFFF00', 'full_text': '0'}, + {'full_text': ':'}, + {'color': '#FFFF00', 'full_text': '00'}, + {'full_text': ':'}, + {'color': '#FFFF00', 'full_text': '54'}, +] +""" + +from time import time +from threading import Timer + + +class Py3status: + """ + """ + # available configuration parameters + format = 'Stopwatch {stopwatch}' + + def post_config_hook(self): + self.running = False + self.time_start = None + self.time_state = None + self.color = None + self.paused = False + + def stopwatch(self): + + def make_2_didget(value): + value = str(value) + if len(value) == 1: + value = '0' + value + return value + + if self.running: + t = int(time() - self.time_start) + else: + if self.time_state: + t = self.time_state + else: + t = 0 + + # Hours + hours, t = divmod(t, 3600) + # Minutes + mins, t = divmod(t, 60) + # Seconds + seconds = t + + if self.running: + cached_until = self.py3.time_in(0, offset=1) + else: + cached_until = self.py3.CACHE_FOREVER + + composites = [ + { + 'full_text': str(hours), + 'color': self.color, + 'index': 'hours', + }, + { + 'color': '#CCCCCC', + 'full_text': ':', + }, + { + 'full_text': make_2_didget(mins), + 'color': self.color, + 'index': 'mins', + }, + { + 'color': '#CCCCCC', + 'full_text': ':', + }, + { + 'full_text': make_2_didget(seconds), + 'color': self.color, + 'index': 'seconds', + }, + ] + + stopwatch = self.py3.composite_create(composites) + + return { + 'cached_until': cached_until, + 'full_text': self.py3.safe_format(self.format, {'stopwatch': stopwatch}) + } + + def on_click(self, event): + deltas = { + 'hours': 3600, + 'mins': 60, + 'seconds': 1 + } + index = event['index'] + button = event['button'] + + if button == 1: + if self.running: + # pause stopwatch + self.running = False + self.paused = True + self.time_state = int(time() - self.time_start) + self.color = '#FFFF00' + else: + # start/restart stopwatch + if self.paused: + self.time_start = int(time() - self.time_state) + self.running = True + else: + self.time_start = time() + self.running = True + self.color = '#00FF00' + + if button == 2: + # reset and pause stopwatch + self.running = False + self.paused = False + self.time_state = None + self.color = None + + if not self.running: + # change timer section HH:MM:SS + t = self.time_state + + +if __name__ == "__main__": + """ + Run module in test mode. + """ + from py3status.module_test import module_test + module_test(Py3status) From ecaaee7059ea47dfc49c2de2a2129667740dea96 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Mon, 8 Jan 2018 17:33:37 +0100 Subject: [PATCH 02/17] Update stopwatch.py Remove unused variables --- py3status/modules/stopwatch.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index fdf80ad289..a8a017f50c 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -41,8 +41,6 @@ """ from time import time -from threading import Timer - class Py3status: """ @@ -119,12 +117,6 @@ def make_2_didget(value): } def on_click(self, event): - deltas = { - 'hours': 3600, - 'mins': 60, - 'seconds': 1 - } - index = event['index'] button = event['button'] if button == 1: From 8b20c8c70dbabc676f7b20cfe030a9e2832fac61 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Mon, 8 Jan 2018 17:56:40 +0100 Subject: [PATCH 03/17] Fix unused variable, break long lines --- py3status/modules/stopwatch.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index a8a017f50c..8d979ee565 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -42,6 +42,7 @@ from time import time + class Py3status: """ """ @@ -113,7 +114,8 @@ def make_2_didget(value): return { 'cached_until': cached_until, - 'full_text': self.py3.safe_format(self.format, {'stopwatch': stopwatch}) + 'full_text': + self.py3.safe_format(self.format, {'stopwatch': stopwatch}) } def on_click(self, event): @@ -143,10 +145,6 @@ def on_click(self, event): self.time_state = None self.color = None - if not self.running: - # change timer section HH:MM:SS - t = self.time_state - if __name__ == "__main__": """ From f14f755c6f7c95320a9970088859b2d543e4f759 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 18:58:18 +0100 Subject: [PATCH 04/17] fix hardcoded colors --- py3status/modules/stopwatch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 8d979ee565..86536fdc48 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -127,7 +127,7 @@ def on_click(self, event): self.running = False self.paused = True self.time_state = int(time() - self.time_start) - self.color = '#FFFF00' + self.color = self.py3.COLOR_BAD else: # start/restart stopwatch if self.paused: @@ -136,7 +136,7 @@ def on_click(self, event): else: self.time_start = time() self.running = True - self.color = '#00FF00' + self.color = self.py3.COLOR_GOOD if button == 2: # reset and pause stopwatch From a9504b9563405e4eae841cc3a02bb9f3f6fd59a8 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 19:35:56 +0100 Subject: [PATCH 05/17] remove duplicate variable assigning --- py3status/modules/stopwatch.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 86536fdc48..b7da6164e8 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -126,17 +126,16 @@ def on_click(self, event): # pause stopwatch self.running = False self.paused = True - self.time_state = int(time() - self.time_start) + self.time_state = int(time() u- self.time_start) self.color = self.py3.COLOR_BAD else: + self.running = True + self.color = self.py3.COLOR_GOOD # start/restart stopwatch if self.paused: self.time_start = int(time() - self.time_state) - self.running = True else: self.time_start = time() - self.running = True - self.color = self.py3.COLOR_GOOD if button == 2: # reset and pause stopwatch From 2ac182eabacac2ec0c37699b7ab349ebd6f7b92c Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 19:39:18 +0100 Subject: [PATCH 06/17] better code formatting --- py3status/modules/stopwatch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index b7da6164e8..c7ef3b9111 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -114,8 +114,8 @@ def make_2_didget(value): return { 'cached_until': cached_until, - 'full_text': - self.py3.safe_format(self.format, {'stopwatch': stopwatch}) + 'full_text': self.py3.safe_format( + self.format, {'stopwatch': stopwatch}) } def on_click(self, event): @@ -130,7 +130,7 @@ def on_click(self, event): self.color = self.py3.COLOR_BAD else: self.running = True - self.color = self.py3.COLOR_GOOD + self.color = self.py3.COLOR_GOOD # start/restart stopwatch if self.paused: self.time_start = int(time() - self.time_state) From 2827b827ed06b5341b6e2b447d92225813291b74 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 19:45:50 +0100 Subject: [PATCH 07/17] remove useless comments --- py3status/modules/stopwatch.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index c7ef3b9111..eb33254c59 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -72,11 +72,8 @@ def make_2_didget(value): else: t = 0 - # Hours hours, t = divmod(t, 3600) - # Minutes - mins, t = divmod(t, 60) - # Seconds + minutes, t = divmod(t, 60) seconds = t if self.running: @@ -126,11 +123,11 @@ def on_click(self, event): # pause stopwatch self.running = False self.paused = True - self.time_state = int(time() u- self.time_start) + self.time_state = int(time() - self.time_start) self.color = self.py3.COLOR_BAD else: - self.running = True self.color = self.py3.COLOR_GOOD + self.running = True # start/restart stopwatch if self.paused: self.time_start = int(time() - self.time_state) From 21dff90b2f6d564013ef1c6d5f51cff96d3822e6 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 19:57:40 +0100 Subject: [PATCH 08/17] fix random stuff --- py3status/modules/stopwatch.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index eb33254c59..8975b9034d 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -55,6 +55,8 @@ def post_config_hook(self): self.time_state = None self.color = None self.paused = False + self.button_reset = 3 + self.button_toggle = 1 def stopwatch(self): @@ -85,25 +87,22 @@ def make_2_didget(value): { 'full_text': str(hours), 'color': self.color, - 'index': 'hours', }, { - 'color': '#CCCCCC', + 'color': self.color, 'full_text': ':', }, { - 'full_text': make_2_didget(mins), + 'full_text': make_2_didget(minutes), 'color': self.color, - 'index': 'mins', }, { - 'color': '#CCCCCC', + 'color': self.color, 'full_text': ':', }, { 'full_text': make_2_didget(seconds), 'color': self.color, - 'index': 'seconds', }, ] @@ -118,7 +117,7 @@ def make_2_didget(value): def on_click(self, event): button = event['button'] - if button == 1: + if button == self.button_toggle: if self.running: # pause stopwatch self.running = False @@ -134,7 +133,7 @@ def on_click(self, event): else: self.time_start = time() - if button == 2: + if button == self.button_reset: # reset and pause stopwatch self.running = False self.paused = False From c763d2949d514e4d8eab26798f5facc6e90c2105 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 20:08:29 +0100 Subject: [PATCH 09/17] simpler code for two digit formatting --- py3status/modules/stopwatch.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 8975b9034d..0b65a69d86 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -48,6 +48,8 @@ class Py3status: """ # available configuration parameters format = 'Stopwatch {stopwatch}' + button_reset = 3 + button_toggle = 1 def post_config_hook(self): self.running = False @@ -55,17 +57,9 @@ def post_config_hook(self): self.time_state = None self.color = None self.paused = False - self.button_reset = 3 - self.button_toggle = 1 def stopwatch(self): - def make_2_didget(value): - value = str(value) - if len(value) == 1: - value = '0' + value - return value - if self.running: t = int(time() - self.time_start) else: @@ -93,7 +87,7 @@ def make_2_didget(value): 'full_text': ':', }, { - 'full_text': make_2_didget(minutes), + 'full_text': '%02d' % (minutes), 'color': self.color, }, { @@ -101,7 +95,7 @@ def make_2_didget(value): 'full_text': ':', }, { - 'full_text': make_2_didget(seconds), + 'full_text': '%02d' % (seconds), 'color': self.color, }, ] From 2e0a69ac4f012c9d472401eab841a74880ebb21a Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 20:21:46 +0100 Subject: [PATCH 10/17] add reset_time function --- py3status/modules/stopwatch.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 0b65a69d86..44a9b57d71 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -51,18 +51,23 @@ class Py3status: button_reset = 3 button_toggle = 1 - def post_config_hook(self): + def _reset_time(self): self.running = False - self.time_start = None + self.paused = False self.time_state = None self.color = None - self.paused = False + + def post_config_hook(self): + self.time_start = None + self._reset_time() def stopwatch(self): if self.running: + cached_until = self.py3.time_in(0, offset=1) t = int(time() - self.time_start) else: + cached_until = self.py3.CACHE_FOREVER if self.time_state: t = self.time_state else: @@ -72,11 +77,6 @@ def stopwatch(self): minutes, t = divmod(t, 60) seconds = t - if self.running: - cached_until = self.py3.time_in(0, offset=1) - else: - cached_until = self.py3.CACHE_FOREVER - composites = [ { 'full_text': str(hours), @@ -129,10 +129,7 @@ def on_click(self, event): if button == self.button_reset: # reset and pause stopwatch - self.running = False - self.paused = False - self.time_state = None - self.color = None + self._reset_time() if __name__ == "__main__": From 44de92fb67d3f173cec11cafcf411af5854d7a1e Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 20:27:39 +0100 Subject: [PATCH 11/17] variables in alphabetical order --- py3status/modules/stopwatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 44a9b57d71..abac2a291b 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -47,9 +47,9 @@ class Py3status: """ """ # available configuration parameters - format = 'Stopwatch {stopwatch}' button_reset = 3 button_toggle = 1 + format = 'Stopwatch {stopwatch}' def _reset_time(self): self.running = False From 6afd9d9974774b8cfc80f6becd2d1e849761e5b8 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 20:49:46 +0100 Subject: [PATCH 12/17] add two new variables to documentation --- py3status/modules/stopwatch.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index abac2a291b..159409f66b 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -6,9 +6,11 @@ stopwatch. Button 1 starts/pauses the stopwatch. -Button 2 resets stopwatch. +Button 3 resets stopwatch. Configuration parameters: + button_reset: button event id to reset the stopwatch + button_toggle: button event id to start/pause the stopwatch format: display format for this module (default 'Stopwatch {stopwatch}') Format placeholders: From 80a7d090f6c35b635e17fc6818b739eb270ae0a6 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 21:07:19 +0100 Subject: [PATCH 13/17] reordner functions --- py3status/modules/stopwatch.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 159409f66b..94475968ef 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -9,8 +9,8 @@ Button 3 resets stopwatch. Configuration parameters: - button_reset: button event id to reset the stopwatch - button_toggle: button event id to start/pause the stopwatch + button_reset: button event id to reset the stopwatch (default 3) + button_toggle: button event id to start/pause the stopwatch (default 1) format: display format for this module (default 'Stopwatch {stopwatch}') Format placeholders: @@ -53,16 +53,15 @@ class Py3status: button_toggle = 1 format = 'Stopwatch {stopwatch}' + def post_config_hook(self): + self.time_start = None + def _reset_time(self): self.running = False self.paused = False self.time_state = None self.color = None - def post_config_hook(self): - self.time_start = None - self._reset_time() - def stopwatch(self): if self.running: From e59d6afa1c493a8d31ce69cb3c011c5d978665b3 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sat, 20 Jan 2018 21:47:27 +0100 Subject: [PATCH 14/17] readd missing function call --- py3status/modules/stopwatch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 94475968ef..31233da637 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -55,6 +55,7 @@ class Py3status: def post_config_hook(self): self.time_start = None + self._reset_time() def _reset_time(self): self.running = False From ca39388fa4f50fdb856f7b25bba4737f2f074fc5 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sun, 21 Jan 2018 14:51:39 +0100 Subject: [PATCH 15/17] fixed documentation inconsistencies --- py3status/modules/stopwatch.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 31233da637..fb924bb5f4 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -2,15 +2,13 @@ """ A simple stopwatch. -This is a very basic stopwatch. You can start, pause and reset the -stopwatch. - -Button 1 starts/pauses the stopwatch. -Button 3 resets stopwatch. +This module is able to start tracking the amount of time elapsed +when activated through a mouse click. The stopwatch can also be +paused, resumed and with a different mouse button, reset to zero. Configuration parameters: - button_reset: button event id to reset the stopwatch (default 3) - button_toggle: button event id to start/pause the stopwatch (default 1) + button_reset: mouse button to reset the stopwatch (default 3) + button_toggle: mouse button to start/stop the stopwatch (default 1) format: display format for this module (default 'Stopwatch {stopwatch}') Format placeholders: @@ -19,7 +17,7 @@ @author Jonas Heinrich SAMPLE OUTPUT -{'full_text': 'Stopwatch 0:01:00'} +{'full_text': 'Stopwatch 0:00:00'} running [ @@ -38,7 +36,7 @@ {'full_text': ':'}, {'color': '#FFFF00', 'full_text': '00'}, {'full_text': ':'}, - {'color': '#FFFF00', 'full_text': '54'}, + {'color': '#FFFF00', 'full_text': '58'}, ] """ @@ -128,10 +126,11 @@ def on_click(self, event): self.time_start = int(time() - self.time_state) else: self.time_start = time() - - if button == self.button_reset: + elif button == self.button_reset: # reset and pause stopwatch self._reset_time() + else: + self.py3.prevent_refresh() if __name__ == "__main__": From cfba571583b02548a3dd62aea1c1a0b412db5fcb Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Sun, 21 Jan 2018 15:35:24 +0100 Subject: [PATCH 16/17] changed pause color and use different mouse button for reset --- py3status/modules/stopwatch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index fb924bb5f4..7c433a9a44 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -7,7 +7,7 @@ paused, resumed and with a different mouse button, reset to zero. Configuration parameters: - button_reset: mouse button to reset the stopwatch (default 3) + button_reset: mouse button to reset the stopwatch (default 2) button_toggle: mouse button to start/stop the stopwatch (default 1) format: display format for this module (default 'Stopwatch {stopwatch}') @@ -47,7 +47,7 @@ class Py3status: """ """ # available configuration parameters - button_reset = 3 + button_reset = 2 button_toggle = 1 format = 'Stopwatch {stopwatch}' @@ -117,7 +117,7 @@ def on_click(self, event): self.running = False self.paused = True self.time_state = int(time() - self.time_start) - self.color = self.py3.COLOR_BAD + self.color = self.py3.COLOR_DEGRADED else: self.color = self.py3.COLOR_GOOD self.running = True From d3197556800f5250bb38f35e30b68beac0fce709 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Tue, 23 Jan 2018 18:00:19 +0100 Subject: [PATCH 17/17] rewrite watch composition --- py3status/modules/stopwatch.py | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/py3status/modules/stopwatch.py b/py3status/modules/stopwatch.py index 7c433a9a44..e10374afbe 100644 --- a/py3status/modules/stopwatch.py +++ b/py3status/modules/stopwatch.py @@ -62,7 +62,6 @@ def _reset_time(self): self.color = None def stopwatch(self): - if self.running: cached_until = self.py3.time_in(0, offset=1) t = int(time() - self.time_start) @@ -77,35 +76,14 @@ def stopwatch(self): minutes, t = divmod(t, 60) seconds = t - composites = [ - { - 'full_text': str(hours), - 'color': self.color, - }, - { - 'color': self.color, - 'full_text': ':', - }, - { - 'full_text': '%02d' % (minutes), - 'color': self.color, - }, - { - 'color': self.color, - 'full_text': ':', - }, - { - 'full_text': '%02d' % (seconds), - 'color': self.color, - }, - ] - - stopwatch = self.py3.composite_create(composites) + stopwatch = self.py3.safe_format('\?color=%s %d:%02d:%02d' % ( + self.color, hours, minutes, seconds)) return { 'cached_until': cached_until, 'full_text': self.py3.safe_format( - self.format, {'stopwatch': stopwatch}) + self.format, {'stopwatch': stopwatch} + ) } def on_click(self, event):