rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.12k stars 553 forks source link

🌈 Fixes a bug in the calculation of the print_wrapped method #719

Closed sergio-bobillier closed 4 years ago

sergio-bobillier commented 4 years ago

Description

This pull request fixes a bug that caused the print_wrapped method in the Thor::Shell::Basic class to incorrectly wrap the text when printing on the terminal. The reason for the bug is that the method assumes that the loop starts with the first word, when in fact it starts with the second word and the first word is already in the memo variable.

Before the change:

Screen Shot 2020-03-12 at 16 56 39

After the change

Screen Shot 2020-03-12 at 16 57 09

To see why this occurs you can run the following code on IRB or PRY:

>%w[hello world! how are you doing].inject { |memo, word| puts "#{memo} : #{word}"  }
hello : world!
 : how
 : are
 : you
 : doing
=> nil

As you can see the first iteration already has "hello" in memo and the first word is actually "word!". The print_wrapped was not accounting for this and therefore given the right conditions the method would print beyond the edge of the terminal.