Skip to content

Commit

Permalink
Change to use resume to allow greater control of a single progress ba…
Browse files Browse the repository at this point in the history
…r rendering (ref #37)
  • Loading branch information
piotrmurach committed Jul 11, 2020
1 parent 7f795f1 commit 40ccfca
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/tty/progressbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ def finish
emit(:done)
end

# Continue rendering if after bar is done total number is updated
# Resume rendering when bar is done or stopped to update information
#
# @api public
def continue
def resume
@done = false
@stopped = false
end
Expand Down
13 changes: 12 additions & 1 deletion lib/tty/progressbar/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def register(format, options = {})
observe(bar) if observable
if @top_bar
@top_bar.update(total: total)
@top_bar.continue
@top_bar.resume if @top_bar.done?
@top_bar.update(width: total) unless @width
end
end
Expand Down Expand Up @@ -177,6 +177,17 @@ def stopped?
end
end

# Check if all bars are stopped or finished
#
# @return [Boolean]
#
# @api public
def done?
synchronize do
(@bars - [@top_bar]).dup.all?(&:done?)
end
end

# Stop all progress bars
#
# @api public
Expand Down
43 changes: 43 additions & 0 deletions spec/unit/multi/resume_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
RSpec.describe TTY::ProgressBar::Multi.new, "#resume" do
let(:output) { StringIO.new('', 'w+') }
let(:save) { TTY::Cursor.save }
let(:restore) { TTY::Cursor.restore }
let(:top) { TTY::ProgressBar::Multi::DEFAULT_INSET[:top] }
let(:middle) { TTY::ProgressBar::Multi::DEFAULT_INSET[:middle] }
let(:bottom) { TTY::ProgressBar::Multi::DEFAULT_INSET[:bottom] }

it "resumes top bar when new bar registered and advanced" do
top_bar = TTY::ProgressBar::Multi.new("[:bar] top (:current/:total)", output: output)

bar1 = top_bar.register("[:bar] one", total: 5)
bar1. advance(5)

expect(top_bar.done?).to eq(true)
expect(bar1.done?).to eq(true)

bar2 = top_bar.register("[:bar] two", total: 5)
bar2.advance

output.rewind
expect(output.string).to eq([
"\e[1G#{top}[=====] top (5/5)\n",
save,
"\e[1A",
"#{top}\n",
restore,
"\e[1G#{bottom}[=====] one\n",
save,
"\e[1A#{bottom}\n",
restore,
save,
"\e[2A", # up 2 lines
"\e[1G#{top}[====== ] top (6/10)",
restore,
"\e[1G#{bottom}[= ] two\n"
].join)

expect(top_bar.done?).to eq(false)
expect(bar1.done?).to eq(true)
expect(bar2.done?).to eq(false)
end
end

0 comments on commit 40ccfca

Please sign in to comment.