lib/powerbar.rb in powerbar-1.0.9 vs lib/powerbar.rb in powerbar-1.0.10
- old
+ new
@@ -27,10 +27,11 @@
#
# This is PowerBar - The last progressbar-library you'll ever need.
#
class PowerBar
STRIP_ANSI = Regexp.compile '\e\[(\d+)(;\d+)?(;\d+)?[m|K]', nil
+ RUBY18 = RUBY_VERSION[0..2] == "1.8"
def initialize(opts={})
@@exit_hooked = false
@state = Hashie::Mash.new( {
:time_last_show => Time.at(0), # <- don't mess with us
@@ -69,12 +70,12 @@
:main => '${<msg>}: ${[<bar>] }${<rate>/s }${<percent>% }${<elapsed>}${, ETA: <eta>}',
:post => '', # printed after the progressbar
:wipe => "\e[0m\e[1000D\e[K", # printed when 'wipe' is called
:close => "\e[?25h\n", # printed when 'close' is called
:exit => "\e[?25h", # printed if the process exits unexpectedly
- :barchar => "\u2588", # fill-char for the progress-bar
- :padchar => "\u2022" # padding-char for the progress-bar
+ :barchar => RUBY18 ? '#' : "\u2588", # fill-char for the progress-bar
+ :padchar => RUBY18 ? '.' : "\u2022" # padding-char for the progress-bar
},
},
:infinite => { # <== Settings for an infinite progress "bar" (when total is :unknown)
:output => Proc.new{ |s| $stderr.print s[0..terminal_width()-1] },
:interval => 0.1,
@@ -84,12 +85,12 @@
:main => "${<msg>}: ${<done> }${<rate>/s }${<elapsed>}",
:post => "\e[K",
:wipe => "\e[0m\e[1000D\e[K",
:close => "\e[?25h\n",
:exit => "\e[?25h",
- :barchar => "\u2588",
- :padchar => "\u2022"
+ :barchar => RUBY18 ? '#' : "\u2588",
+ :padchar => RUBY18 ? '.' : "\u2022"
},
}
},
:notty => { # <== Settings when stdout is not a tty
:finite => {
@@ -286,11 +287,11 @@
def rate
@rate.avg
end
def h_rate
- humanize_quantity(rate.round(1))
+ humanize_quantity(round(rate, 1))
end
def total
state.total
end
@@ -314,10 +315,19 @@
private
def state
@state
end
+ def round(number, digit)
+ if number.zero? or number.nan?
+ number
+ else
+ factor = 10.0 ** digit
+ (number * factor).round / factor
+ end
+ end
+
# Cap'n Hook
def exit!
return if state.closed
scope.output.call(scope.template.exit) unless scope.template.exit.nil?
end
@@ -325,21 +335,21 @@
def render_template(tplid=:main, skip=[])
tpl = scope.template[tplid]
skip.each do |s|
tpl = tpl.gsub(/\$\{([^<]*)<#{s}>([^}]*)\}/, '\1\2')
end
- tpl.gsub(/\${[^}]+}/) do |var|
+ tpl.gsub(/\$\{[^\}]+\}/) do |var|
sub = nil
r = var.gsub(/<[^>]+>/) do |t|
t = t[1..-2]
begin
- sub = self.send(('h_'+t).to_sym)
+ sub = send "h_#{t}"
rescue NoMethodError
raise NameError, "Invalid token '#{t}' in template '#{tplid}'"
end
- end[2..-2]
- sub.nil? ? '' : r
+ end
+ sub.nil? ? '' : r[2..-2]
end
end
HQ_UNITS = %w(b k M G T).freeze
def humanize_quantity(number, format='%n%u')
@@ -353,10 +363,10 @@
exponent = (Math.log(number) / Math.log(kilo)).to_i
exponent = max_exp if exponent > max_exp
number /= kilo ** exponent
unit = HQ_UNITS[exponent]
- return format.gsub(/%n/, number.round(1).to_s).gsub(/%u/, unit)
+ return format.gsub(/%n/, round(number, 1).to_s).gsub(/%u/, unit)
end
def humanize_interval(s)
return nil if s.nil? or s.infinite?
sprintf("%02d:%02d:%02d", s / 3600, s / 60 % 60, s % 60)