From 26b4a7e5a08713f7300e73da32ac6acb0772f3c6 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Wed, 20 Jun 2018 14:54:37 -0700 Subject: [PATCH] Pad formatted output when it gets shorter When using the CurrentByte formatter the rendered progress bar may get shorter. When the current values advances from under 1MB to over 1MB the bar should show "1024.00KB" then "1.00MB". The output was not padded so the end result on the screen would look like "1.00MB0KB" which was not easy to read and does not look nice. Now the output is padded so the characters left over from the last render are replaced by spaces so the result on the screen will look like "1.00MB". To accomplish this the existing ProgressBar#padout method is called from ProgressBar#render and #padout is updated to use ProgressBar#display_columns so the next render width is correctly calculated. --- lib/tty/progressbar.rb | 11 ++++++++--- spec/unit/custom_token_spec.rb | 2 +- spec/unit/render_spec.rb | 27 +++++++++++++++++++++++++++ spec/unit/resize_spec.rb | 2 +- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 spec/unit/render_spec.rb 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)