Class: Mongify::ProgressBar

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

Overview

Progress bar used to display results

Direct Known Subclasses

ReversedProgressBar

Constant Summary collapse

VERSION =

Progress bar version

"0.9.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, total) ⇒ ProgressBar

Returns a new instance of ProgressBar.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongify/progressbar.rb', line 22

def initialize (title, total)
  @title = title
  @total = total
  @out = Mongify::Configuration.out_stream
  @terminal_width = 80
  @bar_mark = "o"
  @current = 0
  @previous = 0
  @finished_p = false
  @start_time = Time.now
  @previous_time = @start_time
  @title_width = 37
  @format = "%-#{@title_width}s %s %3d%% %s %s"
  @format_arguments = [:title, :count, :percentage, :bar, :stat]
  clear
  show
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



40
41
42
# File 'lib/mongify/progressbar.rb', line 40

def current
  @current
end

#start_timeObject

Returns the value of attribute start_time.



42
43
44
# File 'lib/mongify/progressbar.rb', line 42

def start_time
  @start_time
end

#titleObject (readonly)

Returns the value of attribute title.



39
40
41
# File 'lib/mongify/progressbar.rb', line 39

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



41
42
43
# File 'lib/mongify/progressbar.rb', line 41

def total
  @total
end

Instance Method Details

#clearObject

Clear’s line



199
200
201
202
203
204
# File 'lib/mongify/progressbar.rb', line 199

def clear
  return unless @out
  @out.print "\r"
  @out.print(" " * (get_width - 1))
  @out.print "\r"
end

#file_transfer_modeObject

Sets bar to file trasfer mode



219
220
221
# File 'lib/mongify/progressbar.rb', line 219

def file_transfer_mode
  @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
end

#finishObject

Marks finished



207
208
209
210
211
# File 'lib/mongify/progressbar.rb', line 207

def finish
  @current = @total
  @finished_p = true
  show
end

#finished?Boolean

Returns if the bar is finished

Returns:

  • (Boolean)


214
215
216
# File 'lib/mongify/progressbar.rb', line 214

def finished?
  @finished_p
end

#format=(format) ⇒ Object

Allows format to be re/defined



224
225
226
# File 'lib/mongify/progressbar.rb', line 224

def format= (format)
  @format = format
end

#format_arguments=(arguments) ⇒ Object

Allows to change the arguments of items that are shown



229
230
231
# File 'lib/mongify/progressbar.rb', line 229

def format_arguments= (arguments)
  @format_arguments = arguments
end

#haltObject

halts drawing of bar



234
235
236
237
# File 'lib/mongify/progressbar.rb', line 234

def halt
  @finished_p = true
  show
end

#inc(step = 1) ⇒ Integer

Incremets bar

Returns:

  • (Integer)

    current bar frame



241
242
243
244
245
246
# File 'lib/mongify/progressbar.rb', line 241

def inc (step = 1)
  @current += step
  @current = @total if @current > @total
  show_if_needed
  @previous = @current
end

#inspectObject

Returns string representation of this object



260
261
262
# File 'lib/mongify/progressbar.rb', line 260

def inspect
  "#<ProgressBar:#{@current}/#{@total}>"
end

#set(count) ⇒ Integer

Allows you to set bar frame

Returns:

  • (Integer)

    current bar frame



250
251
252
253
254
255
256
257
# File 'lib/mongify/progressbar.rb', line 250

def set (count)
  if count < 0 || count > @total
    raise "invalid count: #{count} (total: #{@total})"
  end
  @current = count
  show_if_needed
  @previous = @current
end