Class: ANSI::ProgressBar
- Inherits:
-
Object
- Object
- ANSI::ProgressBar
- Defined in:
- lib/ansi/progressbar.rb
Overview
Progressbar
Progressbar is a text-based progressbar library.
= Progressbar.new( "Demo", 100 )
100.times { .inc }
.finish
Instance Attribute Summary collapse
-
#format(format, *arguments) ⇒ Object
Set format and format arguments.
-
#format_arguments ⇒ Object
Returns the value of attribute format_arguments.
-
#styles ⇒ Object
Returns the value of attribute styles.
Instance Method Summary collapse
- #bar ⇒ Object private
- #bar_mark=(mark) ⇒ Object (also: #barmark=, #mark=)
- #bytes ⇒ Object private
- #clear ⇒ Object
- #colorize(part, style) ⇒ Object private
- #convert_bytes(bytes) ⇒ Object private
- #elapsed ⇒ Object private
- #eol ⇒ Object private
-
#eta ⇒ Object
private
ETA stands for Estimated Time of Arrival.
- #finish ⇒ Object
- #flush ⇒ Object
- #format_time(t) ⇒ Object private
- #get_width ⇒ Object private
- #halt ⇒ Object
- #inc(step = 1) ⇒ Object
-
#initialize(title, total, out = STDERR) ⇒ ProgressBar
constructor
A new instance of ProgressBar.
- #inspect ⇒ Object
- #percentage ⇒ Object private
- #reset ⇒ Object
- #set(count) ⇒ Object
- #show ⇒ Object private
- #show_progress ⇒ Object private
- #standard_mode ⇒ Object
- #stat ⇒ Object private
- #stat_for_file_transfer ⇒ Object private
-
#style(options) ⇒ Object
Set ANSI styling options.
- #title ⇒ Object private
- #title=(str) ⇒ Object
- #total_overflow=(boolv) ⇒ Object
- #transfer_mode ⇒ Object (also: #file_transfer_mode)
- #transfer_rate ⇒ Object private
Constructor Details
#initialize(title, total, out = STDERR) ⇒ ProgressBar
Returns a new instance of ProgressBar.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ansi/progressbar.rb', line 22 def initialize(title, total, out=STDERR) @title = title @total = total @out = out @bar_length = 80 @bar_mark = "|" @total_overflow = true @current = 0 @previous = 0 @is_finished = false @start_time = Time.now @format = "%-14s %3d%% %s %s" @format_arguments = [:title, :percentage, :bar, :stat] @styles = {} # yield self if block_given? # show_progress end |
Instance Attribute Details
#format(format, *arguments) ⇒ Object
Set format and format arguments.
65 66 67 |
# File 'lib/ansi/progressbar.rb', line 65 def format @format end |
#format_arguments ⇒ Object
Returns the value of attribute format_arguments.
46 47 48 |
# File 'lib/ansi/progressbar.rb', line 46 def format_arguments @format_arguments end |
#styles ⇒ Object
Returns the value of attribute styles.
47 48 49 |
# File 'lib/ansi/progressbar.rb', line 47 def styles @styles end |
Instance Method Details
#bar ⇒ Object (private)
207 208 209 210 |
# File 'lib/ansi/progressbar.rb', line 207 def len = percentage * @bar_length / 100 sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len)) end |
#bar_mark=(mark) ⇒ Object Also known as: barmark=, mark=
54 55 56 |
# File 'lib/ansi/progressbar.rb', line 54 def (mark) @bar_mark = String(mark)[0..0] end |
#bytes ⇒ Object (private)
163 164 165 |
# File 'lib/ansi/progressbar.rb', line 163 def bytes convert_bytes(@current) end |
#clear ⇒ Object
135 136 137 |
# File 'lib/ansi/progressbar.rb', line 135 def clear @out.print(" " * get_width + eol) end |
#colorize(part, style) ⇒ Object (private)
275 276 277 278 279 |
# File 'lib/ansi/progressbar.rb', line 275 def colorize(part, style) return part unless style #[style].flatten.inject(part){ |pt, st| ANSI::Code.ansi(pt, *st) } ANSI::Code.ansi(part, *style) end |
#convert_bytes(bytes) ⇒ Object (private)
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ansi/progressbar.rb', line 146 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 |
#elapsed ⇒ Object (private)
186 187 188 189 |
# File 'lib/ansi/progressbar.rb', line 186 def elapsed elapsed = Time.now - @start_time sprintf("Time: %s", format_time(elapsed)) end |
#eol ⇒ Object (private)
203 204 205 |
# File 'lib/ansi/progressbar.rb', line 203 def eol if @is_finished then "\n" else "\r" end end |
#eta ⇒ Object (private)
ETA stands for Estimated Time of Arrival.
176 177 178 179 180 181 182 183 184 |
# File 'lib/ansi/progressbar.rb', line 176 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 |
#finish ⇒ Object
90 91 92 93 94 |
# File 'lib/ansi/progressbar.rb', line 90 def finish @current = @total @is_finished = true show_progress end |
#flush ⇒ Object
96 97 98 |
# File 'lib/ansi/progressbar.rb', line 96 def flush @out.flush end |
#format_time(t) ⇒ Object (private)
167 168 169 170 171 172 173 |
# File 'lib/ansi/progressbar.rb', line 167 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 (private)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/ansi/progressbar.rb', line 224 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 |
#halt ⇒ Object
100 101 102 103 |
# File 'lib/ansi/progressbar.rb', line 100 def halt @is_finished = true show_progress end |
#inc(step = 1) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/ansi/progressbar.rb', line 127 def inc(step = 1) @current += step @current = @total if @current > @total show_progress @previous = @current end |
#inspect ⇒ Object
139 140 141 |
# File 'lib/ansi/progressbar.rb', line 139 def inspect "(ProgressBar: #{@current}/#{@total})" end |
#percentage ⇒ Object (private)
212 213 214 215 216 217 218 |
# File 'lib/ansi/progressbar.rb', line 212 def percentage if @total.zero? 100 else @current * 100 / @total end end |
#reset ⇒ Object
121 122 123 124 |
# File 'lib/ansi/progressbar.rb', line 121 def reset @current = 0 @is_finished = false end |
#set(count) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ansi/progressbar.rb', line 105 def set(count) if count < 0 raise "invalid count less than zero: #{count}" elsif count > @total if @total_overflow @total = count + 1 else raise "invalid count greater than total: #{count}" end end @current = count show_progress @previous = @current end |
#show ⇒ Object (private)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/ansi/progressbar.rb', line 242 def show arguments = @format_arguments.map do |method| colorize(send(method), styles[method]) end line = sprintf(@format, *arguments) width = get_width length = ANSI::Code.uncolor{line}.length if length == width - 1 @out.print(line + eol) elsif length >= width @bar_length = [@bar_length - (length - width + 1), 0].max @bar_length == 0 ? @out.print(line + eol) : show else #line.length < width - 1 @bar_length += width - length + 1 show end end |
#show_progress ⇒ Object (private)
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/ansi/progressbar.rb', line 261 def show_progress 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 if cur_percentage > prev_percentage || @is_finished show end end |
#standard_mode ⇒ Object
76 77 78 79 |
# File 'lib/ansi/progressbar.rb', line 76 def standard_mode @format = "%-14s %3d%% %s %s" @format_arguments = [:title, :percentage, :bar, :stat] end |
#stat ⇒ Object (private)
191 192 193 |
# File 'lib/ansi/progressbar.rb', line 191 def stat if @is_finished then elapsed else eta end end |
#stat_for_file_transfer ⇒ Object (private)
195 196 197 198 199 200 201 |
# File 'lib/ansi/progressbar.rb', line 195 def stat_for_file_transfer if @is_finished then sprintf("%s %s %s", bytes, transfer_rate, elapsed) else sprintf("%s %s %s", bytes, transfer_rate, eta) end end |
#style(options) ⇒ Object
Set ANSI styling options.
71 72 73 |
# File 'lib/ansi/progressbar.rb', line 71 def style() @styles = end |
#title ⇒ Object (private)
220 221 222 |
# File 'lib/ansi/progressbar.rb', line 220 def title @title[0,13] + ":" end |
#title=(str) ⇒ Object
50 51 52 |
# File 'lib/ansi/progressbar.rb', line 50 def title=(str) @title = str end |
#total_overflow=(boolv) ⇒ Object
60 61 62 |
# File 'lib/ansi/progressbar.rb', line 60 def total_overflow=(boolv) @total_overflow = boolv ? true : false end |
#transfer_mode ⇒ Object Also known as: file_transfer_mode
82 83 84 85 |
# File 'lib/ansi/progressbar.rb', line 82 def transfer_mode @format = "%-14s %3d%% %s %s" @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer] end |
#transfer_rate ⇒ Object (private)
158 159 160 161 |
# File 'lib/ansi/progressbar.rb', line 158 def transfer_rate bytes_per_second = @current.to_f / (Time.now - @start_time) sprintf("%s/s", convert_bytes(bytes_per_second)) end |