Class: RR::ScanProgressPrinters::ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/scan_progress_printers/progress_bar.rb

Overview

A helper class to print a text progress bar.

Constant Summary collapse

MAX_MARKERS =

length of the progress bar (in characters)

25

Instance Method Summary collapse

Constructor Details

#initialize(max_steps, session, left_table, right_table) ⇒ ProgressBar

Creates a new progress bar.

  • max_steps: number of steps at completion

  • session: the current Session

  • left_table: name of the left database table

  • right_table: name of the right database table



30
31
32
33
34
35
# File 'lib/rubyrep/scan_progress_printers/progress_bar.rb', line 30

def initialize(max_steps, session, left_table, right_table)
  @use_ansi = session.configuration.options_for_table(left_table)[:use_ansi]
  @max_steps, @current_steps = max_steps, 0
  @steps_per_marker = @max_steps.to_f / max_markers
  @current_markers, @current_percentage = 0, 0
end

Instance Method Details

#max_markersObject

Returns the length (in characters) of the progress bar.



21
22
23
# File 'lib/rubyrep/scan_progress_printers/progress_bar.rb', line 21

def max_markers
  @max_markers ||= arg ? arg.to_i : MAX_MARKERS
end

#step(step_increment = 1) ⇒ Object

Increases progress by step_increment steps.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubyrep/scan_progress_printers/progress_bar.rb', line 38

def step(step_increment = 1)
  @current_steps+= step_increment
  new_markers = @max_steps != 0 ? (@current_steps / @steps_per_marker).to_i : max_markers

  new_percentage = @max_steps != 0 ? @current_steps * 100 / @max_steps : 100
  if @use_ansi and new_percentage != @current_percentage
    # This part uses ANSI escape sequences to show a running percentage
    # to the left of the progress bar
    print "\e[1D" * (@current_markers + 5) if @current_percentage != 0 # go left
    print "#{new_percentage}%".rjust(4) << " "
    print "\e[1C" * @current_markers if @current_markers != 0 # go back right
    $stdout.flush
    @current_percentage = new_percentage
  end

  if new_markers > @current_markers
    print '.'  * (new_markers - @current_markers)
    @current_markers = new_markers
    $stdout.flush
  end
  if @current_steps == @max_steps
    print '.' * (max_markers - @current_markers) + ' '
    $stdout.flush
  end
end