Parent

Class Index [+]

Quicksearch

ProgressBar

Ruby/ProgressBar - a text progress bar library

Copyright (C) 2001-2005 Satoru Takabayashi

    All rights reserved.
    This is free software with ABSOLUTELY NO WARRANTY.

You can redistribute it and/or modify it under the terms of Ruby’s license.

Constants

VERSION

Attributes

title[R]
current[R]
total[R]
start_time[RW]
bar_mark[W]

Public Class Methods

new(title, total, out = STDERR) click to toggle source
    # File lib/progressbar.rb, line 15
15:   def initialize (title, total, out = STDERR)
16:     @title = title
17:     @total = total
18:     @out = out
19:     @terminal_width = 80
20:     @bar_mark = "o"
21:     @current = 0
22:     @previous = 0
23:     @finished_p = false
24:     @start_time = Time.now
25:     @previous_time = @start_time
26:     @title_width = 14
27:     @format = "%-#{@title_width}s %3d%% %s %s"
28:     @format_arguments = [:title, :percentage, :bar, :stat]
29:     clear
30:     show
31:   end

Public Instance Methods

clear() click to toggle source
     # File lib/progressbar.rb, line 178
178:   def clear
179:     @out.print "\r"
180:     @out.print(" " * (get_width - 1))
181:     @out.print "\r"
182:   end
file_transfer_mode() click to toggle source
     # File lib/progressbar.rb, line 194
194:   def file_transfer_mode
195:     @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
196:   end
finish() click to toggle source
     # File lib/progressbar.rb, line 184
184:   def finish
185:     @current = @total
186:     @finished_p = true
187:     show
188:   end
finished?() click to toggle source
     # File lib/progressbar.rb, line 190
190:   def finished?
191:     @finished_p
192:   end
format=(format) click to toggle source
     # File lib/progressbar.rb, line 198
198:   def format= (format)
199:     @format = format
200:   end
format_arguments=(arguments) click to toggle source
     # File lib/progressbar.rb, line 202
202:   def format_arguments= (arguments)
203:     @format_arguments = arguments
204:   end
halt() click to toggle source
     # File lib/progressbar.rb, line 206
206:   def halt
207:     @finished_p = true
208:     show
209:   end
inc(step = 1) click to toggle source
     # File lib/progressbar.rb, line 211
211:   def inc (step = 1)
212:     @current += step
213:     @current = @total if @current > @total
214:     show_if_needed
215:     @previous = @current
216:   end
inspect() click to toggle source
     # File lib/progressbar.rb, line 227
227:   def inspect
228:     "#<ProgressBar:#{@current}/#{@total}>"
229:   end
set(count) click to toggle source
     # File lib/progressbar.rb, line 218
218:   def set (count)
219:     if count < 0 || count > @total
220:       raise "invalid count: #{count} (total: #{@total})"
221:     end
222:     @current = count
223:     show_if_needed
224:     @previous = @current
225:   end

Private Instance Methods

bytes() click to toggle source
    # File lib/progressbar.rb, line 83
83:   def bytes
84:     convert_bytes(@current)
85:   end
convert_bytes(bytes) click to toggle source
    # File lib/progressbar.rb, line 66
66:   def convert_bytes (bytes)
67:     if bytes < 1024
68:       sprintf("%6dB", bytes)
69:     elsif bytes < 1024 * 1000 # 1000kb
70:       sprintf("%5.1fKB", bytes.to_f / 1024)
71:     elsif bytes < 1024 * 1024 * 1000  # 1000mb
72:       sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
73:     else
74:       sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
75:     end
76:   end
do_percentage() click to toggle source
     # File lib/progressbar.rb, line 115
115:   def do_percentage
116:     if @total.zero?
117:       100
118:     else
119:       @current  * 100 / @total
120:     end
121:   end
elapsed() click to toggle source
     # File lib/progressbar.rb, line 106
106:   def elapsed
107:     elapsed = Time.now - @start_time
108:     sprintf("Time: %s", format_time(elapsed))
109:   end
eol() click to toggle source
     # File lib/progressbar.rb, line 111
111:   def eol
112:     if @finished_p then "\n" else "\r" end
113:   end
eta() click to toggle source

ETA stands for Estimated Time of Arrival.

     # File lib/progressbar.rb, line 96
 96:   def eta
 97:     if @current == 0
 98:       "ETA:  --:--:--"
 99:     else
100:       elapsed = Time.now - @start_time
101:       eta = elapsed * @total / @current - elapsed;
102:       sprintf("ETA:  %s", format_time(eta))
103:     end
104:   end
fmt_bar() click to toggle source
    # File lib/progressbar.rb, line 39
39:   def fmt_bar
40:     bar_width = do_percentage * @terminal_width / 100
41:     sprintf("|%s%s|",
42:             @bar_mark * bar_width,
43:             " " *  (@terminal_width - bar_width))
44:   end
fmt_percentage() click to toggle source
    # File lib/progressbar.rb, line 46
46:   def fmt_percentage
47:     do_percentage
48:   end
fmt_stat() click to toggle source
    # File lib/progressbar.rb, line 50
50:   def fmt_stat
51:     if @finished_p then elapsed else eta end
52:   end
fmt_stat_for_file_transfer() click to toggle source
    # File lib/progressbar.rb, line 54
54:   def fmt_stat_for_file_transfer
55:     if @finished_p then
56:       sprintf("%s %s %s", bytes, transfer_rate, elapsed)
57:     else
58:       sprintf("%s %s %s", bytes, transfer_rate, eta)
59:     end
60:   end
fmt_title() click to toggle source
    # File lib/progressbar.rb, line 62
62:   def fmt_title
63:     @title[0,(@title_width - 1)] + ":"
64:   end
format_time(t) click to toggle source
    # File lib/progressbar.rb, line 87
87:   def format_time (t)
88:     t = t.to_i
89:     sec = t % 60
90:     min  = (t / 60) % 60
91:     hour = t / 3600
92:     sprintf("%02d:%02d:%02d", hour, min, sec);
93:   end
get_width() click to toggle source
     # File lib/progressbar.rb, line 123
123:   def get_width
124:     # FIXME: I don't know how portable it is.
125:     default_width = 80
126:     begin
127:       tiocgwinsz = 0x5413
128:       data = [0, 0, 0, 0].pack("SSSS")
129:       if @out.ioctl(tiocgwinsz, data) >= 0 then
130:         rows, cols, xpixels, ypixels = data.unpack("SSSS")
131:         if cols >= 0 then cols else default_width end
132:       else
133:         default_width
134:       end
135:     rescue Exception
136:       default_width
137:     end
138:   end
show() click to toggle source
     # File lib/progressbar.rb, line 140
140:   def show
141:     arguments = @format_arguments.map {|method|
142:       method = sprintf("fmt_%s", method)
143:       send(method)
144:     }
145:     line = sprintf(@format, *arguments)
146: 
147:     width = get_width
148:     if line.length == width - 1
149:       @out.print(line + eol)
150:       @out.flush
151:     elsif line.length >= width
152:       @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
153:       if @terminal_width == 0 then @out.print(line + eol) else show end
154:     else # line.length < width - 1
155:       @terminal_width += width - line.length + 1
156:       show
157:     end
158:     @previous_time = Time.now
159:   end
show_if_needed() click to toggle source
     # File lib/progressbar.rb, line 161
161:   def show_if_needed
162:     if @total.zero?
163:       cur_percentage = 100
164:       prev_percentage = 0
165:     else
166:       cur_percentage  = (@current  * 100 / @total).to_i
167:       prev_percentage = (@previous * 100 / @total).to_i
168:     end
169: 
170:     # Use "!=" instead of ">" to support negative changes
171:     if cur_percentage != prev_percentage ||
172:         Time.now - @previous_time >= 1 || @finished_p
173:       show
174:     end
175:   end
transfer_rate() click to toggle source
    # File lib/progressbar.rb, line 78
78:   def transfer_rate
79:     bytes_per_second = @current.to_f / (Time.now - @start_time)
80:     sprintf("%s/s", convert_bytes(bytes_per_second))
81:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.