88 lines
2.0 KiB
Python
Executable File
88 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# vim: set filetype=python:
|
|
"""
|
|
Pomodoro timer
|
|
|
|
- Writes pomodoro timer to temporary file so statusbar can read it
|
|
- Notification on session finish
|
|
- Notification on break finish
|
|
"""
|
|
|
|
import atexit
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
from argparse import ArgumentParser
|
|
from time import sleep
|
|
|
|
POMO_PATH = os.path.join(
|
|
os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share")), "pomo"
|
|
)
|
|
|
|
|
|
@atexit.register
|
|
def clear():
|
|
if os.path.exists(POMO_PATH):
|
|
os.remove(POMO_PATH)
|
|
|
|
|
|
def format_mins_secs(mins, secs):
|
|
return f"{mins:02d}:{secs:02d}"
|
|
|
|
|
|
def countdown(duration):
|
|
while duration != 0:
|
|
mins = duration // 60
|
|
secs = duration % 60
|
|
with open(POMO_PATH, "w") as f:
|
|
f.write(format_mins_secs(mins, secs))
|
|
sleep(1)
|
|
duration -= 1
|
|
|
|
|
|
def main(args):
|
|
work_duration = args.work_duration * 60
|
|
break_duration = args.break_duration * 60
|
|
repeats = args.repeats
|
|
|
|
while repeats != 0:
|
|
subprocess.run(["notify-send", "Get started"])
|
|
countdown(work_duration)
|
|
if break_duration != 0:
|
|
subprocess.run(["notify-send", "Time for a break"])
|
|
countdown(break_duration)
|
|
subprocess.run(["notify-send", "Break is over, back to work"])
|
|
repeats -= 1
|
|
|
|
|
|
def handle_signal(signal, frame):
|
|
# Wait for clear to finish
|
|
clear()
|
|
print("Exiting")
|
|
exit(0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser()
|
|
parser.add_argument(
|
|
"-w", "--work-duration", type=int, help="Session duration", default=25
|
|
)
|
|
parser.add_argument(
|
|
"-b", "--break-duration", type=int, help="Break duration", default=5
|
|
)
|
|
parser.add_argument(
|
|
"-r", "--repeats", type=int, help="Numer of sessions", default=1
|
|
)
|
|
parser.add_argument("-c", "--clear", action="store_true", help="Clear timer")
|
|
|
|
args = parser.parse_args()
|
|
|
|
signal.signal(signal.SIGINT, handle_signal)
|
|
signal.signal(signal.SIGTERM, handle_signal)
|
|
|
|
if args.clear:
|
|
clear()
|
|
else:
|
|
main(args)
|