class Archive::Tar::Minitar::Command::ProgressBar

Constants

VERSION

Attributes

title[RW]
total[RW]

Public Class Methods

new(title, total, out = STDERR) click to toggle source
   # File lib/archive/tar/minitar/command.rb
35 def initialize (title, total, out = STDERR)
36   @title = title
37   @total = total
38   @out = out
39   @bar_width = 80
40   @bar_mark = "o"
41   @current = 0
42   @previous = 0
43   @is_finished = false
44   @start_time = Time.now
45   @previous_time = @start_time
46   @title_width = 14
47   @format = "%-#{@title_width}s %3d%% %s %s"
48   @format_arguments = [:title, :percentage, :bar, :stat]
49   show
50 end

Public Instance Methods

file_transfer_mode() click to toggle source
    # File lib/archive/tar/minitar/command.rb
182   def file_transfer_mode
183   @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]  
184 end
finish() click to toggle source
    # File lib/archive/tar/minitar/command.rb
194 def finish
195   @current = @total
196   @is_finished = true
197   show_progress
198 end
format=(format) click to toggle source
    # File lib/archive/tar/minitar/command.rb
186 def format= (format)
187   @format = format
188 end
format_arguments=(arguments) click to toggle source
    # File lib/archive/tar/minitar/command.rb
190 def format_arguments= (arguments)
191   @format_arguments = arguments
192 end
halt() click to toggle source
    # File lib/archive/tar/minitar/command.rb
200 def halt
201   @is_finished = true
202   show_progress
203 end
inc(step = 1) click to toggle source
    # File lib/archive/tar/minitar/command.rb
214 def inc (step = 1)
215   @current += step
216   @current = @total if @current > @total
217   show_progress
218   @previous = @current
219 end
inspect() click to toggle source
    # File lib/archive/tar/minitar/command.rb
221 def inspect
222   "(ProgressBar: #{@current}/#{@total})"
223 end
set(count) click to toggle source
    # File lib/archive/tar/minitar/command.rb
205 def set (count)
206   if count < 0 || count > @total
207     raise "invalid count: #{count} (total: #{@total})"
208   end
209   @current = count
210   show_progress
211   @previous = @current
212 end

Private Instance Methods

bar() click to toggle source
    # File lib/archive/tar/minitar/command.rb
114 def bar
115   len = percentage * @bar_width / 100
116   sprintf("|%s%s|", @bar_mark * len, " " *  (@bar_width - len))
117 end
bytes() click to toggle source
   # File lib/archive/tar/minitar/command.rb
70 def bytes
71   convert_bytes(@current)
72 end
convert_bytes(bytes) click to toggle source
   # File lib/archive/tar/minitar/command.rb
53 def convert_bytes (bytes)
54   if bytes < 1024
55     sprintf("%6dB", bytes)
56   elsif bytes < 1024 * 1000 # 1000kb
57     sprintf("%5.1fKB", bytes.to_f / 1024)
58   elsif bytes < 1024 * 1024 * 1000  # 1000mb
59     sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
60   else
61     sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
62   end
63 end
elapsed() click to toggle source
   # File lib/archive/tar/minitar/command.rb
93 def elapsed
94   elapsed = Time.now - @start_time
95   sprintf("Time: %s", format_time(elapsed))
96 end
eol() click to toggle source
    # File lib/archive/tar/minitar/command.rb
110 def eol
111   if @is_finished then "\n" else "\r" end
112 end
eta() click to toggle source

ETA stands for Estimated Time of Arrival.

   # File lib/archive/tar/minitar/command.rb
83 def eta
84   if @current == 0
85     "ETA:  --:--:--"
86   else
87   elapsed = Time.now - @start_time
88   eta = elapsed * @total / @current - elapsed;
89   sprintf("ETA:  %s", format_time(eta))
90   end
91 end
format_time(t) click to toggle source
   # File lib/archive/tar/minitar/command.rb
74 def format_time (t)
75   t = t.to_i
76   sec = t % 60
77   min  = (t / 60) % 60
78   hour = t / 3600
79   sprintf("%02d:%02d:%02d", hour, min, sec);
80 end
get_width() click to toggle source
    # File lib/archive/tar/minitar/command.rb
131 def get_width
132   # FIXME: I don't know how portable it is.
133   default_width = 80
134     #   begin
135     #     tiocgwinsz = 0x5413
136     #     data = [0, 0, 0, 0].pack("SSSS")
137     #     if @out.ioctl(tiocgwinsz, data) >= 0 then
138     #       rows, cols, xpixels, ypixels = data.unpack("SSSS")
139     #       if cols >= 0 then cols else default_width end
140     #     else
141     #       default_width
142     #     end
143     #   rescue Exception
144     #     default_width
145     #   end
146 end
percentage(value = nil) click to toggle source
    # File lib/archive/tar/minitar/command.rb
119 def percentage(value = nil)
120   if @total.zero?
121     100
122   else
123     (value || @current) * 100 / @total
124   end
125 end
show() click to toggle source
    # File lib/archive/tar/minitar/command.rb
148 def show
149   arguments = @format_arguments.map {|method| send(method) }
150   line = sprintf(@format, *arguments)
151 
152   width = get_width
153   if line.length == width - 1 
154     @out.print(line + eol)
155   elsif line.length >= width
156     @bar_width = [@bar_width - (line.length - width + 1), 0].max
157     if @bar_width == 0 then @out.print(line + eol) else show end
158   else # line.length < width - 1
159     @bar_width += width - line.length + 1
160     show
161   end
162   @previous_time = Time.now
163 end
show_progress() click to toggle source
    # File lib/archive/tar/minitar/command.rb
165 def show_progress
166   if @total.zero?
167     cur_percentage = 100
168     prev_percentage = 0
169   else
170     cur_percentage  = (@current  * 100 / @total).to_i
171     prev_percentage = (@previous * 100 / @total).to_i
172   end
173 
174   if cur_percentage > prev_percentage || 
175     Time.now - @previous_time >= 1 ||
176     @is_finished
177     show
178   end
179 end
stat() click to toggle source
    # File lib/archive/tar/minitar/command.rb
 98 def stat
 99   if @is_finished then elapsed else eta end
100 end
stat_for_file_transfer() click to toggle source
    # File lib/archive/tar/minitar/command.rb
102 def stat_for_file_transfer
103   if @is_finished then 
104     sprintf("%s %s %s", bytes, transfer_rate, elapsed)
105   else 
106     sprintf("%s %s %s", bytes, transfer_rate, eta)
107   end
108 end
transfer_rate() click to toggle source
   # File lib/archive/tar/minitar/command.rb
65 def transfer_rate
66   bytes_per_second = @current.to_f / (Time.now - @start_time)
67   sprintf("%s/s", convert_bytes(bytes_per_second))
68 end