lib/progress.rb in progress-0.0.9.0 vs lib/progress.rb in progress-0.0.9.1
- old
+ new
@@ -26,13 +26,13 @@
# 10.times_with_progress('10') do |one_of_10|
# sleep(0.001)
# end
# end
# ==== To output progress as lines (not trying to stay on line)
- # Progress.start('Test', 1000, :lines => true) do
- # 1000.times{ Progress.step }
- # end
+ # Progress.lines = true
+ # ==== To force highlight
+ # Progress.highlight = true
def self.start(title, total = 1, options = {})
levels << new(title, total, levels.length, options)
print_message
if block_given?
result = yield
@@ -84,24 +84,57 @@
def stop
levels.pop.stop
@io.puts if levels.empty?
end
+ def io
+ @io ||= $stderr
+ end
+
def io=(io)
@io = io
end
+ def lines=(value)
+ @lines = !!value
+ end
+ def lines?
+ if @lines.nil?
+ @lines = !io.tty?
+ end
+ @lines
+ end
+
+ def highlight=(value)
+ @highlight = !!value
+ end
+ def highlight?
+ if @highlight.nil?
+ @highlight = !!io.tty?
+ end
+ @highlight
+ end
+
protected
def print_message
message = levels.map{ |level| level.message } * ' > '
- @io ||= $stderr
- @io.sync = true
- if @io.tty? && !levels.any?{ |level| level.options[:lines] }
- @io.print message.ljust(@previous_length || 0).gsub(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" } + "\r"
+ io.sync = true
+
+ unless lines?
+ previous_length = @previous_length || 0
@previous_length = message.length
+ message = message.ljust(previous_length, '>') + "\r"
+ end
+
+ if highlight?
+ message.gsub!(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" }
+ end
+
+ unless lines?
+ io.print message
else
- @io.puts message
+ io.puts message
end
end
def levels
@levels ||= []