C0 code coverage information
Generated on Wed Oct 07 08:33:57 -0700 2009 with rcov 0.8.2.1
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership. The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
14 # the License.
15
16
17 class ProgressBar
18
19 class << self
20
21 def start(args, &block)
22 new(args).start &block
23 end
24
25 def width
26 @width ||= $terminal.output_cols || 80
27 end
28
29 end
30
31 def initialize(args = {})
32 @title = args[:title] || ''
33 @total = args[:total] || 0
34 @mark = args[:mark] || '.'
35 @format = args[:format] || default_format
36 @output = args[:output] || $stderr unless args[:hidden] || !$stdout.isatty
37 clear
38 end
39
40 def start
41 @start = @last_time = Time.now
42 @count = 0
43 @finished = false
44 render
45 if block_given?
46 result = yield(self) if block_given?
47 finish
48 result
49 else
50 self
51 end
52 end
53
54 def inc(count)
55 set @count + count
56 end
57
58 def <<(bytes)
59 inc bytes.size
60 end
61
62 def set(count)
63 @count = [count, 0].max
64 @count = [count, @total].min unless @total == 0
65 render if changed?
66 end
67
68 def title
69 @title.size > ProgressBar.width / 5 ? (@title[0, ProgressBar.width / 5 - 2] + '..') : @title
70 end
71
72 def count
73 human(@count)
74 end
75
76 def total
77 human(@total)
78 end
79
80 def percentage
81 '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
82 end
83
84 def time
85 @finished ? elapsed : eta
86 end
87
88 def eta
89 return 'ETA: --:--:--' if @count == 0
90 elapsed = Time.now - @start
91 eta = elapsed * @total / @count - elapsed
92 'ETA: %s' % duration(eta.ceil)
93 end
94
95 def elapsed
96 'Time: %s' % duration(Time.now - @start)
97 end
98
99 def rate
100 '%s/s' % human(@count / (Time.now - @start))
101 end
102
103 def progress(width)
104 width -= 2
105 marks = @total == 0 ? width : (@count * width / @total)
106 "|%-#{width}s|" % (@mark * marks)
107 end
108
109 def human(bytes)
110 magnitude = (0..3).find { |i| bytes < (1024 << i * 10) } || 3
111 return '%dB' % bytes if magnitude == 0
112 return '%.1f%s' % [ bytes.to_f / (1 << magnitude * 10), [nil, 'KB', 'MB', 'GB'][magnitude] ]
113 end
114
115 def duration(seconds)
116 '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
117 end
118
119 def finish
120 unless @finished
121 @finished = true
122 render
123 end
124 end
125
126 protected
127
128 def clear
129 return unless @output
130 @output.print "\r", " " * (ProgressBar.width - 1), "\r"
131 @output.flush
132 end
133
134 def render
135 return unless @output
136 format, *args = @format
137 line = format % args.map { |arg| send(arg) }
138 @output.print line.sub('|--|') { progress(ProgressBar.width - line.size + 3) }
139 @output.print @finished ? "\n" : "\r"
140 @output.flush
141 @previous = @count
142 @last_time = Time.now
143 end
144
145 def changed?
146 return false unless @output && Time.now - @last_time > 0.1
147 return human(@count) != human(@previous) if @total == 0
148 return true if (@count - @previous) >= @total / 100
149 return Time.now - @last_time > 1
150 end
151
152 def default_format
153 @total == 0 ? ['%s %8s %s', :title, :count, :elapsed] : ['%s: %s |--| %8s/%s %s', :title, :percentage, :count, :total, :time]
154 end
155
156 end
Generated using the rcov code coverage analysis tool for Ruby
version 0.8.2.1.