Skip to content

Instantly share code, notes, and snippets.

@nogweii
Forked from mehdi-farsi/xmas_tree.rb
Created December 22, 2018 20:50
Show Gist options
  • Save nogweii/95e530853829495fc86870dff26d17b7 to your computer and use it in GitHub Desktop.
Save nogweii/95e530853829495fc86870dff26d17b7 to your computer and use it in GitHub Desktop.
A light-up christmas tree in your terminal
class ChristmasTree
FIRST_LINE_LENGTH = 1
LEVEL_HEIGHT = 3
COLORS = ["\033[0m", "\033[0m", "\033[0m", "\033[31m", "\033[33m", "\033[32m", "\033[34m"] # red, yellow, blue
def initialize(levels)
@levels = levels.to_i < 1 ? 3 : levels.to_i
@last_line_length = 5 + ((@levels - 1) * 2)
@current_line_length = FIRST_LINE_LENGTH
end
def draw_level
(1..LEVEL_HEIGHT).each do |line|
row = ("*" * @current_line_length).center(@last_line_length)
row.gsub!(/\*/) { |star| COLORS.sample + '*' + COLORS[0] }
puts row.center(@last_line_length)
@current_line_length += 2 unless line == 3
end
@current_line_length = @current_line_length - 2
end
def color_center(str)
#
end
def draw_trunk
trunk_width = trunk_height = ((@last_line_length / 2) - 2).round
(1..trunk_height).each { puts ("|" * trunk_width).center(@last_line_length) }
end
def draw
(1..@levels).each { draw_level }
draw_trunk
end
end
while true
print "\033[2J"
ChristmasTree.new(ARGV.first).draw
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment