Module: Experiment::Notify::ProgressBar
- Included in:
- Experiment::Notify
- Defined in:
- lib/experiment/notify.rb
Overview
a big part of this module is copied/inspired by Satoru Takabayashi’s <[email protected]> ProgressBar class at 0xcc.net/ruby-progressbar/index.html.en
Instance Method Summary collapse
- #elapsed ⇒ Object
- #eta ⇒ Object
- #format_time(t) ⇒ Object
- #get_width ⇒ Object
-
#inc(step = 1) ⇒ Object
:nodoc.
- #show ⇒ Object
- #show_if_needed ⇒ Object
- #stat ⇒ Object
Instance Method Details
#elapsed ⇒ Object
150 151 152 153 |
# File 'lib/experiment/notify.rb', line 150 def elapsed elapsed = Time.now - @start_time sprintf("Time: %s", format_time(elapsed)) end |
#eta ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/experiment/notify.rb', line 140 def eta if @current == 0 "ETA: --:--:--" else elapsed = Time.now - @start_time eta = elapsed * @total / @current - elapsed; sprintf("ETA: %s", format_time(eta)) end end |
#format_time(t) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/experiment/notify.rb', line 155 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 |
#get_width ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/experiment/notify.rb', line 164 def get_width # 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 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 |
#inc(step = 1) ⇒ Object
:nodoc
90 91 92 93 94 95 |
# File 'lib/experiment/notify.rb', line 90 def inc(step = 1) @current += step @current = @total if @current > @total show_if_needed @previous = @current end |
#show ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/experiment/notify.rb', line 116 def show percent = @current * 100.0 / @total = percent * @terminal_width / 100.0 line = sprintf "%3d%% |%s%s| %s", percent, "=" * .floor, "-" * (@terminal_width - .ceil), stat #width = get_width #if line.length == width - 1 @out.print(line + (@finished_p ? "\n" : "\r")) @out.flush #elsif line.length >= width # @terminal_width = [@terminal_width - (line.length - width + 1), 0].max # if @terminal_width == 0 then @out.print(line + (@finished_p ? "\n" : "\r")) else show end #else # line.length < width - 1 # @terminal_width += width - line.length + 1 # show #end @previous_time = Time.now end |
#show_if_needed ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/experiment/notify.rb', line 97 def show_if_needed return unless @out 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 @finished_p = cur_percentage == 100 # Use "!=" instead of ">" to support negative changes if cur_percentage != prev_percentage || Time.now - @previous_time >= 1 || @finished_p show end end |
#stat ⇒ Object
136 137 138 |
# File 'lib/experiment/notify.rb', line 136 def stat if @finished_p then elapsed else eta end end |