aboutsummaryrefslogtreecommitdiff
path: root/timebomb.py
blob: c8679bb7f16c27654242dcb427f286e69efde3be (plain) (blame)
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
#!/bin/env python3

# Egg timer
# 2020-02-08
# OM

from tkinter import Tk
from tkinter import messagebox
from tkinter import Canvas
#from tkinter import font as tkFont
import argparse
import time
import shutil
import notify2

root = Tk()
root.option_add('*Dialog.msg.font', 'Helvetica 12')

def message(m, s):
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    msg = 'ALARM, Timer ran ' + str(m) + 'm and ' + str(s) + ' s'

    notify_msg = notify2.Notification('ALARM', msg)
    notify_msg.show()

    if screen_width <= 0:
        screen_width = 1024
    if screen_height <= 0:
        screen_height = 768

    c = Canvas(root, bg = "darkred", \
            width = screen_width, height = screen_height)
    c.pack()
    messagebox.showwarning(title='ALARM', message=msg)
    #root.mainloop()

def main():
    argparser = argparse.ArgumentParser()
    argparser.add_argument(
        '-s',
        help='seconds',
        type=int,
        nargs=1
    )
    argparser.add_argument(
        '-m',
        help='minutes',
        type=int,
        nargs=1
    )

    args = argparser.parse_args()
    if not args.s and not args.m:
        argparser.print_help()

    time_s = 0
    time_m = 0
    if args.s:
        time_s = abs(args.s[0])
    if args.m:
        time_m = abs(args.m[0])

    if args.s or args.m:
        notify2.init('timebomb')
        sleep(m=time_m, s=time_s)
        print('')
        message(m=time_m, s=time_s)

def sleep(m, s):
    time_sum_s = (m * 60) + s
    time_m, time_s = divmod(time_sum_s, 60)

    print('Start: ' + time.ctime())
    print('Sleeping for: ' + str(time_m) \
            + ' mins and ' + str(time_s) + ' seconds')

    for x in range(time_sum_s):
        # because dynamic resizing this needs to be here
        cols, rows = shutil.get_terminal_size(fallback=(80, 24))

        pr_m, pr_s = divmod(x, 60)
        left = '  {:02d}:{:02d}  '.format(pr_m, pr_s)
        right = '  {:02d}:{:02d}  '.format(time_m, time_s)
 
        pbar_len = cols - len(left) - len(right)
        if pbar_len < 20:
            pbar_len = 20

        progress = int(x/(time_sum_s-1) * pbar_len)
 
        print(left, end='')
        print('#' * progress + '_' * int(pbar_len-progress), end='')
        print(right, end='\r')

        time.sleep(1)
    
if __name__ == '__main__':
    main()
in each repos: see "about"-tab (if existing) for more details / README.
mailto contact at omeckman dot net
all timestamps in UTC (German winter time: UTC+01:00, summer time: UTC+02:00)
dark theme is a modded version of: https://gist.github.com/Yoplitein/f4b671a2ec70c9e743fa