Skip to content
Merged

Pcc52 #796

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 52/ttafsir/pomodoro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from rich.console import Console
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rich?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been playing with rich lately and it seems super cool and useful for CLI outputs and layouts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, need to check that out, maybe we can do an article on the blog ...

from rich.live import Live
from time import sleep
from datetime import timedelta


console = Console()


class PomodoroTimer:
def __init__(self, start_timer=25, break_timer=5):
self._timer = start_timer
self._break = break_timer
self._countdown = timedelta(minutes=self._timer)

def run(self, prompt="TIMER"):
with Live(auto_refresh=False) as live:
while self._countdown:
console.print(f"{prompt}: {self._countdown}", end="\r")
self._countdown += timedelta(seconds=-1)
sleep(1)
live.refresh()

def restart(self, start_timer, prompt):
self._countdown = timedelta(minutes=start_timer)
self.run(prompt)


timer_duration = int(input("Duration: ").strip())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this into an if __name__ == "__main__" so it doesn't run if you would want to import this module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good point. will do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

break_duration = int(input("Break Duration: ").strip())
console.print("\nCTRL+C to quit")
while True:
try:
timer = PomodoroTimer(start_timer=timer_duration)
timer.run()
timer.restart(start_timer=break_duration, prompt="BREAK")
except KeyboardInterrupt:
console.print("TIME'S UP")
break