Class: ConsoleProgressBar::Animator

Inherits:
Object
  • Object
show all
Includes:
Extras, PrintTools
Defined in:
lib/console_progress_bar/animator.rb

Instance Method Summary collapse

Constructor Details

#initialize(total_count = 100, with_elapsed_time = false, with_remaining_time = false, increment_size = 1) ⇒ Animator

Returns a new instance of Animator.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/console_progress_bar/animator.rb', line 5

def initialize(total_count = 100, with_elapsed_time = false, with_remaining_time = false, increment_size = 1)
  @total = total_count
  @current = 0
  @recent = -1
  @increment_size = increment_size
  @output = "-|-"
  @with_elapsed_time = with_elapsed_time
  @with_remaining_time = with_remaining_time
  @start = Time.now
  @prev_time = Time.now
  @avarage_inceament_time = 0
  @prev_extra_length = 0
  @output_length = 0
end

Instance Method Details

#animateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/console_progress_bar/animator.rb', line 20

def animate
  @current += @increment_size
  if @current >= @total
    finished
  else
    @now = (@current * 1.0 / @total * 100).to_i
    if @recent != @now
      @recent = @now
      shift_back(@output_length + @prev_extra_length)
      @prev_extra_length = 0
      case @now % 3
        when 0 then @output = "-|-"
        when 1 then @output = "-/-"
        when 2 then @output = "-\\-"
      end
      print @output
      @output_length = @output.length
    end
    prepare_extras
  end
end