#!/usr/bin/env python3
"""
Countdown Timer - Simple CLI timer with progress bar
"""
import argparse
import time
import sys
from datetime import timedelta
def format_time(seconds: int) -> str:
"""Format seconds into MM:SS"""
return str(timedelta(seconds=seconds))
def countdown(seconds: int, message: str = "Time's up!"):
"""Run the countdown"""
print(f"⏳ Starting countdown: {format_time(seconds)}\n")
try:
for remaining in range(seconds, 0, -1):
progress = (seconds - remaining) / seconds
bar = "█" * int(30 * progress) + "░" * (30 - int(30 * progress))
print(f"\r{bar} {format_time(remaining)}", end="", flush=True)
time.sleep(1)
# Final message
print(f"\r{'█' * 30} 00:00")
print(f"\n🎉 {message}")
# Try to play a sound (works on most systems)
try:
import os
if sys.platform == "darwin":
os.system("say 'Time is up' &")
elif sys.platform == "linux":
os.system("paplay /usr/share/sounds/freedesktop/stereo/bell.oga 2>/dev/null || echo -e '\a'")
else:
print('\a') # Bell character
except:
print('\a')
except KeyboardInterrupt:
print("\n\n⏹️ Timer stopped by user.")
sys.exit(0)
def main():
parser = argparse.ArgumentParser(description="⏰ Countdown Timer")
parser.add_argument("time", help="Time to count down (e.g. 5m, 90s, 2h30m)")
parser.add_argument("-m", "--message", default="Time's up!",
help="Message to show when timer finishes")
args = parser.parse_args()
# Parse time input
time_str = args.time.lower().strip()
total_seconds = 0
import re
# Support formats like 5m, 90s, 2h, 1h30m, 45
matches = re.findall(r'(\d+)([hms]?)', time_str)
if not matches and time_str.isdigit():
total_seconds = int(time_str)
else:
for value, unit in matches:
value = int(value)
if unit == 'h':
total_seconds += value * 3600
elif unit == 'm':
total_seconds += value * 60
else: # s or no unit
total_seconds += value
if total_seconds <= 0:
print("❌ Please provide a valid time (e.g. 5m, 90s, 2h)")
sys.exit(1)
if total_seconds > 86400: # more than 24 hours
print("⚠️ Very long timer...")
countdown(total_seconds, args.message)
if __name__ == "__main__":
main()