Skip to content

Commit

Permalink
Merge 7e3cbe6 into e29cd5f
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Jun 20, 2018
2 parents e29cd5f + 7e3cbe6 commit ce4f336
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/tty/progressbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/custom_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions spec/unit/render_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
2 changes: 1 addition & 1 deletion spec/unit/resize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"\e[1G[== ]",
"\e[1G[==== ]",
"\e[0m\e[2K\e[1G",
"\e[1G[=== ]",
"\e[1G[=== ] ",
"\e[1G[==== ]",
"\e[1G[=====]\n"
].join)
Expand Down

0 comments on commit ce4f336

Please sign in to comment.