class Asciimation::DropAnimator

Attributes

io[R]
options[R]

Public Class Methods

new(io: , options: ) click to toggle source
# File lib/asciimation/drop_animator.rb, line 3
def initialize(io: , options: )
  @io      = io
  @options = options
end

Public Instance Methods

animate(screen) click to toggle source
# File lib/asciimation/drop_animator.rb, line 11
def animate(screen)
  lines.each_with_index.reverse_each do |line, i|
    (i + 1 + top_lines(screen)).times do |y|
      draw_line(screen, " " * line.size, y - 1) unless y.zero?
      draw_line(screen, line,            y)
      redraw(screen)
      wait(screen)
    end
  end
end

Private Instance Methods

draw_line(screen, line, y) click to toggle source
# File lib/asciimation/drop_animator.rb, line 42
def draw_line(screen, line, y)
  screen.move_cursor(x: 0, y: y)
  screen.draw_string(left_padding(screen) + line)
end
left_padding(screen) click to toggle source
# File lib/asciimation/drop_animator.rb, line 28
def left_padding(screen)
  @left_padding ||= " " * ((screen.columns - lines.map(&:size).max) / 2)
end
lines() click to toggle source
# File lib/asciimation/drop_animator.rb, line 24
def lines
  @lines ||= io.map { |line| line.rstrip }
end
pause(screen) click to toggle source
# File lib/asciimation/drop_animator.rb, line 36
def pause(screen)
  @pause ||=
    options[:duration] /
    (1..lines.size).inject(0) { |sum, i| sum + i + top_lines(screen) + 1 }
end
redraw(screen) click to toggle source
# File lib/asciimation/drop_animator.rb, line 47
def redraw(screen)
  screen.refresh_in_memory
  Rurses.update_screen
end
top_lines(screen) click to toggle source
# File lib/asciimation/drop_animator.rb, line 32
def top_lines(screen)
  @top_lines ||= (screen.lines - lines.size) / 2
end
wait(screen) click to toggle source
# File lib/asciimation/drop_animator.rb, line 52
def wait(screen)
  sleep pause(screen)
end