Skip to content
Open
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
99 changes: 64 additions & 35 deletions Pomodoro-Timer/pomodoro.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,72 @@
import time
import datetime

# Create a function that acts as a countdown
def pomodoro_timer(task, h, m, s):
# Calculate the total number of seconds
total_seconds = h * 3600 + m * 60 + s

# Counter to keep track of the breaks
break_count = 0
FOCUS_DURATION = 25 * 60 # 25 minutes
SHORT_BREAK = 5 * 60 # 5 minutes
LONG_BREAK = 20 * 60 # 20 minutes
SESSIONS_BEFORE_LONG_BREAK = 4

while total_seconds > 0:
# Timer represents time left on the countdown
timer = datetime.timedelta(seconds=total_seconds)
# Prints the time left on the timer
print(f"Focusing on {task}... Session time left: {timer}", end="\r")

# Delays the program one second
def countdown(label, seconds):
"""Runs a countdown timer."""
while seconds > 0:
timer = datetime.timedelta(seconds=seconds)
print(f"{label} - Time left: {timer}", end="\r")
time.sleep(1)
seconds -= 1
print(f"\n{label} completed!\n")


def pomodoro_timer(task, total_seconds):
"""Runs Pomodoro cycles based on total time."""
sessions_completed = 0

while total_seconds > 0:
print(f"\nStarting focus session for: {task}")
session_time = min(FOCUS_DURATION, total_seconds)
countdown("Focus Session", session_time)
total_seconds -= session_time
sessions_completed += 1

if total_seconds <= 0:
break

if sessions_completed % SESSIONS_BEFORE_LONG_BREAK == 0:
print("Starting Long Break...")
countdown("Long Break", LONG_BREAK)
else:
print("Starting Short Break...")
countdown("Short Break", SHORT_BREAK)

print("Task Completed Successfully 🎉")


def get_user_input():
"""Handles validated user input."""
try:
task = input("Enter the task to focus on: ")

h = int(input("Enter hours: "))
m = int(input("Enter minutes: "))
s = int(input("Enter seconds: "))

if h < 0 or m < 0 or s < 0:
raise ValueError("Time values cannot be negative.")

return task, h * 3600 + m * 60 + s

except ValueError as e:
print(f"Invalid input: {e}")
return None, None


if __name__ == "__main__":
try:
task, total_seconds = get_user_input()

if task and total_seconds:
pomodoro_timer(task, total_seconds)

# Reduces total time by one second
total_seconds -= 1

# Check if it's time for a break (only for the first 4 breaks)
if total_seconds > 0 and break_count < 4 and total_seconds % 1500 == 0:
print("\nNow on a short break!")
time.sleep(300) # Short break for 5 minutes
break_count += 1

# Check if it's time for a long break (after 4 sessions)
elif total_seconds > 0 and break_count == 4 and total_seconds % 1500 == 0:
print("\nNow on a long break!")
time.sleep(1200) # Long break for 20 minutes
break_count = 0 # Reset the break count for the next cycle

print("\nTask Completed")

# Inputs for hours, minutes, and seconds on the timer
task = input("Enter the task to focus on: ")
h = int(input("Enter the time in hours: "))
m = int(input("Enter the time in minutes: "))
s = int(input("Enter the time in seconds: "))
pomodoro_timer(task, h, m, s)
except KeyboardInterrupt:
print("\nTimer stopped manually.")