Skip to content

Instantly share code, notes, and snippets.

@cattywampus
Last active March 3, 2016 19:50
Show Gist options
  • Save cattywampus/951e9c0c18fcf812f56f to your computer and use it in GitHub Desktop.
Save cattywampus/951e9c0c18fcf812f56f to your computer and use it in GitHub Desktop.
A colored and animated American flag in Ruby
#!/usr/bin/env ruby
def red(string)
"\e[37;41m#{string}\e[0m"
end
def white(string)
"\e[30;47m#{string}\e[0m"
end
def blue(string)
"\e[1;37;44m#{string}\e[0m"
end
max_width = `tput cols`.to_i
stars_width = max_width * 0.40
spacing = (stars_width / 6).to_i
flag = []
(0..25).step(2).each_with_index do |row, index|
flag[row] = (" " * max_width).split(//).map { |c| index.even? ? red(c) : white(c) }
flag[row+1] = (" " * max_width).split(//).map { |c| index.even? ? red(c) : white(c) }
end
14.times do |row|
stars_width.to_i.times do |col|
char = " "
case row % 3
when 0
char = "*" if (col - (spacing/2)) % spacing == 0
when 1
char = "*" if row != 13 && col >= spacing && col % spacing == 0
end
flag[row][col] = blue(char)
end
end
throw = 2
increment = 0
step = 0.75
loop do
canvas = []
(flag.length + 2 * throw).times do |x|
canvas[x] = []
max_width.times do |y|
canvas[x][y] = " "
end
end
flag.each_with_index do |row, rindex|
row.each_with_index do |col, cindex|
x = (cindex / row.length.to_f) * (2 * Math::PI) + increment
offset = Math.sin(x) * throw
position = rindex + throw + offset
canvas[position][cindex] = flag[rindex][cindex]
end
end
canvas.each do |row|
puts row.join
end
increment += step
sleep 0.25
end
@cattywampus
Copy link
Author

Added a simple animation using a sine wave
https://www.youtube.com/watch?v=MOcmBTGmfbk

Copy link

ghost commented Jan 3, 2016

Expressing patriotism from within a terminal. It doesn't get any better than this!

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