class CommandLine::ProgressBar

Constants

VERSION

Attributes

current[R]
format[W]
format_arguments[W]
start_time[RW]
title[R]
total[R]

Public Class Methods

new(title, total, out = STDERR) click to toggle source
   # File lib/cli/progressbar.rb
15 def initialize(title, total, out = STDERR)
16   @title = title
17   @total = total
18   @out = out
19   @terminal_width = 80
20   @bar_mark = '='
21   @current = 0
22   @previous = 0
23   @finished_p = false
24   @start_time = Time.now
25   @previous_time = @start_time
26   @title_width = 24
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/cli/progressbar.rb
163 def clear
164   @out.print "\r"
165   @out.print(' ' * (CommandLine::Tools.terminal_width(80) - 1))
166   @out.print "\r"
167 end
file_transfer_mode() click to toggle source
    # File lib/cli/progressbar.rb
179 def file_transfer_mode
180   @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
181 end
finish() click to toggle source
    # File lib/cli/progressbar.rb
169 def finish
170   @current = @total
171   @finished_p = true
172   show
173 end
finished?() click to toggle source
    # File lib/cli/progressbar.rb
175 def finished?
176   @finished_p
177 end
halt() click to toggle source
    # File lib/cli/progressbar.rb
187 def halt
188   @finished_p = true
189   show
190 end
inc(step = 1) click to toggle source
    # File lib/cli/progressbar.rb
192 def inc(step = 1)
193   @current += step
194   @current = @total if @current > @total
195   show_if_needed
196   @previous = @current
197 end
inspect() click to toggle source
    # File lib/cli/progressbar.rb
208 def inspect
209   "#<ProgressBar:#{@current}/#{@total}>"
210 end
set(count) click to toggle source
    # File lib/cli/progressbar.rb
199 def set(count)
200   count = 0 if count < 0
201   count = @total if count > @total
202 
203   @current = count
204   show_if_needed
205   @previous = @current
206 end

Private Instance Methods

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

ETA stands for Estimated Time of Arrival.

    # File lib/cli/progressbar.rb
 97 def eta
 98   if @current == 0
 99     'ETA:  --:--:--'
100   else
101     elapsed = Time.now - @start_time
102     eta = elapsed * @total / @current - elapsed
103     sprintf('ETA:  %s', format_time(eta))
104   end
105 end
fmt_bar() click to toggle source
   # File lib/cli/progressbar.rb
40 def fmt_bar
41   bar_width = do_percentage * @terminal_width / 100
42   sprintf('[%s%s]',
43           @bar_mark * bar_width,
44           ' ' *  (@terminal_width - bar_width))
45 end
fmt_percentage() click to toggle source
   # File lib/cli/progressbar.rb
47 def fmt_percentage
48   do_percentage
49 end
fmt_stat() click to toggle source
   # File lib/cli/progressbar.rb
51 def fmt_stat
52   if @finished_p then elapsed else eta end
53 end
fmt_stat_for_file_transfer() click to toggle source
   # File lib/cli/progressbar.rb
55 def fmt_stat_for_file_transfer
56   if @finished_p then
57     sprintf('%s %s %s', bytes, transfer_rate, elapsed)
58   else
59     sprintf('%s %s %s', bytes, transfer_rate, eta)
60   end
61 end
fmt_title() click to toggle source
   # File lib/cli/progressbar.rb
63 def fmt_title
64   @title[0, (@title_width - 1)] + ':'
65 end
format_time(t) click to toggle source
   # File lib/cli/progressbar.rb
88 def format_time(t)
89   t = t.to_i
90   sec = t % 60
91   min  = (t / 60) % 60
92   hour = t / 3600
93   sprintf('%02d:%02d:%02d', hour, min, sec)
94 end
show() click to toggle source
    # File lib/cli/progressbar.rb
124 def show
125   arguments = @format_arguments.map do |method|
126     method = sprintf('fmt_%s', method)
127     send(method)
128   end
129   line = sprintf(@format, *arguments)
130 
131   width = CommandLine::Tools.terminal_width(80, @out)
132   if line.length == width - 1
133     @out.print(line + eol)
134     @out.flush
135   elsif line.length >= width
136     @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
137     if @terminal_width == 0 then @out.print(line + eol) else show end
138   else # line.length < width - 1
139     @terminal_width += width - line.length + 1
140     show
141   end
142   @previous_time = Time.now
143 end
show_if_needed() click to toggle source
    # File lib/cli/progressbar.rb
145 def show_if_needed
146   if @total.zero?
147     cur_percentage = 100
148     prev_percentage = 0
149   else
150     cur_percentage  = (@current  * 100 / @total).to_i
151     prev_percentage = (@previous * 100 / @total).to_i
152   end
153 
154   # Use "!=" instead of ">" to support negative changes
155   if cur_percentage != prev_percentage ||
156       Time.now - @previous_time >= 1 || @finished_p
157     show
158   end
159 end
transfer_rate() click to toggle source
   # File lib/cli/progressbar.rb
79 def transfer_rate
80   bytes_per_second = @current.to_f / (Time.now - @start_time)
81   sprintf('%s/s', convert_bytes(bytes_per_second))
82 end