Skip to content

Instantly share code, notes, and snippets.

@jatinkrmalik
Last active December 25, 2024 20:14
Show Gist options
  • Save jatinkrmalik/4ee7b0746f76074ce7f2f7117b89fd12 to your computer and use it in GitHub Desktop.
Save jatinkrmalik/4ee7b0746f76074ce7f2f7117b89fd12 to your computer and use it in GitHub Desktop.
Colorful Christmas Tree Generator in Python
"""
Christmas Tree Terminal Program
This program prints a festive Christmas tree in your terminal based on the height of your terminal or a specified height.
It also displays a large "Merry Christmas!" greeting below the tree.
### How to Run:
1. Install dependencies:
pip install colorama pyfiglet
2. Run the script:
python script.py [height]
- [height] (optional): Specify the height of the tree as a positive integer.
If not provided, the program will calculate an optimal height based on your terminal size.
3. Example:
python christmas_tree.py 15
python christmas_tree.py
### Features:
- Automatically adjusts tree height based on terminal size if no height is specified.
- Displays "Merry Christmas!" in large ASCII text at the bottom.
"""
import sys
import os
from colorama import Fore, Back, Style, init
import random
from pyfiglet import Figlet
# Initialize colorama
init(autoreset=True)
def get_terminal_height():
"""Get the height of the terminal."""
try:
return os.get_terminal_size().lines
except OSError:
# Default to 24 lines if terminal size cannot be determined
return 24
def print_christmas_tree(height):
"""
Print a pyramid-shaped Christmas tree with ornaments and a trunk.
Args:
height (int): The height of the tree.
"""
# Define colors for ornaments and tree elements
ornament_colors = [Fore.RED, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN]
tree_color = Fore.GREEN
trunk_color = Fore.YELLOW + Back.BLACK
# Print the tree body
for i in range(height):
spaces = " " * (height - i - 1) # Spaces to center each row
row = ""
for j in range(2 * i + 1): # Number of stars/ornaments per row
if random.random() < 0.2: # 20% chance to place an ornament
row += random.choice(ornament_colors) + "o"
else:
row += tree_color + "*"
print(spaces + row)
# Print the trunk
trunk_width = height // 3 if height > 4 else 1 # Trunk width scales with tree height
trunk_height = max(2, height // 5) # Minimum trunk height is 2 lines
trunk_space = " " * (height - trunk_width // 2 - 1) # Center the trunk
for _ in range(trunk_height):
print(trunk_space + trunk_color + "|" * trunk_width)
def print_merry_christmas():
"""
Print 'Merry Christmas!' in large ASCII text.
"""
figlet = Figlet(font='slant') # You can change the font style here
print(Fore.YELLOW + Style.BRIGHT + figlet.renderText("Merry Christmas!"))
# Main function
if __name__ == "__main__":
# Check for command-line arguments or provide help message
if len(sys.argv) > 1:
if sys.argv[1].lower() in ["-h", "--help"]:
print(Fore.CYAN + """
Usage: python script.py [height]
Options:
- [height]: Specify the height of the Christmas tree as a positive integer.
If omitted, the program calculates an optimal height based on your terminal size.
Examples:
- python script.py # Automatically adjusts tree size to terminal dimensions.
- python script.py 15 # Prints a Christmas tree with a height of 15.
Enjoy and Merry Christmas! ๐ŸŽ„
""")
sys.exit(0)
try:
tree_height = int(sys.argv[1])
if tree_height <= 0:
raise ValueError("Height must be a positive integer.")
except ValueError:
print(Fore.RED + "Invalid height argument. Please provide a positive integer.")
sys.exit(1)
else:
# Calculate optimal height based on terminal size
terminal_height = get_terminal_height()
tree_height = max(terminal_height - 25, 5) # Leave space for decorations and message
# Print the pyramid-shaped Christmas tree
print_christmas_tree(tree_height)
# Print Merry Christmas message at the bottom
print_merry_christmas()
@jatinkrmalik
Copy link
Author

Merry Christmas, folks! ๐ŸŽ„

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment