class AsciiParadise::AnimatedProgressBar

Constants

BACKTRACKING

BACKTRACKING

#
Bottom
BottomLeft
BottomRight
Check
Left
LightCheck
MAX_VALUE
STEP_SIZE
STEP_TO
Top
TopLeft
#

The various constants - top left, top, top right and so forth.

These may be Unicode symbols.

#
TopRight

Attributes

progress[RW]

Public Class Methods

new( pad = ['','',''], io = $stdout ) click to toggle source
#

initialize

#
# File lib/ascii_paradise/animations/animated_progress_bar.rb, line 49
def initialize(
    pad = ['','',''],
    io  = $stdout
  )
  register_sigint
  reset
  @pad = pad # An Array. Initially empty.
  @io  = io # Stdout.
  # output_three_newlines
  run
end

Public Instance Methods

finish() click to toggle source
#

finish

#
# File lib/ascii_paradise/animations/animated_progress_bar.rb, line 99
def finish
  @io.print BACKTRACKING,
    @pad[0], TopLeft, Top * MAX_VALUE, TopRight, "\n",
    @pad[1], Left, "\e[44m", " " * MAX_VALUE, "\e[0m", Right, " ", Check, "    \n",
    @pad[2], BottomLeft, Bottom * MAX_VALUE, BottomRight, "\n"
  @io.flush
end
output_three_newlines() click to toggle source
#

output_three_newlines

#
# File lib/ascii_paradise/animations/animated_progress_bar.rb, line 71
def output_three_newlines
  puts N+N+N # Make three newlines. Unsure why.
end
reset() click to toggle source
#

reset

#
# File lib/ascii_paradise/animations/animated_progress_bar.rb, line 64
def reset
  @progress = 0
end
run() click to toggle source
#

run

#
# File lib/ascii_paradise/animations/animated_progress_bar.rb, line 110
def run
  0.step(STEP_TO, STEP_SIZE) { |i|
    update(i)
    sleep(rand * 1)
  }
  finish
end
update(progress = nil) click to toggle source
#

update

The main method that will output the bar.

#
# File lib/ascii_paradise/animations/animated_progress_bar.rb, line 80
def update(progress = nil)
  @progress = progress if progress
  prog   = @progress
  filled = (0.5 * prog).round
  free   = MAX_VALUE - filled
  # ======================================================================= #
  # Next draw the box.
  # ======================================================================= #
  @io.print BACKTRACKING,
    @pad[0], TopLeft, Top * MAX_VALUE, TopRight, "\n",
    @pad[1], Left, "\e[44m", ' ' * filled, "\e[46m", ' ' * free, 
    "\e[0m", Right, ' ', prog.round, "%\n",
    @pad[2], BottomLeft, Bottom * MAX_VALUE, BottomRight, N
  @io.flush
end