This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thud.py
136 lines (109 loc) · 3.63 KB
/
thud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import urwid
import datetime
class UpCountingTimer(object):
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
old_record = list(self.record)
self.record.clear()
return old_record
def start(self):
self._last_started = datetime.datetime.now()
self.record.append(('start', self._last_started))
def pause(self):
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 not self.running:
self.start()
else:
self.pause()
@property
def current(self):
if not self.running:
return self._accumulated
else:
current = datetime.datetime.now() - self._last_started
return self._accumulated + current
def __str__(self):
return '\n'.join('%5s %s' % data for data in self.record)
class TaskEdit(urwid.Edit):
def __init__(self, ui, *args, **kwargs):
self.ui = ui
super().__init__(*args, **kwargs)
def keypress(self, size, key):
if key == 'enter':
self.ui.edit_box_enter_callback()
else:
return super().keypress(size, key)
class DisableToggle(urwid.WidgetWrap):
def disable(self):
self._original = self._w
self._w = urwid.WidgetDisable(self._w)
def enable(self):
self._w = self._original
def toggle(self):
if isinstance(self._w, urwid.WidgetDisable):
self.enable()
else:
self.disable()
class UserInterface(urwid.MainLoop):
def __init__(self):
self.timer = UpCountingTimer()
self.edit = TaskEdit(self)
self.edit_container = DisableToggle(self.edit)
self.text = urwid.Text(self.time_display)
widget = urwid.ListBox([
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)
@property
def time_display(self):
return str(self.timer.current).split('.')[0]
def unhandled_input(self, key):
if key == ' ':
self.timer.toggle()
elif key == 'enter':
self.new_task()
elif key.lower() == 'q' or key == 'esc':
raise urwid.ExitMainLoop
def new_task(self):
if getattr(self, '_new_task_wait', False):
return
def callback(_, __):
self.edit_container.enable()
self.edit.edit_text = ''
self.timer.reset()
self._new_task_wait = False
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()
self.edit_container.toggle()
def _update_callback(self, _, __):
self.text.set_text(self.time_display)
self.alarm = self.set_alarm_in(0.1, self._update_callback)
def main():
UserInterface().run()