Skip to content

Commit 9ef12a0

Browse files
committed
Preserve whitespace alignment in wrap_lines
Split on /(\s+)/ instead of /\s+/ so multi-space gaps in help text (e.g. option alignment) are preserved when wrapping lines.
1 parent 0fe6758 commit 9ef12a0

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

lib/irb/command/base.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,17 @@ def wrap_lines(text, width)
6363
next [line] if line.length <= width
6464

6565
indent = line[/\A\s*/]
66-
words = line.strip.split(/\s+/)
66+
parts = line.strip.split(/(\s+)/)
6767
result = []
6868
current = indent.dup
69-
words.each do |word|
70-
if current == indent
71-
current << word
72-
elsif current.length + 1 + word.length <= width
73-
current << ' ' << word
74-
else
75-
result << current
76-
current = indent.dup + word
69+
parts.each do |part|
70+
if current != indent && current.length + part.length > width
71+
result << current.rstrip
72+
current = indent.dup
7773
end
74+
current << part unless current == indent && part.match?(/\A\s+\z/)
7875
end
79-
result << current unless current == indent
76+
result << current.rstrip unless current == indent
8077
result
8178
end
8279
end

test/irb/test_input_method.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,19 @@ def test_doc_dialog_content_wraps_long_lines
244244
assert_operator stripped.length, :<=, 30, "Line exceeds width: #{line.inspect}"
245245
end
246246
end
247+
248+
def test_wrap_lines_preserves_whitespace_alignment
249+
text = <<~TEXT
250+
-g [query] Filter the output with a query.
251+
-a [aa] Foo bar
252+
TEXT
253+
lines = IRB::Command::Base.send(:wrap_lines, text, 30)
254+
expected = <<~EXPECTED.chomp
255+
-g [query] Filter the output
256+
with a query.
257+
-a [aa] Foo bar
258+
EXPECTED
259+
assert_equal expected, lines.join("\n")
260+
end
247261
end
248262
end

0 commit comments

Comments
 (0)