Class: Tapsoob::Progress::Bar

Inherits:
Object
  • Object
show all
Defined in:
lib/tapsoob/progress/bar.rb

Direct Known Subclasses

ReversedBar, ThreadSafeBar

Constant Summary collapse

VERSION =
"0.9"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, total, out = STDOUT, title_width = nil) ⇒ Bar

Returns a new instance of Bar.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tapsoob/progress/bar.rb', line 18

def initialize (title, total, out = STDOUT, title_width = nil)
  @title = title
  @total = total
  @out = out
  @terminal_width = 80
  @bar_mark = "="
  @current = 0
  @previous = 0
  @finished_p = false
  @start_time = ::Time.now
  @previous_time = @start_time
  # Set title width: use provided width, or accommodate the title, with a minimum of 14
  @title_width = title_width || [title.length, 14].max
  @format = "%-#{@title_width}s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
  clear
  show
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



37
38
39
# File 'lib/tapsoob/progress/bar.rb', line 37

def current
  @current
end

#start_timeObject

Returns the value of attribute start_time.



39
40
41
# File 'lib/tapsoob/progress/bar.rb', line 39

def start_time
  @start_time
end

#titleObject (readonly)

Returns the value of attribute title.



36
37
38
# File 'lib/tapsoob/progress/bar.rb', line 36

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



38
39
40
# File 'lib/tapsoob/progress/bar.rb', line 38

def total
  @total
end

Instance Method Details

#clearObject



181
182
183
184
185
# File 'lib/tapsoob/progress/bar.rb', line 181

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

#file_transfer_modeObject



197
198
199
# File 'lib/tapsoob/progress/bar.rb', line 197

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

#finishObject



187
188
189
190
191
# File 'lib/tapsoob/progress/bar.rb', line 187

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

#finished?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/tapsoob/progress/bar.rb', line 193

def finished?
  @finished_p
end

#format=(format) ⇒ Object



201
202
203
# File 'lib/tapsoob/progress/bar.rb', line 201

def format= (format)
  @format = format
end

#format_arguments=(arguments) ⇒ Object



205
206
207
# File 'lib/tapsoob/progress/bar.rb', line 205

def format_arguments= (arguments)
  @format_arguments = arguments
end

#haltObject



209
210
211
212
# File 'lib/tapsoob/progress/bar.rb', line 209

def halt
  @finished_p = true
  show
end

#inc(step = 1) ⇒ Object



214
215
216
217
218
219
# File 'lib/tapsoob/progress/bar.rb', line 214

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

#inspectObject



230
231
232
# File 'lib/tapsoob/progress/bar.rb', line 230

def inspect
  "#<Tapsoob::Progress::Bar:#{@current}/#{@total}>"
end

#set(count) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/tapsoob/progress/bar.rb', line 221

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