-
Notifications
You must be signed in to change notification settings - Fork 116
/
breaktime.py
executable file
·324 lines (243 loc) · 9.22 KB
/
breaktime.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
# Reminder to get up and walk around if you've been at the computer
# for more than half an hour.
# Requires idle.py from gajim:
# https://dev.gajim.org/gajim/gajim/-/blob/89c7eb6e6ab3f61a188c6cee063a000526df522c/gajim/common/idle.py
#
# ipcsimple comes from Akkana's scripts repository, same as this script
import ipcsimple
import idle
import time
import tkinter
import signal
import sys, os
# Times are all in seconds
# How often should you get up and take time away from the computer? (minutes)
GETUP_INTERVAL = 30
# How long, at minimum, do you have to stay away for it to count? (minutes)
AWAY_MINIMUM = 5
# How often does the program wake up and poll?
POLL_INTERVAL = 30
# How much padding in the dialog, to make it a little bigger and more obvious
DIALOG_PADDING = 30
COLORS_NEEDBREAK = ("white", "darkred")
COLORS_NORMAL = ("black", "lightgrey")
COLORS_LONGENOUGH = ("black", "palegreen")
FONT_NORMAL = ("Serif", 18, "normal")
FONT_BOLD = ("Serif", 18, "bold")
DEBUG = False
# Timers counting idle and nonidle time
idle_start = 0
nonidle_start = 0
# When was the last check?
last_check = 0
# The dialog button where messages will show
tkroot = None
button = None
WINWIDTH = 600
WINHEIGHT = 240
def create_window():
global tkroot, button
tkroot = tkinter.Tk(className="breaktime")
# tkroot.overrideredirect(1)
tkroot.title("Break Time")
tkroot.withdraw()
tkroot.resizable(False, False)
def close():
tkroot.withdraw()
# Allocate enough space for three lines of text in the button:
button = tkinter.Button(tkroot, text="""MMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMM""",
fg=COLORS_NORMAL[0],
activeforeground=COLORS_NORMAL[0],
bg=COLORS_NORMAL[1],
activebackground=COLORS_NORMAL[1],
font=FONT_NORMAL,
# width=WINWIDTH, height=WINHEIGHT,
command=close)
# make it a bit bigger than necessary
button.pack(ipadx=DIALOG_PADDING, ipady=DIALOG_PADDING)
# Allow quitting the whole app with ctrl-q in the dialog.
tkroot.bind('<Control-Key-q>', quit)
tkroot.bind('<Control-Key-r>', reset)
# # Callback for key events:
# def key_event(event):
# if event.char == 'q':
# sys.exit(0)
# # Apparently we can't bind key events to a button, only to the dialog:
# tkroot.bind("<Key>", key_event)
# catch ctrl-C -- KeyboardInterrupt doesn't work from Tk
# def handler(event):
# tkroot.destroy()
# tkroot.bind_all('<Control-c>', handler)
tkroot.resizable(False, False)
tkroot.after(1000, poll_for_time)
ipcsimple.set_ipc_handler(communicate)
tkroot.after(50, poll_for_signals) # time in ms.
# Show dialog by default. XXX Eventually might make this configurable;
# omitting this step makes the dialog hidden until the first time
# an alert is shown.
def show_dialog():
update_dialog("Starting...", popup=True)
tkroot.after(50, show_dialog)
tkroot.mainloop()
def poll_for_time():
msg, colors, font = get_check_msg()
update_dialog(msg=msg, popup=("bold" in font), colors=colors, font=font)
tkroot.after(POLL_INTERVAL*1000, poll_for_time)
def poll_for_signals():
"""This controls how promptly the Tk app can respond to a signal"""
tkroot.after(1000, poll_for_signals) # time in ms.
def communicate(bytestring=None):
"""Called by the ipc_handler that is invoked on a SIGUSR1.
Return a string that will be sent through the handler.
bytestring is probably None and will be ignored.
"""
return get_check_msg()
def update_dialog(msg, colors=None, popup=True, font=None):
if DEBUG:
print("update_dialog:", msg)
if colors:
button["fg"] = colors[0]
button["activeforeground"] = colors[0]
button["bg"] = colors[1]
button["activebackground"] = colors[1]
if font:
button["font"] = font
button["text"] = msg # Can also use button.config(text=message)
if popup:
tkroot.deiconify()
return
# Return control to the shell before starting the loop:
# (disabled for testing)
# rc = os.fork()
# if rc:
# sys.exit(0)
def reset(event):
global idle_start, nonidle_start
now = time.time()
idle_start = now - AWAY_MINIMUM - 1
nonidle_start = 0
update_dialog(msg="Resetting...", popup=False,
colors=COLORS_LONGENOUGH, font=FONT_BOLD)
def get_check_msg():
"""Figure out the current status/time left.
Return message, color, font for the dialog
(also used when querying from an external process).
"""
global idle_start, nonidle_start, last_check
msg = None
colors = None
font = None
now = time.time()
# print("get_check_msg: last_check", last_check, ", now", now)
# Special case: if the machine is waking up from suspend or hibernate,
# then that time should be treated as idle.
if last_check and now - last_check > AWAY_MINIMUM:
last_check = now
nonidle_start = now
idle_start = 0
return "Restarting timer after a break", COLORS_NORMAL, FONT_NORMAL
last_check = now
nonidle_time = -1
def nonidle_str():
return f"{(nonidle_time/60):.1f} min"
idlesecs = idle.getIdleSec()
if idlesecs < POLL_INTERVAL:
# Currently nonidle
idle_start = 0
if not nonidle_start:
# Going from idle to nonidle
nonidle_start = now
if idle_start and DEBUG:
print("Starting nonidle timer after %.1f minutes idle"
% ((now - idle_start)/60))
else:
nonidle_time = now - nonidle_start
msg = f"You've been at the computer\nfor {nonidle_str()}"
if nonidle_time > GETUP_INTERVAL:
msg += "\nTime to take a break"
return msg, COLORS_NEEDBREAK, FONT_BOLD
else:
msg += f"\n(of {int(GETUP_INTERVAL/60)})"
return msg, COLORS_NORMAL, FONT_NORMAL
elif nonidle_start:
# Even though we're idle, calculate nonidle_time
# because it's useful to see in the message.
nonidle_time = now - nonidle_start
# Else currently idle
if not idle_start:
idle_start = now
return (f"You've been at the computer\nfor {nonidle_str()}",
COLORS_NORMAL, FONT_NORMAL)
idle_time = now - idle_start
if idle_time >= AWAY_MINIMUM:
# Away long enough
# zero the nonidle timer so it can be restarted when nonidle
if nonidle_start:
nonidle_start = 0
msg = f"Away long enough,\n{(idle_time/60):.1f} minutes"
return msg, COLORS_LONGENOUGH, FONT_NORMAL
# else still idle.
# Avoid the "Idle for 0.0 minutes" message:
if idle_time < 30:
return (f"""Starting idle timer
(after {nonidle_str()} at the computer)""",
COLORS_NORMAL, FONT_NORMAL)
if nonidle_time > GETUP_INTERVAL:
color = COLORS_NEEDBREAK
else:
color = COLORS_NORMAL
return ( f"""\nIdle for {(idle_time/60):.1f} minutes
({nonidle_str()} at the computer)""",
color, FONT_NORMAL )
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description=\
"""Monitor time spent at the computer, and suggest taking breaks.
Times should be specified in minutes.""")
parser.add_argument('-d', "--debug", dest="debug", default=False,
action="store_true", help="Print debugging messages")
parser.add_argument('-q', "--query", dest="query", default=False,
action="store_true",
help="Query running instance for current status")
parser.add_argument('-b', '--break', dest='min_break_time',
default=AWAY_MINIMUM, nargs='?', type=float,
help='Minimum time for a break')
parser.add_argument('max_time', nargs='?', type=float,
default=GETUP_INTERVAL,
help='Maximum time at the computer before taking a break')
args = parser.parse_args(sys.argv[1:])
# Debug mode?
if args.debug:
DEBUG = True
if args.query:
try:
pidlist = ipcsimple.list_procs("breaktime", uid=os.getuid())
for pid in pidlist:
pid = int(pid)
result = ipcsimple.ping_running_process(pid)
print("Process", pid, "status:")
print(result)
except RuntimeError as e:
print("Couldn't find a running breaktime:", e)
sys.exit(0)
# Should only allow one running instance of breaktime
ipcsimple.kill_instances("breaktime")
if not DEBUG:
rc = os.fork()
if rc:
sys.exit(0)
GETUP_INTERVAL = args.max_time * 60
AWAY_MINIMUM = args.min_break_time * 60
print("Max time at computer of %.1f min" % (GETUP_INTERVAL/60))
print("Min time away of %.1f min" % (AWAY_MINIMUM/60))
try:
create_window()
except KeyboardInterrupt:
if DEBUG:
print("Keyboard Interrupt")
# except:
# pass
idle.close()