Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Reset immediately if timer isn't running.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwf committed Jun 13, 2016
1 parent 638da73 commit 79c33e2
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions thud.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def __init__(self):
self.record = []
self.reset()

@property
def running(self):
return self._last_started is not None

def reset(self):
self._accumulated = datetime.timedelta(0)
self._last_started = None
Expand All @@ -19,22 +23,22 @@ def start(self):
self.record.append(('start', self._last_started))

def pause(self):
if self._last_started is None:
if not self.running:
raise ValueError('timer not running')
pause_time = datetime.datetime.now()
self.record.append(('pause', pause_time))
self._accumulated += (pause_time - self._last_started)
self._last_started = None

def toggle(self):
if self._last_started is None:
if not self.running:
self.start()
else:
self.pause()

@property
def current(self):
if self._last_started is None:
if not self.running:
return self._accumulated
else:
current = datetime.datetime.now() - self._last_started
Expand Down Expand Up @@ -78,11 +82,15 @@ def __init__(self):
self.edit_container = DisableToggle(self.edit)
self.text = urwid.Text(self.time_display)
widget = urwid.ListBox([
urwid.Columns([
('pack', urwid.Text(' ')),
self.edit_container,
('pack', self.text)
], dividechars=1)])
urwid.LineBox(
urwid.Padding(
urwid.Columns([
self.edit_container,
('pack', self.text)
], dividechars=2),
left=1, right=1)
),
])
super().__init__(widget, unhandled_input=self.unhandled_input)
self.alarm = self.set_alarm_in(0.1, self._update_callback)

Expand All @@ -95,7 +103,7 @@ def unhandled_input(self, key):
self.timer.toggle()
elif key == 'enter':
self.new_task()
elif key.lower() == 'q':
elif key.lower() == 'q' or key == 'esc':
raise urwid.ExitMainLoop

def new_task(self):
Expand All @@ -108,9 +116,12 @@ def callback(_, __):
self.timer.reset()
self._new_task_wait = False

self.timer.toggle()
self._new_task_wait = True
self.set_alarm_in(2, callback)
if self.timer.running:
self.timer.pause()
self._new_task_wait = True
self.set_alarm_in(2, callback)
else:
callback(None, None)

def edit_box_enter_callback(self):
self.timer.start()
Expand Down

0 comments on commit 79c33e2

Please sign in to comment.