-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi3blockstimerthing.py
executable file
·99 lines (78 loc) · 3.03 KB
/
i3blockstimerthing.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
#!/usr/bin/env python3
# Copyright © 2020 Amir Eldor <amir@eldor.dev>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the COPYING file for more details.
from datetime import datetime, timedelta
from os import getenv
from typing import Tuple
filename = getenv("I3BLOCKSTIMERTHING_FILE", "/tmp/i3blockstimerthing.txt")
button = getenv("button")
pomodoro_warning = getenv("POMODORO_WARNING", "1")
RUNNING = "running"
PAUSED = "paused"
def write_stuff_to_file(write_this: str, state: str) -> None:
with open(filename, "w") as timer_file:
timer_file.write(write_this + " " + state)
def restart_timer():
now_as_str: str = str(int(datetime.now().timestamp()))
write_stuff_to_file(now_as_str, RUNNING)
return 0, RUNNING
def read_stuff_from_file() -> Tuple[int, str]:
with open(filename, "r") as timer_file:
lines = timer_file.readlines()
time_value, state = lines[0].split(' ')
return int(time_value), state
def file_time_to_seconds_count(file_time: int) -> int:
"""file_time is usually in unix timestamp"""
start_time = datetime.fromtimestamp(file_time)
delta = datetime.now() - start_time
seconds_count = int(delta.total_seconds())
return seconds_count
def read_timer() -> Tuple[int, str]:
try:
time_value, state = read_stuff_from_file()
if state == RUNNING:
# time_value is a "start time" timestamp
seconds_count = file_time_to_seconds_count(time_value)
elif state == PAUSED:
# time_value is the total seconds left
seconds_count = int(time_value)
else:
seconds_count = 0
return seconds_count, state
except IOError:
restart_timer()
return 0, RUNNING
def toggle_timer():
time_value, state = read_stuff_from_file()
if state == RUNNING:
seconds_count = file_time_to_seconds_count(time_value)
write_stuff_to_file(str(seconds_count), PAUSED)
elif state == PAUSED:
now_timestamp = datetime.now().timestamp()
new_timer_start_time = int(now_timestamp) - time_value
write_stuff_to_file(str(new_timer_start_time), RUNNING)
if button == "1":
restart_timer()
elif button == "2" or button == "3":
toggle_timer()
seconds_count, state = read_timer()
def format_time(seconds_count):
if seconds_count < 3600:
minutes = str(int(seconds_count / 60))
seconds = str(int(seconds_count % 60)).zfill(2)
return f"{minutes}:{seconds}"
else:
return str(timedelta(seconds=seconds_count))
if state == "paused":
icon = "⏸️ "
else:
icon = "⏱ "
def wrap_with_color(text, color, background):
return f'<span color="{color}" bgcolor="{background}">{text}</span>'
to_display = format_time(seconds_count)
if seconds_count >= 25 * 60 and pomodoro_warning == '1':
to_display = wrap_with_color(to_display, "white", "#ff4f4f")
time_display = icon + to_display
print(time_display)