mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/irb] Omit output if first line of multiline is too long
https://github.com/ruby/irb/commit/0feeae38c5
This commit is contained in:
parent
e468d9f49c
commit
8f9b1902f4
2 changed files with 88 additions and 2 deletions
14
lib/irb.rb
14
lib/irb.rb
|
@ -750,10 +750,20 @@ module IRB
|
|||
str = @context.inspect_last_value
|
||||
multiline_p = str.include?("\n")
|
||||
if omit
|
||||
winwidth = @context.io.winsize.last
|
||||
if multiline_p
|
||||
str.gsub!(/(\A.*?\n).*/m, "\\1...")
|
||||
first_line = str.split("\n").first
|
||||
result = @context.newline_before_multiline_output? ? (@context.return_format % first_line) : first_line
|
||||
output_width = Reline::Unicode.calculate_width(result, true)
|
||||
diff_size = output_width - Reline::Unicode.calculate_width(first_line, true)
|
||||
if diff_size.positive? and output_width > winwidth
|
||||
lines, _ = Reline::Unicode.split_by_width(first_line, winwidth - diff_size - 3)
|
||||
str = "%s...\e[0m" % lines.first
|
||||
multiline_p = false
|
||||
else
|
||||
str.gsub!(/(\A.*?\n).*/m, "\\1...")
|
||||
end
|
||||
else
|
||||
winwidth = @context.io.winsize.last
|
||||
output_width = Reline::Unicode.calculate_width(@context.return_format % str, true)
|
||||
diff_size = output_width - Reline::Unicode.calculate_width(str, true)
|
||||
if diff_size.positive? and output_width > winwidth
|
||||
|
|
|
@ -286,6 +286,82 @@ module TestIRB
|
|||
assert_equal("", out)
|
||||
end
|
||||
|
||||
def test_omit_multiline_on_assignment
|
||||
input = TestInputMethod.new([
|
||||
"class A; def inspect; ([?* * 1000] * 3).join(%{\\n}); end; end; a = A.new\n",
|
||||
"a\n"
|
||||
])
|
||||
value = ([?* * 1000] * 3).join(%{\n})
|
||||
value_first_line = (?* * 1000).to_s
|
||||
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
||||
irb.context.return_format = "=> %s\n"
|
||||
|
||||
irb.context.echo = true
|
||||
irb.context.echo_on_assignment = false
|
||||
irb.context.omit_on_assignment = true
|
||||
out, err = capture_io do
|
||||
irb.eval_input
|
||||
end
|
||||
assert_empty err
|
||||
assert_equal("=> \n#{value}\n", out)
|
||||
irb.context.evaluate('A.remove_method(:inspect)', 0)
|
||||
|
||||
input.reset
|
||||
irb.context.echo = true
|
||||
irb.context.echo_on_assignment = true
|
||||
irb.context.omit_on_assignment = true
|
||||
out, err = capture_io do
|
||||
irb.eval_input
|
||||
end
|
||||
assert_empty err
|
||||
assert_equal("=> #{value_first_line[0..(input.winsize.last - 9)]}...\e[0m\n=> \n#{value}\n", out)
|
||||
irb.context.evaluate('A.remove_method(:inspect)', 0)
|
||||
|
||||
input.reset
|
||||
irb.context.echo = true
|
||||
irb.context.echo_on_assignment = true
|
||||
irb.context.omit_on_assignment = false
|
||||
out, err = capture_io do
|
||||
irb.eval_input
|
||||
end
|
||||
assert_empty err
|
||||
assert_equal("=> \n#{value}\n=> \n#{value}\n", out)
|
||||
irb.context.evaluate('A.remove_method(:inspect)', 0)
|
||||
|
||||
input.reset
|
||||
irb.context.echo = false
|
||||
irb.context.echo_on_assignment = false
|
||||
irb.context.omit_on_assignment = true
|
||||
out, err = capture_io do
|
||||
irb.eval_input
|
||||
end
|
||||
assert_empty err
|
||||
assert_equal("", out)
|
||||
irb.context.evaluate('A.remove_method(:inspect)', 0)
|
||||
|
||||
input.reset
|
||||
irb.context.echo = false
|
||||
irb.context.echo_on_assignment = true
|
||||
irb.context.omit_on_assignment = true
|
||||
out, err = capture_io do
|
||||
irb.eval_input
|
||||
end
|
||||
assert_empty err
|
||||
assert_equal("", out)
|
||||
irb.context.evaluate('A.remove_method(:inspect)', 0)
|
||||
|
||||
input.reset
|
||||
irb.context.echo = false
|
||||
irb.context.echo_on_assignment = true
|
||||
irb.context.omit_on_assignment = false
|
||||
out, err = capture_io do
|
||||
irb.eval_input
|
||||
end
|
||||
assert_empty err
|
||||
assert_equal("", out)
|
||||
irb.context.evaluate('A.remove_method(:inspect)', 0)
|
||||
end
|
||||
|
||||
def test_echo_on_assignment_conf
|
||||
# Default
|
||||
IRB.conf[:ECHO] = nil
|
||||
|
|
Loading…
Reference in a new issue