diff --git a/lib/tty/progressbar.rb b/lib/tty/progressbar.rb index c47fd8a..cd7d85b 100644 --- a/lib/tty/progressbar.rb +++ b/lib/tty/progressbar.rb @@ -257,7 +257,10 @@ def render @tokens.each do |token, val| formatted = formatted.gsub(":#{token}", val) end - write(formatted, true) + + padded = padout(formatted) + + write(padded, true) @last_render_time = Time.now @last_render_width = display_columns(formatted) @@ -479,8 +482,10 @@ def inspect # # @api private def padout(message) - if @last_render_width > message.length - remaining_width = @last_render_width - message.length + message_length = display_columns(message) + + if @last_render_width > message_length + remaining_width = @last_render_width - message_length message += ' ' * remaining_width end message diff --git a/spec/unit/custom_token_spec.rb b/spec/unit/custom_token_spec.rb index 923a43a..5a3835d 100644 --- a/spec/unit/custom_token_spec.rb +++ b/spec/unit/custom_token_spec.rb @@ -8,7 +8,7 @@ output.rewind expect(output.read).to eq([ "\e[1G(1) Hello Piotr!", - "\e[1G(4) Bye Piotr!\n" + "\e[1G(4) Bye Piotr! \n" ].join) end end diff --git a/spec/unit/render_spec.rb b/spec/unit/render_spec.rb new file mode 100644 index 0000000..89f270f --- /dev/null +++ b/spec/unit/render_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +RSpec.describe TTY::ProgressBar, "#render" do + let(:output) { StringIO.new(+"", "w+") } + + it "pads out longer previous lines" do + progress = TTY::ProgressBar.new ":current_byte" do |config| + config.no_width = true + config.output = output + config.total = 1_048_577 + end + + progress.advance(1) + progress.advance(1_048_574) + progress.advance(1) + progress.advance(1) + + output.rewind + + expect(output.read).to eq([ + "\e[1G1B", + "\e[1G1024.00KB", # must not pad, line is longer + "\e[1G1.00MB ", # must pad out "0KB" + "\e[1G1.00MB", # must not pad, line is equal + ].join) + end +end diff --git a/spec/unit/resize_spec.rb b/spec/unit/resize_spec.rb index 87f4462..f590b24 100644 --- a/spec/unit/resize_spec.rb +++ b/spec/unit/resize_spec.rb @@ -11,7 +11,7 @@ "\e[1G[== ]", "\e[1G[==== ]", "\e[0m\e[2K\e[1G", - "\e[1G[=== ]", + "\e[1G[=== ] ", "\e[1G[==== ]", "\e[1G[=====]\n" ].join)