|
| 1 | +""" |
| 2 | +Break Reminder — A simple cross-platform Python script |
| 3 | +that reminds you to take a break every hour based on system uptime. |
| 4 | +
|
| 5 | +✅ Works on Windows, macOS, and Linux |
| 6 | +✅ Uses only the Python standard library |
| 7 | +✅ Under 100 lines and well-commented |
| 8 | +""" |
| 9 | + |
| 10 | +import time |
| 11 | +import platform |
| 12 | +import ctypes |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import datetime |
| 16 | +import subprocess |
| 17 | + |
| 18 | +def get_uptime_seconds(): |
| 19 | + """Return the system uptime in seconds for the current OS.""" |
| 20 | + system = platform.system() |
| 21 | + |
| 22 | + try: |
| 23 | + if system == "Windows": |
| 24 | + # Windows: milliseconds since last boot |
| 25 | + return ctypes.windll.kernel32.GetTickCount64() / 1000 |
| 26 | + |
| 27 | + elif system == "Linux": |
| 28 | + # Linux: read uptime from /proc/uptime |
| 29 | + with open("/proc/uptime", "r") as f: |
| 30 | + return float(f.readline().split()[0]) |
| 31 | + |
| 32 | + elif system == "Darwin": |
| 33 | + # macOS: use sysctl to get boot time |
| 34 | + boot_time = subprocess.check_output(["sysctl", "-n", "kern.boottime"]).decode() |
| 35 | + sec = int(boot_time.split("sec =")[1].split(",")[0]) |
| 36 | + boot = datetime.datetime.fromtimestamp(sec) |
| 37 | + return (datetime.datetime.now() - boot).total_seconds() |
| 38 | + |
| 39 | + else: |
| 40 | + raise NotImplementedError("Unsupported OS") |
| 41 | + |
| 42 | + except Exception as e: |
| 43 | + print("Error detecting uptime:", e) |
| 44 | + return 0 |
| 45 | + |
| 46 | + |
| 47 | +def notify(message): |
| 48 | + """Display a cross-platform notification with the given message.""" |
| 49 | + system = platform.system() |
| 50 | + |
| 51 | + if system == "Windows": |
| 52 | + ctypes.windll.user32.MessageBoxW(0, message, "Break Reminder", 0x40) |
| 53 | + elif system == "Darwin": |
| 54 | + os.system(f'''osascript -e 'display notification "{message}" with title "Break Reminder"' ''') |
| 55 | + else: # Linux |
| 56 | + os.system(f'notify-send "Break Reminder" "{message}"') |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + """Main loop — checks uptime and reminds user every hour.""" |
| 61 | + print("☕ Break Reminder started! You'll be notified every hour.\n") |
| 62 | + |
| 63 | + last_reminder = 0 # Tracks how many hours have passed |
| 64 | + |
| 65 | + while True: |
| 66 | + uptime = get_uptime_seconds() |
| 67 | + hours_on = int(uptime // 3600) |
| 68 | + mins_on = int((uptime % 3600) // 60) |
| 69 | + |
| 70 | + # Show live uptime in the terminal |
| 71 | + sys.stdout.write(f"\rSystem on for {hours_on}h {mins_on}m") |
| 72 | + sys.stdout.flush() |
| 73 | + |
| 74 | + # Send a reminder every hour |
| 75 | + if uptime // 3600 > last_reminder: |
| 76 | + notify("You’ve been working for an hour! Time to take a short break ☕") |
| 77 | + last_reminder += 1 |
| 78 | + |
| 79 | + time.sleep(30) # Check every 30 seconds |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + main() |
0 commit comments