Skip to content

Commit 57fe753

Browse files
authored
Merge pull request #616 from Choms/master
Fix print_wrapped to properly parse "\x5" newline character
2 parents 7a36534 + f08570e commit 57fe753

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/thor/shell/basic.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,20 @@ def print_wrapped(message, options = {})
230230
paras = message.split("\n\n")
231231

232232
paras.map! do |unwrapped|
233-
unwrapped.strip.tr("\n", " ").squeeze(" ").gsub(/.{1,#{width}}(?:\s|\Z)/) { ($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n") }
234-
end
233+
counter = 0
234+
unwrapped.split(" ").inject do |memo, word|
235+
word = word.gsub(/\n\005/, "\n").gsub(/\005/, "\n")
236+
counter = 0 if word.include? "\n"
237+
if (counter + word.length + 1) < width
238+
memo = "#{memo} #{word}"
239+
counter += (word.length + 1)
240+
else
241+
memo = "#{memo}\n#{word}"
242+
counter = word.length
243+
end
244+
memo
245+
end
246+
end.compact!
235247

236248
paras.each do |para|
237249
para.split("\n").each do |line|

0 commit comments

Comments
 (0)