class TV

TVTVTV

Public Class Methods

new(args) click to toggle source
# File lib/tv.rb, line 12
def initialize(args)
  setup_colors
  opts args
  resize
  parse args
end

Private Instance Methods

current_width() click to toggle source

get int number of columns in half of screen

# File lib/tv.rb, line 82
def current_width
  guess = `tput cols`.to_i
  guess == 0 ? 80 : guess
end
endless() click to toggle source
# File lib/tv.rb, line 135
def endless
  puts "Turning on the TV... press Ctrl-C to quit."
  sleep 1

  begin
    while true do tvline end
  rescue Interrupt => e
    puts "\nThanks for watching!"
    exit 0
  end
end
helper() click to toggle source
# File lib/tv.rb, line 126
def helper
  print "Select a pattern (" << @patts.keys.join(", ") << "): "
  parse [] << $stdin.gets.chomp
end
offset() click to toggle source

makes offset sample avg = offset difference. for example, if we have a final gap of 3 characters, and a color width of 5, we would want to add 1 to the width of three colors. on average, we can achieve this by sampling from an array whose average is 3/5 - or [0,0,1,1,1].

in general, the array of 0s and 1s whose average is equal to a rational fraction x/y such that (x/y) < 1 and >= 0 is composed of y-x 0s and x 1s.

# File lib/tv.rb, line 94
def offset
  off = []

  ones = @width % @colorwidth
  zeros = @colorwidth - ones

  ones.times { |_| off << 1 }
  zeros.times { |_| off << 0 }

  off
end
opts(args) click to toggle source
# File lib/tv.rb, line 20
def opts(args)
  args.select { |arg| arg.chr == "-" }.map do |arg|
    case arg
    when "-v", "--version"
      @vers = true
    when "--wavy"
      @wavy = true
    when "--test"
      @test = true
    end
  end
end
parse(args) click to toggle source
# File lib/tv.rb, line 33
def parse(args)
  args = args.select { |arg| arg.chr != "-" }

  if @test
    set_bg args.first unless args.empty?
    testing

  elsif @vers
    puts @Version

  else
    set_bg args.first
    endless
  end
end
resize() click to toggle source
# File lib/tv.rb, line 74
def resize
  @width = current_width
  @colorwidth = (@width / @colors).floor
  @offset = @wavy? offset : [1]
end
set_bg(color) click to toggle source
# File lib/tv.rb, line 118
def set_bg(color)
  if @patts.keys.include? color
    @patt = @patts[color]
  else
    helper
  end
end
setup_colors() click to toggle source
# File lib/tv.rb, line 49
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
testing() click to toggle source
# File lib/tv.rb, line 131
def testing
  100.times { |_| tvline }
end
tvline() click to toggle source
# File lib/tv.rb, line 106
def tvline
  resize unless @width == current_width

  @patt.each do |bg|
    space = " " * (@colorwidth + @offset.sample)

    print space.colorize bg
  end

  sleep 0.01 # still tunable...
end