New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added module stopwatch #1216
Added module stopwatch #1216
Changes from all commits
9fae958
ecaaee7
8b20c8c
f14f755
a9504b9
2ac182e
2827b82
21dff90
c763d29
2e0a69a
44de92f
6afd9d9
80a7d09
e59d6af
ca39388
cfba571
d319755
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A simple 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: 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}') | ||
|
||
Format placeholders: | ||
{stopwatch} display hours:minutes:seconds | ||
|
||
@author Jonas Heinrich | ||
|
||
SAMPLE OUTPUT | ||
{'full_text': 'Stopwatch 0:00: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': '58'}, | ||
] | ||
""" | ||
|
||
from time import time | ||
|
||
|
||
class Py3status: | ||
""" | ||
""" | ||
# available configuration parameters | ||
button_reset = 2 | ||
button_toggle = 1 | ||
format = 'Stopwatch {stopwatch}' | ||
|
||
def post_config_hook(self): | ||
self.time_start = None | ||
self._reset_time() | ||
|
||
def _reset_time(self): | ||
self.running = False | ||
self.paused = False | ||
self.time_state = None | ||
self.color = None | ||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we might be able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
t = 0 | ||
|
||
hours, t = divmod(t, 3600) | ||
minutes, t = divmod(t, 60) | ||
seconds = t | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Nit). I'd like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
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} | ||
) | ||
} | ||
|
||
def on_click(self, event): | ||
button = event['button'] | ||
|
||
if button == self.button_toggle: | ||
if self.running: | ||
# pause stopwatch | ||
self.running = False | ||
self.paused = True | ||
self.time_state = int(time() - self.time_start) | ||
self.color = self.py3.COLOR_DEGRADED | ||
else: | ||
self.color = self.py3.COLOR_GOOD | ||
self.running = True | ||
# start/restart stopwatch | ||
if self.paused: | ||
self.time_start = int(time() - self.time_state) | ||
else: | ||
self.time_start = time() | ||
elif button == self.button_reset: | ||
# reset and pause stopwatch | ||
self._reset_time() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prevent users from updating this module with clicks/scrolling if they feel like doing it. EDIT: And users have no reasons to play with this anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay done |
||
else: | ||
self.py3.prevent_refresh() | ||
|
||
|
||
if __name__ == "__main__": | ||
""" | ||
Run module in test mode. | ||
""" | ||
from py3status.module_test import module_test | ||
module_test(Py3status) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docs...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done