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, out = STDERR) ⇒ 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, out = STDERR)
  @title = title
  @total = total
  @out = out
  @terminal_width = 80
  @bar_mark = "o"
  @current = 0
  @previous = 0
  @finished_p = false
  @start_time = Time.now
  @previous_time = @start_time
  @title_width = 35
  @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



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

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

#file_transfer_modeObject

Sets bar to file trasfer mode



217
218
219
# File 'lib/mongify/progressbar.rb', line 217

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

#finishObject

Marks finished



205
206
207
208
209
# File 'lib/mongify/progressbar.rb', line 205

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

#finished?Boolean

Returns if the bar is finished

Returns:

  • (Boolean)


212
213
214
# File 'lib/mongify/progressbar.rb', line 212

def finished?
  @finished_p
end

#format=(format) ⇒ Object

Allows format to be re/defined



222
223
224
# File 'lib/mongify/progressbar.rb', line 222

def format= (format)
  @format = format
end

#format_arguments=(arguments) ⇒ Object

Allows to change the arguments of items that are shown



227
228
229
# File 'lib/mongify/progressbar.rb', line 227

def format_arguments= (arguments)
  @format_arguments = arguments
end

#haltObject

halts drawing of bar



232
233
234
235
# File 'lib/mongify/progressbar.rb', line 232

def halt
  @finished_p = true
  show
end

#inc(step = 1) ⇒ Integer

Incremets bar

Returns:

  • (Integer)

    current bar frame



239
240
241
242
243
244
# File 'lib/mongify/progressbar.rb', line 239

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

#inspectObject

Returns string representation of this object



258
259
260
# File 'lib/mongify/progressbar.rb', line 258

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

#set(count) ⇒ Integer

Allows you to set bar frame

Returns:

  • (Integer)

    current bar frame



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

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