class Vamp::Animator

play animation on console

Attributes

ani_color[RW]
data[RW]
height[RW]
number[RW]
scroll_counter[RW]
scroll_text[RW]
text_color[RW]
width[RW]

Public Class Methods

new(file, number = 31, start = 0, height = number, scroll_text = nil, cycles = 1) click to toggle source
# File lib/vamp/animator.rb, line 16
def initialize(file, number = 31, start = 0, height = number, scroll_text = nil, cycles = 1)
  @data = []
  @number = number
  @height = height - start
  @scroll_text = scroll_text
  @cycles = cycles
  @width = detect_terminal_width || 80
  if scroll_text
    @height += 1
    @scroll_counter = 0
    @scroll_text = (" " * @width) + scroll_text + (" " * @width)
  end
  @ani_color = "red"
  @text_color = "blue"
  lines = IO.readlines(file)
  lines.each_slice(number) do |block|
    d = []
    block.each_with_index do |line, index|
      d << (line.rstrip + (" " * 80))[0..(width - 1)] if index >= start
      break if index >= height
    end
    @data << d
  end
end

Public Instance Methods

animate(msg) click to toggle source
# File lib/vamp/animator.rb, line 62
def animate(msg)
  home
  if ani_color
    puts Vamp::Colorize.colorize(ani_color, msg)
  else
    puts msg
  end
  if scroll_text
    if text_color
      puts Vamp::Colorize.colorize(text_color, running_line)
    else
      puts running_line
    end
  end
  flush
  sleep(1.0/48.0)
end
clear() click to toggle source
# File lib/vamp/animator.rb, line 41
def clear
  print "\e[H\e[2J"
end
command_exists?(command) click to toggle source

Determines if a shell command exists by searching for it in ENV.

# File lib/vamp/animator.rb, line 135
def command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
end
cursor_off() click to toggle source
# File lib/vamp/animator.rb, line 85
def cursor_off
  print "\e[H\e[?25l"
end
cursor_on() click to toggle source
# File lib/vamp/animator.rb, line 89
def cursor_on
  print "\e[H\e[?25h"
end
detect_terminal_width() click to toggle source

stolen from <github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb> Returns width of terminal when detected, nil if not detected.

# File lib/vamp/animator.rb, line 120
def detect_terminal_width
  if ENV['COLUMNS'] =~ /^\d+$/
    ENV['COLUMNS'].to_i
  elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
    `tput cols`.to_i
  elsif STDIN.tty? && command_exists?('stty')
    `stty size`.scan(/\d+/)[1].to_i
  else
    nil
  end
rescue
  nil
end
down(lines = height) click to toggle source
# File lib/vamp/animator.rb, line 49
def down(lines = height)
  # lines.times { puts }
  print "\e[H\e[#{lines}E"
end
finished?() click to toggle source
# File lib/vamp/animator.rb, line 108
def finished?
  if scroll_text
    scroll_offset + width >= scroll_text.size
  else
    @cycle ||= 0
    @cycle += 1
    @cycle > @cycles
  end
end
flush() click to toggle source
# File lib/vamp/animator.rb, line 54
def flush
  $stdout.flush
end
home(lines = height) click to toggle source
# File lib/vamp/animator.rb, line 45
def home(lines = height)
  print "\e[H\e[#{lines}F"
end
play() click to toggle source
# File lib/vamp/animator.rb, line 93
def play
  if $stdout.isatty
    begin
      cursor_off
      clear
      data.each { |lines| animate(lines.join("\n")) } until finished?
    ensure
      cursor_on
      down(height + 1)
    end
  else
    puts "sorry, I must have a tty device to play an animation"
  end
end
running_line() click to toggle source
# File lib/vamp/animator.rb, line 80
def running_line
  @scroll_counter += 1
  "#{scroll_text[scroll_offset..(scroll_offset + width - 1)]}"
end
scroll_offset() click to toggle source
# File lib/vamp/animator.rb, line 58
def scroll_offset
  @scroll_counter / 3
end