Skip to content

Commit 46075de

Browse files
Fixes a bug in the calculation of the print_wrapped method
Fixes a bug that was causing the print_wrapped method to incorrectly wrap the text and print beyond the edge of the terminal. The reason for the bug is that the function assumed that the iteration started with the first word but it actually started with the second word and the first word was already in memo.
1 parent 0e5efb8 commit 46075de

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/thor/shell/basic.rb

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

232232
paras.map! do |unwrapped|
233-
counter = 0
234-
unwrapped.split(" ").inject do |memo, word|
233+
words = unwrapped.split(" ")
234+
counter = words.first.length
235+
words.inject do |memo, word|
235236
word = word.gsub(/\n\005/, "\n").gsub(/\005/, "\n")
236237
counter = 0 if word.include? "\n"
237238
if (counter + word.length + 1) < width

spec/shell/basic_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,46 @@ def shell
175175
end
176176
end
177177

178+
describe "#print_wrapped" do
179+
let(:message) do
180+
"Creates a back-up of the given folder by compressing it in a .tar.gz\n"\
181+
"file and then uploading it to the configured Amazon S3 Bucket.\n\n"\
182+
"It does not verify the integrity of the generated back-up."
183+
end
184+
185+
before do
186+
allow(ENV).to receive(:[]).with("THOR_COLUMNS").and_return(80)
187+
end
188+
189+
context "without indentation" do
190+
subject(:wrap_text) { described_class.new.print_wrapped(message) }
191+
192+
let(:expected_output) do
193+
"Creates a back-up of the given folder by compressing it in a .tar.gz file and\n"\
194+
"then uploading it to the configured Amazon S3 Bucket.\n\n"\
195+
"It does not verify the integrity of the generated back-up.\n"
196+
end
197+
198+
it "properly wraps the text around the 80th column" do
199+
expect { wrap_text }.to output(expected_output).to_stdout
200+
end
201+
end
202+
203+
context "with indentation" do
204+
subject(:wrap_text) { described_class.new.print_wrapped(message, :indent => 4) }
205+
206+
let(:expected_output) do
207+
" Creates a back-up of the given folder by compressing it in a .tar.gz file\n"\
208+
" and then uploading it to the configured Amazon S3 Bucket.\n\n"\
209+
" It does not verify the integrity of the generated back-up.\n"
210+
end
211+
212+
it "properly wraps the text around the 80th column" do
213+
expect { wrap_text }.to output(expected_output).to_stdout
214+
end
215+
end
216+
end
217+
178218
describe "#say_status" do
179219
it "prints a message to the user with status" do
180220
expect($stdout).to receive(:print).with(" create ~/.thor/command.thor\n")

0 commit comments

Comments
 (0)