Class: Sprout::ProgressBarImpl

Inherits:
Object
  • Object
show all
Defined in:
sprout/lib/sprout/progress_bar.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ProgressBarImpl) initialize(title, total, out = STDERR)

A new instance of ProgressBarImpl



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'sprout/lib/sprout/progress_bar.rb', line 46

def initialize (title, total, out = STDERR)
  @title = title
  @total = total
  @out = out
  @terminal_width = 80
  @bar_mark = "."
  @current = 0
  @previous = 0
  @finished_p = false
  @start_time = Time.now
  @previous_time = @start_time
  @title_width = 18
  @format = "%-#{@title_width}s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
  clear
  show
end

Instance Attribute Details

- (Object) bar_mark

Returns the value of attribute bar_mark



66
67
68
# File 'sprout/lib/sprout/progress_bar.rb', line 66

def bar_mark
  @bar_mark
end

- (Object) current (readonly)

Returns the value of attribute current



64
65
66
# File 'sprout/lib/sprout/progress_bar.rb', line 64

def current
  @current
end

- (Object) start_time

Returns the value of attribute start_time



66
67
68
# File 'sprout/lib/sprout/progress_bar.rb', line 66

def start_time
  @start_time
end

- (Object) title (readonly)

Returns the value of attribute title



63
64
65
# File 'sprout/lib/sprout/progress_bar.rb', line 63

def title
  @title
end

- (Object) title_width

Returns the value of attribute title_width



66
67
68
# File 'sprout/lib/sprout/progress_bar.rb', line 66

def title_width
  @title_width
end

- (Object) total (readonly)

Returns the value of attribute total



65
66
67
# File 'sprout/lib/sprout/progress_bar.rb', line 65

def total
  @total
end

Instance Method Details

- (Object) bytes



114
115
116
# File 'sprout/lib/sprout/progress_bar.rb', line 114

def bytes
  convert_bytes(@current)
end

- (Object) clear



214
215
216
217
218
# File 'sprout/lib/sprout/progress_bar.rb', line 214

def clear
  @out.print "\r"
  @out.print(" " * (get_width - 1))
  @out.print "\r"
end

- (Object) convert_bytes(bytes)



97
98
99
100
101
102
103
104
105
106
107
# File 'sprout/lib/sprout/progress_bar.rb', line 97

def convert_bytes (bytes)
  if bytes < 1024
    sprintf("%6dB", bytes)
  elsif bytes < 1024 * 1000 # 1000kb
    sprintf("%5.1fKB", bytes.to_f / 1024)
  elsif bytes < 1024 * 1024 * 1000  # 1000mb
    sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
  else
    sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
  end
end

- (Object) do_percentage



146
147
148
149
150
151
152
# File 'sprout/lib/sprout/progress_bar.rb', line 146

def do_percentage
  if @total.zero?
    100
  else
    @current  * 100 / @total
  end
end

- (Object) elapsed



137
138
139
140
# File 'sprout/lib/sprout/progress_bar.rb', line 137

def elapsed
  elapsed_time = Time.now - @start_time
  sprintf("Time: %s", format_time(elapsed_time))
end

- (Object) eol



142
143
144
# File 'sprout/lib/sprout/progress_bar.rb', line 142

def eol
  if @finished_p then "\n" else "\r" end
end

- (Object) eta

ETA stands for Estimated Time of Arrival.



127
128
129
130
131
132
133
134
135
# File 'sprout/lib/sprout/progress_bar.rb', line 127

def eta
  if @current == 0
    "ETA:  --:--:--"
  else
    elapsed_time = Time.now - @start_time
    estimated_time = elapsed_time * @total / @current - elapsed_time
    sprintf("ETA:  %s", format_time(estimated_time))
  end
end

- (Object) file_transfer_mode



230
231
232
# File 'sprout/lib/sprout/progress_bar.rb', line 230

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

- (Object) finish



220
221
222
223
224
# File 'sprout/lib/sprout/progress_bar.rb', line 220

def finish
  @current = @total
  @finished_p = true
  show
end

- (Boolean) finished?

Returns:

  • (Boolean)


226
227
228
# File 'sprout/lib/sprout/progress_bar.rb', line 226

def finished?
  @finished_p
end

- (Object) fmt_bar



70
71
72
73
74
75
# File 'sprout/lib/sprout/progress_bar.rb', line 70

def fmt_bar
  bar_width = do_percentage * @terminal_width / 100
  sprintf("|%s%s|", 
          @bar_mark * bar_width, 
          " " *  (@terminal_width - bar_width))
end

- (Object) fmt_percentage



77
78
79
# File 'sprout/lib/sprout/progress_bar.rb', line 77

def fmt_percentage
  do_percentage
end

- (Object) fmt_stat



81
82
83
# File 'sprout/lib/sprout/progress_bar.rb', line 81

def fmt_stat
  if @finished_p then elapsed else eta end
end

- (Object) fmt_stat_for_file_transfer



85
86
87
88
89
90
91
# File 'sprout/lib/sprout/progress_bar.rb', line 85

def fmt_stat_for_file_transfer
  if @finished_p then 
    sprintf("%s %s %s", bytes, transfer_rate, elapsed)
  else 
    sprintf("%s %s %s", bytes, transfer_rate, eta)
  end
end

- (Object) fmt_title



93
94
95
# File 'sprout/lib/sprout/progress_bar.rb', line 93

def fmt_title
  @title[0,(@title_width - 1)] + ":"
end

- (Object) format=(format)



234
235
236
# File 'sprout/lib/sprout/progress_bar.rb', line 234

def format= (format)
  @format = format
end

- (Object) format_arguments=(arguments)



238
239
240
# File 'sprout/lib/sprout/progress_bar.rb', line 238

def format_arguments= (arguments)
  @format_arguments = arguments
end

- (Object) format_time(t)



118
119
120
121
122
123
124
# File 'sprout/lib/sprout/progress_bar.rb', line 118

def format_time (t)
  t = t.to_i
  sec = t % 60
  min  = (t / 60) % 60
  hour = t / 3600
  sprintf("%02d:%02d:%02d", hour, min, sec)
end

- (Object) get_width



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'sprout/lib/sprout/progress_bar.rb', line 154

def get_width
  #    return 80
  # FIXME: I don't know how portable it is.
  default_width = 80
  begin
    tiocgwinsz = 0x5413
    data = [0, 0, 0, 0].pack("SSSS")
    if @out.ioctl(tiocgwinsz, data) >= 0 then
      unpacked = data.unpack("SSSS")
      cols = unpacked[1]
      # Commented this because Aptana was complaining about
      # The unused variables
      # rows, cols, xpixels, ypixels = data.unpack("SSSS")
      if cols >= 0 then cols else default_width end
    else
      default_width
    end
  rescue Exception
    default_width
  end
end

- (Object) halt



242
243
244
245
# File 'sprout/lib/sprout/progress_bar.rb', line 242

def halt
  @finished_p = true
  show
end

- (Object) inc(step = 1)



247
248
249
250
251
252
# File 'sprout/lib/sprout/progress_bar.rb', line 247

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

- (Object) inspect



263
264
265
# File 'sprout/lib/sprout/progress_bar.rb', line 263

def inspect
  "#<ProgressBar:#{@current}/#{@total}>"
end

- (Object) set(count)



254
255
256
257
258
259
260
261
# File 'sprout/lib/sprout/progress_bar.rb', line 254

def set (count)
  if count < 0 || count > @total
    @total = count
  end
  @current = count
  show_if_needed
  @previous = @current
end

- (Object) show



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'sprout/lib/sprout/progress_bar.rb', line 176

def show
  arguments = @format_arguments.map do |method|
    method = sprintf("fmt_%s", method)
    send(method)
  end
  line = sprintf(@format, *arguments)

  width = get_width
  if(line.length == width - 1)
    @out.print(line + eol)
    @out.flush
  elsif(line.length >= width)
    @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
    if @terminal_width == 0 then @out.print(line + eol) else show end
  else # line.length < width - 1
    @terminal_width += width - line.length + 1
    show
  end
  @previous_time = Time.now
end

- (Object) show_if_needed



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'sprout/lib/sprout/progress_bar.rb', line 197

def show_if_needed
  if @total.zero?
    cur_percentage = 100
    prev_percentage = 0
  else
    cur_percentage  = (@current  * 100 / @total).to_i
    prev_percentage = (@previous * 100 / @total).to_i
  end

  # Use "!=" instead of ">" to support negative changes
  if cur_percentage != prev_percentage || 
      Time.now - @previous_time >= 1 || @finished_p
    show
  end
end

- (Object) transfer_rate



109
110
111
112
# File 'sprout/lib/sprout/progress_bar.rb', line 109

def transfer_rate
  bytes_per_second = @current.to_f / (Time.now - @start_time)
  sprintf("%s/s", convert_bytes(bytes_per_second))
end