-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmas_tool.py
403 lines (343 loc) · 11.1 KB
/
xmas_tool.py
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from __future__ import annotations
import curses
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
import random
import time
import uuid
def time_until_christmas(timezone_offset: int):
now_utc = datetime.now(timezone.utc)
# Apply the timezone offset
offset = timedelta(hours=timezone_offset)
now_local = now_utc + offset
# Determine the year for the next Christmas
current_year = now_local.year
christmas_this_year = datetime(current_year, 12, 25, 0, 0, 0, tzinfo=timezone(offset))
# Check if Christmas has already passed this year
if now_local >= christmas_this_year:
next_christmas = datetime(current_year + 1, 12, 25, 0, 0, 0, tzinfo=timezone(offset))
else:
next_christmas = christmas_this_year
# Calculate the time difference
delta = next_christmas - now_local
# Extract days, hours, minutes, and seconds
days = delta.days
seconds_in_day = delta.seconds
hours = seconds_in_day // 3600
minutes = (seconds_in_day % 3600) // 60
seconds = (seconds_in_day % 3600) % 60
return {
"days": days,
"hours": hours,
"minutes": minutes,
"seconds": seconds,
}
class Canvas:
def __init__(self, nrows, ncols):
self.nrows = nrows
self.ncols = ncols
self.elements = {}
def upsert(self, canvas_element: CanvasElement):
self.elements[canvas_element.id] = canvas_element
def remove(self, canvas_element: CanvasElement):
if element.name in self.elements:
removed_element = self.elements.pop(element.name)
else:
print(f"No element found with name '{element.name}'.")
def get_element(self, name: str):
return self.elements.get(name, None)
def render(self):
"""Renders the canvas as a string and prints it."""
canvas = [[' ' for _ in range(self.ncols)]
for _ in range(self.nrows)]
element_list = sorted(
[e for k, e in self.elements.items()],
key= lambda elem: elem.z
)
for element in element_list:
for cell in element.cell_list:
if not Cell.in_bounds(cell, self.nrows, self.ncols):
continue
canvas[cell.x][cell.y] = cell.c
return canvas
@dataclass
class Cell:
x: int
y: int
c: str
@staticmethod
def in_bounds(cell: Cell, nrows: int, ncols: int) -> Bool:
if cell.x >= 0 and cell.x < nrows and cell.y >= 0 and cell.y < ncols:
return True
return False
class CanvasElement:
def __init__(self, z: int, cell_list: [Cell]):
self.id = str(uuid.uuid4())
self.z = z
self.cell_list = cell_list
@staticmethod
def merge(element1: CanvasElement, element2: CanvasElement) -> CanvasElement:
return CanvasElement(
z=element1.z,
cell_list = element1.cell_list + element2.cell_list
)
class Message:
def __init__(
self,
start_row: int,
start_col: int,
message: str):
self.start_row = start_row
self.start_col = start_col
self.message = message
self.message_length = len(message)
self.cell_list = self._build()
def _build(self):
cell_list = [
Cell(x=self.start_row, y=y, c=self.message[i])
for i, y in enumerate(
range(self.start_col, self.start_col + self.message_length
)
)]
return cell_list
class Box:
TOP_LEFT_CORNER = '╭'
TOP_RIGHT_CORNER = '╮'
BOTTOM_LEFT_CORNER = '╰'
BOTTOM_RIGHT_CORNER = '╯'
HORIZONTAL_EDGE = '─'
VERTICAL_EDGE = '│'
def __init__(
self,
start_row: int,
end_row: int,
start_col: int,
end_col: int,
filled: bool):
self.start_row = start_row
self.end_row = end_row
self.start_col = start_col
self.end_col = end_col
self.filled = filled
self.cell_list = self._build()
def _build(self):
cell_list = []
if self.filled:
for i in range(self.start_row + 1, self.end_row):
for j in range(self.start_col + 1, self.end_col):
cell_list.append(
Cell(x=i, y=j, c=' ')
)
# Top edge
cell_list.append(
Cell(
x=self.start_row,
y=self.start_col,
c=self.TOP_LEFT_CORNER
)
)
cell_list.append(
Cell(
x=self.start_row,
y=self.end_col,
c=self.TOP_RIGHT_CORNER
)
)
for i in range(self.start_col+1, self.end_col):
cell_list.append(
Cell(x=self.start_row, y=i, c=self.HORIZONTAL_EDGE)
)
# Bottom edge
cell_list.append(
Cell(
x=self.end_row,
y=self.start_col,
c=self.BOTTOM_LEFT_CORNER
)
)
cell_list.append(
Cell(
x=self.end_row,
y=self.end_col,
c=self.BOTTOM_RIGHT_CORNER
)
)
for i in range(self.start_col+1, self.end_col):
cell_list.append(
Cell(x=self.end_row, y=i, c=self.HORIZONTAL_EDGE)
)
# Left and right edges
for i in range(self.start_row + 1, self.end_row):
cell_list.append(
Cell(x=i, y=self.start_col, c=self.VERTICAL_EDGE)
)
cell_list.append(
Cell(x=i, y=self.end_col, c=self.VERTICAL_EDGE)
)
return cell_list
class XmasTree:
def __init__(self, start_row: int, start_col: int, height: int):
self.start_row = start_row
self.start_col = start_col
self.height = height
self.width = 2 * height - 1
self.cell_list = self._build()
self._add_ornaments(9)
def _build(self):
cell_list = []
cell_list.append(
Cell(x=self.start_row, y=self.start_col + self.width // 2, c='✪')
)
# Build tree leaves
for i in range(1,self.height):
mid = self.width // 2
for j in range(mid - i, mid + i + 1):
cell_list.append(
Cell(x=self.start_row + i, y=self.start_col + j, c ='*')
)
# Build tree trunk
trunk_width = self.height // 3
trunk_width = trunk_width if trunk_width % 2 == 1 else trunk_width + 1
trunk_height = self.height // 4
trunk_start = self.width // 2 - trunk_width // 2
for i in range(self.height, self.height + trunk_height):
for j in range(trunk_start, trunk_start + trunk_width):
cell_list.append(
Cell(x=self.start_row + i, y=self.start_col + j, c ='*')
)
return cell_list
def _add_ornaments(self, n: int):
"""Randomly add 'O' ornaments to the tree leaves.
:param n: Number of ornaments to add
"""
num_cell = len(self.cell_list)
for _ in range(n):
while True:
i = random.randint(1, num_cell-1)
if self.cell_list[i].c == '*':
self.cell_list[i].c = 'O'
break
class CanvasElementFactory:
@staticmethod
def create_box(
start_row: int,
end_row: int,
start_col: int,
end_col: int,
filled: bool,
z: int):
box = Box(
start_row=start_row,
end_row=end_row,
start_col=start_col,
end_col=end_col,
filled=filled)
return CanvasElement(z=z, cell_list=box.cell_list)
@staticmethod
def create_message(
start_row: int,
start_col: int,
message: str,
z: int):
message = Message(
start_row = start_row,
start_col = start_col,
message=message)
return CanvasElement(z=z, cell_list=message.cell_list)
@staticmethod
def create_xmas_tree(
start_row: int,
start_col: int,
height: int,
z: int):
tree = XmasTree(
height=height,
start_row=start_row,
start_col=start_col)
return CanvasElement(z=z, cell_list=tree.cell_list)
def xmas(mouth_open=False):
nrows = 20
ncols = 70
canvas = Canvas(nrows, ncols)
factory = CanvasElementFactory
message = factory.create_message(
start_row = 0,
start_col = 5,
message='🅧🅜🅐🅢',
z=10)
border = factory.create_box(
start_row = 0,
end_row = nrows - 1,
start_col = 0,
end_col = ncols - 1,
filled = False,
z=5)
tree = factory.create_xmas_tree(
height = 15,
start_row = 2,
start_col = 2,
z=1)
# build face out of box and message
face = factory.create_box(
start_row = 7,
end_row = 11,
start_col = 9,
end_col = 23,
filled = True,
z=4)
if not mouth_open:
eyes = factory.create_message(
start_row = 9,
start_col = 15,
message='。◕◡◕。',
z=4)
else:
eyes = factory.create_message(
start_row = 9,
start_col = 15,
message='。◕▿◕。',
z=4)
face = CanvasElement.merge(face, eyes)
instructions = factory.create_message(
start_row = nrows - 2,
start_col = 43,
message='q - quit',
z=10)
# EST is -5
until_xmas = time_until_christmas(-5)
count_down = factory.create_message(
start_row = 10,
start_col = 30,
message=f"{until_xmas['days']} days, {until_xmas['hours']} hours, "\
f"{until_xmas['minutes']} minutes, {until_xmas['seconds']} seconds",
z = 10
)
canvas.upsert(border)
canvas.upsert(message)
canvas.upsert(tree)
canvas.upsert(face)
canvas.upsert(instructions)
canvas.upsert(count_down)
state = canvas.render()
return state
def animated_loop(stdscr):
curses.curs_set(0) # Hide the cursor
stdscr.nodelay(True) # Non-blocking input
fps = 5
frame = 0
while True:
canvas = xmas(
(frame % 2 == 0) or (frame % 3 == 0) or (frame % 5 == 0)
)
# Now draw them on the curses screen
for i, row in enumerate(canvas):
stdscr.addstr(i, 0, ''.join(row))
stdscr.refresh()
time.sleep(1.0/fps)
frame += 1 % 100
key = stdscr.getch()
if key == ord('q'):
break
# Example usage:
if __name__ == "__main__":
curses.wrapper(animated_loop)