Class ProgressBar
In: lib/more/facets/progressbar.rb
Parent: Object

ProgressBar

ProgressBar is a text-based progressbar library.

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

Methods

Public Class methods

[Source]

    # File lib/more/facets/progressbar.rb, line 38
38:   def initialize(title, total, out = STDERR)
39:     @title = title
40:     @total = total
41:     @out = out
42:     @bar_length = 80
43:     @bar_mark = "o"
44:     @total_overflow = true
45:     @current = 0
46:     @previous = 0
47:     @is_finished = false
48:     @start_time = Time.now
49:     @format = "%-14s %3d%% %s %s"
50:     @format_arguments = [:title, :percentage, :bar, :stat]
51:     show_progress
52:   end

Public Instance methods

[Source]

     # File lib/more/facets/progressbar.rb, line 189
189:   def bar_mark=(mark)
190:     @bar_mark = String(mark)[0..0]
191:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 181
181:   def file_transfer_mode
182:     @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
183:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 205
205:   def finish
206:     @current = @total
207:     @is_finished = true
208:     show_progress
209:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 211
211:   def flush
212:     @out.flush
213:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 197
197:   def format=(format)
198:     @format = format
199:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 201
201:   def format_arguments=(arguments)
202:     @format_arguments = arguments
203:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 215
215:   def halt
216:     @is_finished = true
217:     show_progress
218:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 235
235:   def inc(step = 1)
236:     @current += step
237:     @current = @total if @current > @total
238:     show_progress
239:     @previous = @current
240:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 242
242:   def inspect
243:     "(ProgressBar: #{@current}/#{@total})"
244:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 220
220:   def set(count)
221:     if count < 0
222:       raise "invalid count less than zero: #{count}"
223:     elsif count > @total
224:       if @total_overflow
225:         @total = count + 1
226:       else
227:         raise "invalid count greater than total: #{count}"
228:       end
229:     end
230:     @current = count
231:     show_progress
232:     @previous = @current
233:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 185
185:   def title=(str)
186:     @title = str
187:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 193
193:   def total_overflow=(boolv)
194:     @total_overflow = boolv ? true : false
195:   end

Private Instance methods

[Source]

     # File lib/more/facets/progressbar.rb, line 116
116:   def bar
117:     len = percentage * @bar_length / 100
118:     sprintf("|%s%s|", @bar_mark * len, " " *  (@bar_length - len))
119:   end

[Source]

    # File lib/more/facets/progressbar.rb, line 72
72:   def bytes
73:     convert_bytes(@current)
74:   end

[Source]

    # File lib/more/facets/progressbar.rb, line 55
55:   def convert_bytes (bytes)
56:     if bytes < 1024
57:       sprintf("%6dB", bytes)
58:     elsif bytes < 1024 * 1000 # 1000kb
59:       sprintf("%5.1fKB", bytes.to_f / 1024)
60:     elsif bytes < 1024 * 1024 * 1000  # 1000mb
61:       sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
62:     else
63:       sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
64:     end
65:   end

[Source]

    # File lib/more/facets/progressbar.rb, line 95
95:   def elapsed
96:     elapsed = Time.now - @start_time
97:     sprintf("Time: %s", format_time(elapsed))
98:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 112
112:   def eol
113:     if @is_finished then "\n" else "\r" end
114:   end

ETA stands for Estimated Time of Arrival.

[Source]

    # File lib/more/facets/progressbar.rb, line 85
85:   def eta
86:     if @current == 0
87:       "ETA:  --:--:--"
88:     else
89:       elapsed = Time.now - @start_time
90:       eta = elapsed * @total / @current - elapsed;
91:       sprintf("ETA:  %s", format_time(eta))
92:     end
93:   end

[Source]

    # File lib/more/facets/progressbar.rb, line 76
76:   def format_time(t)
77:     t = t.to_i
78:     sec = t % 60
79:     min  = (t / 60) % 60
80:     hour = t / 3600
81:     sprintf("%02d:%02d:%02d", hour, min, sec);
82:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 133
133:   def get_width
134:     # FIXME: I don't know how portable it is.
135:     default_width = 80
136:     begin
137:       tiocgwinsz = 0x5413
138:       data = [0, 0, 0, 0].pack("SSSS")
139:       if @out.ioctl(tiocgwinsz, data) >= 0 then
140:         rows, cols, xpixels, ypixels = data.unpack("SSSS")
141:         if cols >= 0 then cols else default_width end
142:       else
143:         default_width
144:       end
145:     rescue Exception
146:       default_width
147:     end
148:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 121
121:   def percentage
122:     if @total.zero?
123:       100
124:     else
125:       @current  * 100 / @total
126:     end
127:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 150
150:   def show
151:     arguments = @format_arguments.map{|method| send(method)}
152:     line = sprintf(@format, *arguments)
153: 
154:     width = get_width
155:     if line.length == width - 1
156:       @out.print(line + eol)
157:     elsif line.length >= width
158:       @bar_length = [@bar_length - (line.length - width + 1), 0].max
159:       if @bar_length == 0 then @out.print(line + eol) else show end
160:     else #line.length < width - 1
161:       @bar_length += width - line.length + 1
162:       show
163:     end
164:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 166
166:   def show_progress
167:     if @total.zero?
168:       cur_percentage = 100
169:       prev_percentage = 0
170:     else
171:       cur_percentage  = (@current  * 100 / @total).to_i
172:       prev_percentage = (@previous * 100 / @total).to_i
173:     end
174: 
175:     if cur_percentage > prev_percentage || @is_finished
176:       show
177:     end
178:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 100
100:   def stat
101:     if @is_finished then elapsed else eta end
102:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 104
104:   def stat_for_file_transfer
105:     if @is_finished then 
106:       sprintf("%s %s %s", bytes, transfer_rate, elapsed)
107:     else 
108:       sprintf("%s %s %s", bytes, transfer_rate, eta)
109:     end
110:   end

[Source]

     # File lib/more/facets/progressbar.rb, line 129
129:   def title
130:     @title[0,13] + ":"
131:   end

[Source]

    # File lib/more/facets/progressbar.rb, line 67
67:   def transfer_rate
68:     bytes_per_second = @current.to_f / (Time.now - @start_time)
69:     sprintf("%s/s", convert_bytes(bytes_per_second))
70:   end

[Validate]