Class: TT::Progressbar

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/progressbar.rb

Overview

Wrapper for displaying progress to the user.

Since:

  • 2.5.0

Instance Method Summary collapse

Constructor Details

#initialize(range = nil, task_name = 'Processing', decimals_places = 1) ⇒ Progressbar

Returns a new instance of Progressbar

Parameters:

  • range (Mixed) (defaults to: nil)

    Object that gives the amount of work to be done.

  • task_name (String) (defaults to: 'Processing')

    String that will be displayed in the statusbar.

  • decimals_places (Integer) (defaults to: 1)

    Number of decimals for the percentage in the statusbar.

Since:

  • 2.5.0



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
63
64
# File 'TT_Lib2/progressbar.rb', line 38

def initialize( range=nil, task_name='Processing', decimals_places=1 )
  if range.nil?
    @start = 0
    @end = 0
  elsif range.is_a?( Range )
    @start = range.first
    @end = range.last
  elsif range.is_a?( Numeric )
    @start = 0
    @end = range
  else
    [:length, :count, :size, :count_instances].each { |method|
      if range.respond_to?( method )
        @start = 0
        @end = range.send( method )
      end
    }
    if @start.nil?
      raise( ArgumentError, 'Must be Range, Numeric or collection.' )
    end
  end
  @start = @start.to_f
  @end = @end.to_f
  @task_name = task_name
  @decimals = decimals_places.to_i
  reset()
end

Instance Method Details

#elapsed_time(format = false) ⇒ Numeric|String

Returns:

  • (Numeric|String)

Since:

  • 2.5.0



118
119
120
121
# File 'TT_Lib2/progressbar.rb', line 118

def elapsed_time(format=false)
  elapsed = Time.now - @start_time
  (format) ? TT::format_time(elapsed) : elapsed
end

#estimated_time_left(format = false) ⇒ Numeric|String

Returns:

  • (Numeric|String)

Since:

  • 2.5.0



130
131
132
133
134
135
# File 'TT_Lib2/progressbar.rb', line 130

def estimated_time_left(format=false)
  elapsed = elapsed_time()
  ratio = size() / index()
  remaining = (elapsed * ratio) - elapsed
  (format) ? TT::format_time(remaining) : remaining
end

#increment(amount = 1) ⇒ Numeric Also known as: next

Parameters:

  • amount (Numeric) (defaults to: 1)

Returns:

  • (Numeric)

Since:

  • 2.5.0



73
74
75
76
77
# File 'TT_Lib2/progressbar.rb', line 73

def increment(amount=1)
  @value += amount.abs
  updateUI()
  @value
end

#indexNumeric

Returns:

  • (Numeric)

Since:

  • 2.5.0



107
108
109
# File 'TT_Lib2/progressbar.rb', line 107

def index
  @value - @start
end

#sizeNumeric

Returns:

  • (Numeric)

Since:

  • 2.5.0



98
99
100
# File 'TT_Lib2/progressbar.rb', line 98

def size
  @end - @start
end

#value=(new_value) ⇒ Numeric

Parameters:

  • new_value (Numeric)

Returns:

  • (Numeric)

Since:

  • 2.5.0



87
88
89
90
91
# File 'TT_Lib2/progressbar.rb', line 87

def value=(new_value)
  @value = new_value
  updateUI()
  @value
end