Class: ConsoleProgressBar::Counter

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Counter.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/console_progress_bar/counter.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 = "0%"
  @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

#increase(by = nil) ⇒ Object



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

def increase(by = nil)
  @current += (by || @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
      @output = @now.to_s + "%"
      print @output
      @output_length = @output.length
    end
    prepare_extras
  end
end