-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathterminal_tree.py
97 lines (82 loc) · 2.97 KB
/
terminal_tree.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
import argparse
import os
import random
import time
BALL = '⏺'
COLOR = {
'blue': '\033[94m',
'yellow': '\033[93m',
'cyan': '\033[96m',
'green': '\033[92m',
'magenta': '\033[95m',
'white': '\033[97m',
'red': '\033[91m'
}
STAR = '★'
def random_change_char(string, value):
indexes = random.sample(range(0, len(string)), value)
string = list(string)
for idx in indexes:
if string[idx] != ' ' and string[idx] == '_':
string[idx] = BALL
return ''.join(string)
def tree(height=13, screen_width=80):
star = (STAR, 3*STAR)
if height % 2 != 0:
height += 1
body = ['/_\\', '/_\_\\']
trunk = '[___]'
begin = '/'
end = '\\'
pattern = '_/'
j = 5
for i in range(7, height + 1, 2):
middle = pattern + (i - j) * pattern
line = ''.join([begin, middle[:-1], end])
body.append(line)
middle = middle.replace('/', '\\')
line = ''.join([begin, middle[:-1], end])
body.append(line)
j += 1
return [line.center(screen_width) for line in (*star, *body, trunk)]
def balls(tree):
for idx, _ in enumerate(tree[:-3], 2):
tree[idx] = random_change_char(tree[idx], len(tree[idx])//8)
return tree
def colored_stars_balls(tree):
for idx, _ in enumerate(tree):
string = list(tree[idx])
for pos, _ in enumerate(string):
if string[pos] == STAR:
string[pos] = ''.join([COLOR['yellow'], STAR, '\033[0m'])
elif string[pos] == BALL:
string[pos] = ''.join([random.choice(list(COLOR.values())), BALL, '\033[0m'])
tree[idx] = ''.join(string)
return tree
def cli():
parser = argparse.ArgumentParser(prog="Python Christmas Tree by Chico Lucio from Ciencia Programada",
epilog="Ctrl-C interrupts the Christmas :-(")
parser.add_argument('-s', '--size', default=13, type=int,
help="Tree height. If even it will be subtracted 1. If less than 7, considered 5. Default: 13")
parser.add_argument('-w', '--width', default=80, type=int,
help="Screen width. Used to center the tree. Default: 80")
parser.add_argument('-t', '--terminal', action='store_true',
help="Uses the terminal size to center the tree. -s and -w will be ignored")
args = parser.parse_args()
if args.terminal:
screen_width, height = os.get_terminal_size()
height -= 2
else:
height = args.size
screen_width = args.width
while True:
try:
time.sleep(random.uniform(.1, 1))
os.system('cls' if os.name == 'nt' else 'clear')
print('\n'.join(colored_stars_balls(balls(tree(height, screen_width)))))
except KeyboardInterrupt:
os.system('cls' if os.name == 'nt' else 'clear')
print(f"\n{'Merry Christmas!!':^{screen_width}}", end='\n\n')
break
if __name__ == '__main__':
cli()