-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.py
More file actions
341 lines (282 loc) · 10.1 KB
/
Copy pathcountdown.py
File metadata and controls
341 lines (282 loc) · 10.1 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
"""
Animal-Themed TUI Countdown Timer
9-hour countdown with animal progression for each hour
Features: pause/resume, time adjustment, gradient progress, phase indicators
Optimized for 40x6 terminal pane, works with Ghostty & tmux
"""
import curses
import time
import sys
import os
import signal
TOTAL_SECONDS = 9 * 3600
ANIMALS = [
{"emoji": "🐢", "name": "Turtle", "phase": "Dawn", "phase_emoji": "🌅"},
{"emoji": "🐙", "name": "Octopus", "phase": "Sunrise", "phase_emoji": "🌄"},
{"emoji": "🐦", "name": "Bird", "phase": "Morning", "phase_emoji": "☀️"},
{"emoji": "🐱", "name": "Cat", "phase": "Midday", "phase_emoji": "🌤"},
{"emoji": "🦊", "name": "Fox", "phase": "Afternoon", "phase_emoji": "⛅"},
{"emoji": "🦁", "name": "Lion", "phase": "Evening", "phase_emoji": "🌥"},
{"emoji": "🐉", "name": "Dragon", "phase": "Dusk", "phase_emoji": "🌆"},
{"emoji": "🦅", "name": "Eagle", "phase": "Night", "phase_emoji": "🌙"},
{"emoji": "🔥", "name": "Phoenix", "phase": "Final", "phase_emoji": "⭐"},
]
CELEBRATION = {"emoji": "🎊", "name": "Complete!", "phase": "Done", "phase_emoji": "✨"}
def detect_terminal():
"""Detect terminal type for compatibility."""
term = os.environ.get("TERM", "")
term_program = os.environ.get("TERM_PROGRAM", "")
inside_tmux = os.environ.get("TMUX", "") != ""
return {
"ghostty": "ghostty" in term_program.lower() or "ghostty" in term.lower(),
"tmux": inside_tmux,
"term": term,
"term_program": term_program
}
def get_animal_index(remaining):
"""Get current animal based on remaining time."""
hours_left = remaining // 3600
index = max(0, min(8, 8 - hours_left))
return index
def get_phase_color(progress, paused):
"""Return color pair based on time phase."""
if paused:
return 5 # Cyan for paused
if progress > 0.8:
return 2 # Green - Dawn
elif progress > 0.65:
return 3 # Yellow - Morning
elif progress > 0.5:
return 2 # Green - Midday
elif progress > 0.35:
return 3 # Yellow - Afternoon
elif progress > 0.2:
return 4 # Magenta - Evening
else:
return 1 # Red - Night/Final
def format_time(seconds):
"""Format seconds into HH:MM:SS."""
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def draw_timer(stdscr, remaining, total, paused, prev_hour, term_info):
"""Draw the timer interface with layers."""
stdscr.erase()
max_y, max_x = stdscr.getmaxyx()
h = min(6, max_y)
w = min(40, max_x)
progress = remaining / total if total > 0 else 0
animal_idx = get_animal_index(remaining)
animal = ANIMALS[animal_idx]
win = curses.newwin(h, w, 0, 0)
win.erase()
win.border()
# Layer 1: Animal + Hour + Time
current_hour = 9 - (remaining // 3600)
if term_info["tmux"]:
# Simpler display for tmux - use ASCII fallbacks if needed
animal_str = f"{animal['emoji']} Hr{current_hour}/9"
else:
animal_str = f"{animal['emoji']} Hour {current_hour}/9"
time_str = format_time(remaining)
color = get_phase_color(progress, paused)
# Title with animal
try:
win.attron(curses.color_pair(6))
win.addstr(0, 2, animal_str[:15])
win.attroff(curses.color_pair(6))
except curses.error:
pass
# Time display (right-aligned)
try:
win.attron(curses.color_pair(color) | curses.A_BOLD)
win.addstr(0, w - len(time_str) - 2, time_str)
win.attroff(curses.color_pair(color) | curses.A_BOLD)
except curses.error:
pass
# Layer 2: Main progress bar with gradient
bar_width = w - 10
filled = int(bar_width * progress)
empty = bar_width - filled
bar_x = 2
try:
win.attron(curses.color_pair(color))
win.addstr(1, bar_x, "█" * filled)
win.attroff(curses.color_pair(color))
win.attron(curses.color_pair(7))
win.addstr(1, bar_x + filled, "░" * empty)
win.attroff(curses.color_pair(7))
pct = f"{int(progress * 100)}%"
win.addstr(1, w - 5, pct)
except curses.error:
pass
# Layer 3: Decorative gradient blocks
block_width = (w - 4) // 2
filled2 = int(block_width * progress)
empty2 = block_width - filled2
try:
win.attron(curses.color_pair(color))
win.addstr(2, 2, "▓" * filled2)
win.attroff(curses.color_pair(color))
win.attron(curses.color_pair(7))
win.addstr(2, 2 + filled2, "░" * empty2)
win.attroff(curses.color_pair(7))
except curses.error:
pass
# Layer 4: Phase indicator with divider
phase_emoji = animal['phase_emoji']
phase_name = animal['phase']
divider_len = w - 16
left_bar = int(divider_len * progress)
right_bar = divider_len - left_bar
try:
win.attron(curses.color_pair(6))
win.addstr(3, 1, f" {phase_emoji} {phase_name[:8]}")
win.attroff(curses.color_pair(6))
win.attron(curses.color_pair(color))
win.addstr(3, 11, "─" * left_bar)
win.attroff(curses.color_pair(color))
win.attron(curses.color_pair(7))
win.addstr(3, 11 + left_bar, "─" * right_bar)
win.attroff(curses.color_pair(7))
win.attron(curses.color_pair(6))
win.addstr(3, w - 9, "Dusk 🌙")
win.attroff(curses.color_pair(6))
except curses.error:
pass
# Layer 5: Controls
controls = "[p]ause [+/-]adj [q]uit"
ctrl_x = max(1, (w - len(controls)) // 2)
try:
win.attron(curses.color_pair(6))
win.addstr(4, ctrl_x, controls)
win.attroff(curses.color_pair(6))
except curses.error:
pass
# Layer 6: Animal name decoration at bottom
name_display = f"~ {animal['name']} ~"
name_x = max(1, (w - len(name_display)) // 2)
try:
win.attron(curses.color_pair(color) | curses.A_DIM)
win.addstr(5, name_x, name_display[:w-2])
win.attroff(curses.color_pair(color) | curses.A_DIM)
except curses.error:
pass
win.refresh()
def show_completion(stdscr, term_info):
"""Show completion screen."""
stdscr.erase()
max_y, max_x = stdscr.getmaxyx()
h = min(6, max_y)
w = min(40, max_x)
win = curses.newwin(h, w, 0, 0)
win.erase()
win.border()
try:
msg = "🎊 COMPLETE! 🎊"
msg_x = max(1, (w - len(msg)) // 2)
win.attron(curses.color_pair(3) | curses.A_BOLD)
win.addstr(2, msg_x, msg)
win.attroff(curses.color_pair(3) | curses.A_BOLD)
sub_msg = "Time's up!"
sub_x = max(1, (w - len(sub_msg)) // 2)
win.attron(curses.color_pair(2))
win.addstr(3, sub_x, sub_msg)
win.attroff(curses.color_pair(2))
ctrl_msg = "Press any key to exit"
ctrl_x = max(1, (w - len(ctrl_msg)) // 2)
win.attron(curses.color_pair(6))
win.addstr(4, ctrl_x, ctrl_msg)
win.attroff(curses.color_pair(6))
except curses.error:
pass
win.refresh()
def main(stdscr):
"""Main timer function."""
# Detect terminal
term_info = detect_terminal()
# Setup curses
curses.curs_set(0)
stdscr.nodelay(True)
stdscr.timeout(500)
# Enable colors
curses.start_color()
curses.use_default_colors()
# Define color pairs
curses.init_pair(1, curses.COLOR_RED, -1)
curses.init_pair(2, curses.COLOR_GREEN, -1)
curses.init_pair(3, curses.COLOR_YELLOW, -1)
curses.init_pair(4, curses.COLOR_MAGENTA, -1)
curses.init_pair(5, curses.COLOR_CYAN, -1)
curses.init_pair(6, curses.COLOR_BLUE, -1)
curses.init_pair(7, curses.COLOR_WHITE, -1)
remaining = TOTAL_SECONDS
paused = False
last_time = time.time()
prev_hour = 9
# Handle terminal resize (important for tmux)
def handle_resize(sig, frame):
stdscr.clear()
stdscr.refresh()
try:
signal.signal(signal.SIGWINCH, handle_resize)
except (ValueError, OSError):
pass
try:
while remaining >= 0:
current_hour = 9 - (remaining // 3600)
# Beep on hour change
if current_hour != prev_hour and remaining > 0:
try:
curses.beep()
except:
pass
prev_hour = current_hour
draw_timer(stdscr, remaining, TOTAL_SECONDS, paused, prev_hour, term_info)
# Get input
try:
key = stdscr.getch()
except:
key = -1
# Handle keys
if key == ord('q') or key == ord('Q'):
break
elif key == ord('p') or key == ord('P'):
paused = not paused
if not paused:
last_time = time.time()
elif key == ord('+') or key == ord('='):
remaining = min(remaining + 60, TOTAL_SECONDS)
elif key == ord('-') or key == ord('_'):
remaining = max(remaining - 60, 0)
# Update time if not paused
if not paused:
current_time = time.time()
elapsed = current_time - last_time
if elapsed >= 1.0:
remaining -= int(elapsed)
last_time = current_time
remaining = max(0, remaining)
time.sleep(0.05)
finally:
# Show completion screen
if remaining <= 0:
show_completion(stdscr, term_info)
try:
curses.beep()
time.sleep(0.3)
curses.beep()
except:
pass
stdscr.nodelay(False)
try:
stdscr.getch()
except:
pass
if __name__ == "__main__":
try:
curses.wrapper(main)
except KeyboardInterrupt:
pass