Buildr C0 Coverage Information - RCov

lib/buildr/core/progressbar.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/buildr/core/progressbar.rb 161 118
38.51%
44.07%

Key

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.

Coverage Details

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 || 0
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     return @title if ProgressBar.width <= 10
70     @title.size > ProgressBar.width / 5 ? (@title[0, ProgressBar.width / 5 - 2] + '..') : @title
71   end
72 
73   def count
74     human(@count)
75   end
76 
77   def total
78     human(@total)
79   end
80 
81   def percentage
82     '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
83   end
84 
85   def time
86     @finished ? elapsed : eta
87   end
88 
89   def eta
90     return 'ETA:  --:--:--' if @count == 0
91     elapsed = Time.now - @start
92     eta = elapsed * @total / @count - elapsed
93     'ETA:  %s' % duration(eta.ceil)
94   end
95 
96   def elapsed
97     'Time: %s' % duration(Time.now - @start)
98   end
99 
100   def rate
101     '%s/s' % human(@count / (Time.now - @start))
102   end
103 
104   def progress(width)
105     width -= 2
106     marks = @total == 0 ? width : (@count * width / @total)
107     "|%-#{width}s|" % (@mark * marks)
108   end
109 
110   def human(bytes)
111     magnitude = (0..3).find { |i| bytes < (1024 << i * 10) } || 3
112     return '%dB' % bytes if magnitude == 0
113     return '%.1f%s' % [ bytes.to_f / (1 << magnitude * 10), [nil, 'KB', 'MB', 'GB'][magnitude] ]
114   end
115 
116   def duration(seconds)
117     '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
118   end
119 
120   def finish
121     unless @finished
122       @finished = true
123       render
124     end
125   end
126 
127 protected
128 
129   def clear
130     return if @output == nil || ProgressBar.width <= 0
131     @output.print "\r", " " * (ProgressBar.width - 1), "\r"
132     @output.flush
133   end
134 
135   def render
136     return unless @output
137     format, *args = @format
138     line = format % args.map { |arg| send(arg) }
139     if ProgressBar.width >= line.size
140       @output.print line.sub('|--|') { progress(ProgressBar.width - line.size + 3) }
141     else
142       @output.print line.sub('|--|', '')
143     end
144     @output.print @finished ? "\n" : "\r"
145     @output.flush
146     @previous = @count
147     @last_time = Time.now
148   end
149 
150   def changed?
151     return false unless @output && Time.now - @last_time > 0.1
152     return human(@count) != human(@previous) if @total == 0
153     return true if (@count - @previous) >= @total / 100
154     return Time.now - @last_time > 1
155   end
156 
157   def default_format
158     @total == 0 ? ['%s %8s %s', :title, :count, :elapsed] : ['%s: %s |--| %8s/%s %s', :title, :percentage, :count, :total, :time]
159   end
160 
161 end

Generated on 2011-07-06 23:35:37 -0700 with rcov 0.9.8