Skip to content

Commit

Permalink
Change to prevent resizing an indeterminate bar with unicode characters
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jan 9, 2021
1 parent 854a060 commit 4e07bb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/tty/progressbar/formatter/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,20 @@ def call(value)
# @api private
def format_indeterminate(value, width)
buffer = []
width -= @progress.unknown.size
complete = (width * @progress.ratio).round
incomplete = width - complete
possible_width = width
unknown_char_length = ProgressBar.display_columns(@progress.unknown)
complete_char_length = ProgressBar.display_columns(@progress.complete)
incomplete_char_length = ProgressBar.display_columns(@progress.incomplete)
head_char_length = ProgressBar.display_columns(@progress.head)

possible_width -= unknown_char_length
max_char_length = [complete_char_length, incomplete_char_length,
head_char_length].max
# figure out how many unicode chars would fit normally
# when the bar has total to prevent resizing
possible_width = (possible_width / max_char_length) * max_char_length
complete = (possible_width * @progress.ratio).round
incomplete = possible_width - complete

buffer << " " * complete
buffer << @progress.unknown
Expand Down
25 changes: 25 additions & 0 deletions spec/unit/formatter/bar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,30 @@
"\e[1G[本本本> ]\n"
].join)
end

it "unknown with ascii and unicode chars" do
progress = TTY::ProgressBar.new("[:bar]", output: output, incomplete: rem,
complete: done, head: ">",
unknown: "<#{done}>",
total: nil, width: 9)

5.times { progress.advance }
progress.update(total: 10)
5.times { progress.advance }
output.rewind

expect(output.read).to eq([
"\e[1G[<本> ]",
"\e[1G[<本> ]",
"\e[1G[<本> ]",
"\e[1G[<本> ]",
"\e[1G[<本> ]",
"\e[1G[本> 〜〜]",
"\e[1G[本本> 〜]",
"\e[1G[本本> 〜]",
"\e[1G[本本本> ]",
"\e[1G[本本本> ]\n",
].join)
end
end
end

0 comments on commit 4e07bb5

Please sign in to comment.