Class: Clio::Progressbar

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

Overview

Progressbar

Progressbar is a console-based progress bar library.

Usage

pbar = ProgressBar.new( "Demo", 100 )
100.times { pbar.inc }
pbar.finish

Copying

Based-on the original ProgressBar library by Satoru Takabayashi

Copyright © 2001 Satoru Takabayashi

Instance Method Summary collapse

Constructor Details

#initialize(title, total, out = STDERR) ⇒ Progressbar

Returns a new instance of Progressbar.



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

def initialize(title, total, out = STDERR)
  @title = title
  @total = total
  @out = out
  @bar_length = 80
  @bar_mark = "o"
  @total_overflow = true
  @current = 0
  @previous = 0
  @is_finished = false
  @start_time = Time.now
  @format = "%-14s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
  show_progress
end

Instance Method Details

#bar_mark=(mark) ⇒ Object



172
173
174
# File 'lib/clio/progressbar.rb', line 172

def bar_mark=(mark)
  @bar_mark = String(mark)[0..0]
end

#file_transfer_modeObject



164
165
166
# File 'lib/clio/progressbar.rb', line 164

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

#finishObject



188
189
190
191
192
# File 'lib/clio/progressbar.rb', line 188

def finish
  @current = @total
  @is_finished = true
  show_progress
end

#flushObject



194
195
196
# File 'lib/clio/progressbar.rb', line 194

def flush
  @out.flush
end

#format=(format) ⇒ Object



180
181
182
# File 'lib/clio/progressbar.rb', line 180

def format=(format)
  @format = format
end

#format_arguments=(arguments) ⇒ Object



184
185
186
# File 'lib/clio/progressbar.rb', line 184

def format_arguments=(arguments)
  @format_arguments = arguments
end

#haltObject



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

def halt
  @is_finished = true
  show_progress
end

#inc(step = 1) ⇒ Object



218
219
220
221
222
223
# File 'lib/clio/progressbar.rb', line 218

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

#inspectObject



225
226
227
# File 'lib/clio/progressbar.rb', line 225

def inspect
  "(ProgressBar: #{@current}/#{@total})"
end

#set(count) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/clio/progressbar.rb', line 203

def set(count)
  if count < 0
    raise "invalid count less than zero: #{count}"
  elsif count > @total
    if @total_overflow
      @total = count + 1
    else
      raise "invalid count greater than total: #{count}"
    end
  end
  @current = count
  show_progress
  @previous = @current
end

#title=(str) ⇒ Object



168
169
170
# File 'lib/clio/progressbar.rb', line 168

def title=(str)
  @title = str
end

#total_overflow=(boolv) ⇒ Object



176
177
178
# File 'lib/clio/progressbar.rb', line 176

def total_overflow=(boolv)
  @total_overflow = boolv ? true : false
end