diff --git a/terminal_tree.py b/terminal_tree.py index 2b75d4b..702af80 100644 --- a/terminal_tree.py +++ b/terminal_tree.py @@ -3,44 +3,56 @@ import random import time -BALL = '⏺' +BALL = "⏺" COLOR = { - 'blue': '\033[94m', - 'yellow': '\033[93m', - 'cyan': '\033[96m', - 'green': '\033[92m', - 'magenta': '\033[95m', - 'white': '\033[97m', - 'red': '\033[91m' + "blue": "\033[94m", + "yellow": "\033[93m", + "cyan": "\033[96m", + "green": "\033[92m", + "magenta": "\033[95m", + "white": "\033[97m", + "red": "\033[91m", } -STAR = '★' +if os.name == "nt": + # BALL = "\u2022" + BALL = "\u25C9" + COLOR = { + "blue": "", + "yellow": "", + "cyan": "", + "green": "", + "magenta": "", + "white": "", + "red": "", + } +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] == '_': + if string[idx] != " " and string[idx] == "_": string[idx] = BALL - return ''.join(string) + return "".join(string) def tree(height=13, screen_width=80): - star = (STAR, 3*STAR) + star = (STAR, 3 * STAR) if height % 2 != 0: height += 1 - body = ['/_\\', '/_\_\\'] - trunk = '[___]' - begin = '/' - end = '\\' - pattern = '_/' + 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]) + line = "".join([begin, middle[:-1], end]) body.append(line) - middle = middle.replace('/', '\\') - line = ''.join([begin, middle[:-1], end]) + middle = middle.replace("/", "\\") + line = "".join([begin, middle[:-1], end]) body.append(line) j += 1 @@ -49,7 +61,7 @@ def tree(height=13, screen_width=80): def balls(tree): for idx, _ in enumerate(tree[:-3], 2): - tree[idx] = random_change_char(tree[idx], len(tree[idx])//8) + tree[idx] = random_change_char(tree[idx], len(tree[idx]) // 8) return tree @@ -58,22 +70,40 @@ def colored_stars_balls(tree): string = list(tree[idx]) for pos, _ in enumerate(string): if string[pos] == STAR: - string[pos] = ''.join([COLOR['yellow'], STAR, '\033[0m']) + 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) + 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") + 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: @@ -84,14 +114,14 @@ def cli(): 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))))) + time.sleep(random.uniform(0.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') + os.system("cls" if os.name == "nt" else "clear") + print(f"\n{'Merry Christmas!!':^{screen_width}}", end="\n\n") break -if __name__ == '__main__': +if __name__ == "__main__": cli()