-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountdown_Timer.py
More file actions
68 lines (55 loc) · 1.88 KB
/
Copy pathCountdown_Timer.py
File metadata and controls
68 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
from time import sleep
from colorama import Fore, Style
from pyfiglet import figlet_format
CYAN, LIGHT_CYAN = Fore.CYAN, Fore.LIGHTCYAN_EX
GREEN, LIGHT_GREEN = Fore.GREEN, Fore.LIGHTGREEN_EX
BLUE = Fore.BLUE
RED = Fore.RED
YELLOW = Fore.YELLOW
DIM, BRIGHT = Style.DIM, Style.BRIGHT
RESET = Style.RESET_ALL
def countdown(timer):
print(f"{YELLOW}")
last_lines = 0
while timer >= 0:
hours, remainder = divmod(timer, 3600)
minutes, seconds = divmod(remainder, 60)
display_time = f'{hours:02d} : {minutes:02d} : {seconds:02d}'
# Render figlet text
big_text = figlet_format(display_time, font="big")
lines = big_text.splitlines()
# Move cursor up to overwrite previous lines
if last_lines:
sys.stdout.write("\033[F" * last_lines)
# Print and flush
for line in lines:
sys.stdout.write("\033[K" + line + "\n")
sys.stdout.flush()
last_lines = len(lines)
timer -= 1
if timer < 0:
return
sleep(1)
print(f"\n{BRIGHT}{YELLOW} " + " Countdown ".center(19, "-")+ f"")
try:
input_time = int(input(f"\n{LIGHT_CYAN}Enter the time in seconds: "))
if input_time <= 0:
print(f"\n{RED}Invalid input!\n{BLUE}Please enter a positive integer.\n")
sys.exit(1)
except ValueError:
print(f"\n{RED}Invalid input!\n{BLUE}Please enter a positive integer.\n")
sys.exit(1)
except (KeyboardInterrupt, EOFError):
print(f"\n\n{RED}Keyboard Interrupt!!!\n{BLUE}Exiting...\n")
sys.exit(1)
try:
input(f"\n{BLUE} --> Press Enter to start the timer when ready: ")
countdown(input_time)
except (KeyboardInterrupt, EOFError):
print(f"\n\n{RED}Keyboard Interrupt!!!\n{BLUE}Exiting...\n")
sys.exit(1)
except Exception as e:
print(f"\n\n{RED}Error: {e}")
sys.exit(1)
print(f"{CYAN} !!! Times Up !!!{RESET}\n")