From 0341ac29e02c7877c32ee810aa38a6906cc693df Mon Sep 17 00:00:00 2001 From: MeuHubPython Date: Tue, 15 Oct 2024 14:35:07 -0300 Subject: [PATCH 1/2] Python Simple Pomodoro Timer --- Pomodoro Timer/README.md | 27 +++++++++++++++++++++++++++ Pomodoro Timer/main.py | 31 +++++++++++++++++++++++++++++++ Pomodoro Timer/runtime.txt | 1 + 3 files changed, 59 insertions(+) create mode 100644 Pomodoro Timer/README.md create mode 100644 Pomodoro Timer/main.py create mode 100644 Pomodoro Timer/runtime.txt diff --git a/Pomodoro Timer/README.md b/Pomodoro Timer/README.md new file mode 100644 index 0000000..3560f56 --- /dev/null +++ b/Pomodoro Timer/README.md @@ -0,0 +1,27 @@ +# Pomodoro Timer + +A simple, customizable command line pomodoro timer built in Python. + +## Features + +- Customizable work, short and long breaks intervals: Define your own durations for each cycle. +- Cycles: Run Multiple cycles of work and break intervals +- Console output: Show work and break intervals in the console. + +## How it works + +The Pomodoro Timer follow these steps: +1. Set your work and break intervals. +2. Take a short break every work interval. +3. After a set number of cycles, take a long break. + +## Usage + +1. Clone this repository to your machine. +2. Run the Python script from the command line. + '''python pomodoro_timer.py''' + +## Author + +[MeuHubPython](https://github.com/MeuHubPython) + diff --git a/Pomodoro Timer/main.py b/Pomodoro Timer/main.py new file mode 100644 index 0000000..d08d7c5 --- /dev/null +++ b/Pomodoro Timer/main.py @@ -0,0 +1,31 @@ +import time + + +def countdown(minutes, label): + total_seconds = minutes * 60 + while total_seconds: + minutes, seconds = divmod(total_seconds, 60) + timer = f"{minutes:02d}:{seconds:02d}" + print(f"{label} Timer: {timer}", end="\r") + time.sleep(1) + total_seconds -= 1 + print(f"\n{label} finished!") + + +def pomodoro_timer(work_min, short_break_min, long_break_min, cycles): + for i in range(cycles): + print(f"\nCycle {i+1} of {cycles}") + countdown(work_min, "Work") + if i < cycles - 1: + countdown(short_break_min, "Short Break") + else: + countdown(long_break_min, "Long Break") + + +if __name__ == "__main__": + work_minutes = int(input("Enter work interval in minutes: ")) + short_break_minutes = int(input("Enter short break interval in minutes: ")) + long_break_minutes = int(input("Enter long break interval in minutes: ")) + cycles = int(input("Enter the number of cycles: ")) + + pomodoro_timer(work_minutes, short_break_minutes, long_break_minutes, cycles) diff --git a/Pomodoro Timer/runtime.txt b/Pomodoro Timer/runtime.txt new file mode 100644 index 0000000..f63b125 --- /dev/null +++ b/Pomodoro Timer/runtime.txt @@ -0,0 +1 @@ +Python 3.10.12 From ab12cddd1b62be361d0317be8a0976c1073c61c0 Mon Sep 17 00:00:00 2001 From: MeuHubPython Date: Sat, 19 Oct 2024 12:23:13 -0300 Subject: [PATCH 2/2] adding stop and pause features and input validation. --- Pomodoro Timer/README.md | 1 + Pomodoro Timer/main.py | 53 +++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Pomodoro Timer/README.md b/Pomodoro Timer/README.md index 3560f56..243b0fe 100644 --- a/Pomodoro Timer/README.md +++ b/Pomodoro Timer/README.md @@ -5,6 +5,7 @@ A simple, customizable command line pomodoro timer built in Python. ## Features - Customizable work, short and long breaks intervals: Define your own durations for each cycle. +- Pause whenever you want: Pause the timer when you want to take a break. - Cycles: Run Multiple cycles of work and break intervals - Console output: Show work and break intervals in the console. diff --git a/Pomodoro Timer/main.py b/Pomodoro Timer/main.py index d08d7c5..d882414 100644 --- a/Pomodoro Timer/main.py +++ b/Pomodoro Timer/main.py @@ -4,28 +4,69 @@ def countdown(minutes, label): total_seconds = minutes * 60 while total_seconds: - minutes, seconds = divmod(total_seconds, 60) - timer = f"{minutes:02d}:{seconds:02d}" + mins, secs = divmod(total_seconds, 60) + timer = f"{mins:02d}:{secs:02d}" print(f"{label} Timer: {timer}", end="\r") time.sleep(1) total_seconds -= 1 print(f"\n{label} finished!") +def handle_pause_stop(): + while True: + user_input = input( + "\nPress 'p' to pause, 's' to stop, or 'Enter' to continue: " + ).lower() + if user_input == "p": + print("Timer paused. Press 'Enter' to resume.") + input() + elif user_input == "s": + print("Timer stopped.") + return True # Return True to signal that the timer should stop + else: + return False # Return False to continue with the timer + + def pomodoro_timer(work_min, short_break_min, long_break_min, cycles): for i in range(cycles): print(f"\nCycle {i+1} of {cycles}") countdown(work_min, "Work") if i < cycles - 1: + print("\nStarting short break...") + if handle_pause_stop(): + return countdown(short_break_min, "Short Break") else: + print("\nStarting long break...") + if handle_pause_stop(): + return countdown(long_break_min, "Long Break") + if not repeat_or_end(): + return + + +def repeat_or_end(): + user_input = input( + "\nCycle finished. Would you like to repeat the cycle? (y/n): " + ).lower() + return user_input == "y" + + +def get_valid_input(prompt): + while True: + try: + value = int(input(prompt)) + if value <= 0: + raise ValueError + return value + except ValueError: + print("Invalid input. Please enter a positive integer.") if __name__ == "__main__": - work_minutes = int(input("Enter work interval in minutes: ")) - short_break_minutes = int(input("Enter short break interval in minutes: ")) - long_break_minutes = int(input("Enter long break interval in minutes: ")) - cycles = int(input("Enter the number of cycles: ")) + work_minutes = get_valid_input("Enter work interval in minutes: ") + short_break_minutes = get_valid_input("Enter short break interval in minutes: ") + long_break_minutes = get_valid_input("Enter long break interval in minutes: ") + cycles = get_valid_input("Enter the number of cycles: ") pomodoro_timer(work_minutes, short_break_minutes, long_break_minutes, cycles)