lib/progress/class_methods.rb in progress-3.4.0 vs lib/progress/class_methods.rb in progress-3.5.0
- old
+ new
@@ -10,10 +10,11 @@
# start progress indication
def start(total = nil, title = nil)
init(total, title)
print_message force: true
return unless block_given?
+
begin
yield
ensure
stop
end
@@ -42,10 +43,11 @@
end
# stop progress
def stop
return unless running?
+
if @levels.length == 1
print_message force: true, finish: true
stop_beeper
end
@levels.pop
@@ -57,10 +59,11 @@
end
# set note
def note=(note)
return unless running?
+
@levels.last.note = note
end
# stay on one line
def stay_on_line?
@@ -80,11 +83,11 @@
# explicitly set highlighting [true/false/nil]
def highlight=(value)
@highlight = true && value
end
- # show progerss in terminal title
+ # show progress in terminal title
def terminal_title?
@terminal_title.nil? ? io_tty? : @terminal_title
end
# explicitly set showing progress in terminal title [true/false/nil]
@@ -100,10 +103,19 @@
def io_tty?
io.tty? || ENV['PROGRESS_TTY']
end
+ # don't refresh progress (eta) periodically for the duration of the block
+ def without_beeper
+ old_state = @without_beeper
+ @without_beeper = true
+ yield
+ ensure
+ @without_beeper = old_state
+ end
+
private
attr_reader :eta
def init(total = nil, title = nil)
@@ -137,11 +149,11 @@
end
end
def start_beeper
@beeper = Beeper.new(10) do
- print_message
+ print_message unless @without_beeper
end
end
def stop_beeper
@beeper.stop if @beeper
@@ -186,20 +198,21 @@
out
end
def build_message(options)
current = 0
- message = @levels.reverse.map do |level|
+ reverse_parts = @levels.reverse.map do |level|
current = level.to_f(current)
part = current.zero? ? '......' : format('%5.1f%%', current * 100.0)
if highlight? && part != '100.0%'
part = "\e[1m#{part}\e[0m"
end
level.title ? "#{level.title}: #{part}" : part
- end.reverse * ' > '
+ end
+ message = reverse_parts.reverse * ' > '
if options[:finish]
message << " (elapsed: #{eta.elapsed})"
elsif (left = eta.left(current))
message << " (ETA: #{left})"