Last active
February 11, 2016 18:27
-
-
Save jeremywrnr/b12f6a6bd5048e10a066 to your computer and use it in GitHub Desktop.
Colors in your shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# add colors | |
class String | |
def colorize(bg) | |
"\e[0;0;#{bg}m#{self}\e[0;0m" | |
end | |
end | |
# TVTVTV | |
class TV | |
@Version = 0.1 | |
def initialize(args) | |
@colorwidth = 10 | |
@width = 100 | |
setup_colors | |
opts args | |
parse args | |
end | |
private | |
def opts(args) | |
args.select { |arg| arg[0] == "-" }.map do |arg| | |
case arg | |
when "-v", "--version" | |
@vers = true | |
when "--wavy" | |
@wavy = true | |
when "--test" | |
@test = true | |
end | |
end | |
end | |
def parse(args) | |
args = args.select { |arg| arg[0] != "-" } | |
if @test | |
set_bg args.first unless args.empty? | |
testing | |
elsif @vers | |
puts @Version | |
else | |
set_bg args.first | |
endless | |
end | |
end | |
def setup_colors | |
blk = 40 | |
red = 41 | |
grn = 42 | |
yel = 43 | |
blu = 44 | |
ppl = 45 | |
cyn = 46 | |
gry = 47 | |
pnk = 101 | |
wht = 107 | |
# backgrounds | |
@patts = {} | |
@patts['bw'] = [blk, blk, wht, blk, wht, blk, blk, wht, wht] | |
@patts['xmas'] = [grn, grn, red, red, grn, grn, red, red, red] | |
@patts['error'] = [wht, gry, yel, cyn, grn, pnk, red, blu, blk] | |
@patts['rasta'] = [blk, grn, grn, yel, yel, red, red, blk, blk] | |
@patts['pride'] = [blk, ppl, blu, cyn, grn, yel, pnk, red, blk] | |
# pride for testing! | |
@patt = @patts['pride'] | |
@colors = @patt.length | |
end | |
def tvline | |
@patt.each do |bg| | |
space = " " * (@colorwidth + 1) | |
print space.colorize bg | |
end | |
sleep 0.01 # still tunable... | |
end | |
def set_bg(color) | |
if @patts.keys.include? color | |
@patt = @patts[color] | |
else | |
helper | |
end | |
end | |
def helper | |
print "Select a pattern (" << @patts.keys.join(", ") << "): " | |
parse [] << $stdin.gets.chomp | |
end | |
def testing | |
100.times { |_| tvline } | |
end | |
def endless | |
puts "Turning on the TV... press Ctrl-C to quit." | |
sleep 1.5 | |
begin | |
while true do tvline end | |
rescue Interrupt => e | |
puts "\nThanks for watching!" | |
exit 0 | |
end | |
end | |
end | |
# run from args | |
TV.new(ARGV) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment