Skip to content

Commit

Permalink
Add ability to specify width for multi bar
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jun 22, 2018
1 parent b5ffdca commit 9bbb767
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/tty/progressbar/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Multi

def_delegators :@bars, :each, :empty?, :length, :[]

def_delegators :@top_bar, :width, :width=

DEFAULT_INSET = {
top: Gem.win_platform? ? '+ ' : "\u250c ",
middle: Gem.win_platform? ? '|-- ' : "\u251c\u2500\u2500 ",
Expand Down Expand Up @@ -51,6 +53,9 @@ def initialize(*args)
@top_bar = nil
@top_bar = register(format, observable: false) if format

@width = @options[:width]
@top_bar.update(width: @width) if @width

@callbacks = {
progress: [],
stopped: [],
Expand All @@ -73,7 +78,8 @@ def register(format, options = {})
@bars << bar
observe(bar) if observable
if @top_bar
@top_bar.update(total: total, width: total)
@top_bar.update(total: total)
@top_bar.update(width: total) unless @width
end
end

Expand Down
23 changes: 22 additions & 1 deletion spec/unit/multi/width_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:middle) { TTY::ProgressBar::Multi::DEFAULT_INSET[:middle] }
let(:bottom) { TTY::ProgressBar::Multi::DEFAULT_INSET[:bottom] }

it "sets top level bar width to maximum columns when exceeds" do
it "sets top level bar width to maximum columns when exceeds terminal width" do
allow(TTY::Screen).to receive(:width).and_return(15)

bars = TTY::ProgressBar::Multi.new("[:bar] main", output: output)
Expand Down Expand Up @@ -94,4 +94,25 @@
restore
].join)
end

it "sets top level bar width to a custom value" do
bars = TTY::ProgressBar::Multi.new("[:bar] main", output: output, width: 20)

bar1 = bars.register("[:bar] one", total: 20)
bar2 = bars.register("[:bar] two", total: 20)

bar1.advance(10)
bar2.advance(10)

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

0 comments on commit 9bbb767

Please sign in to comment.