8000 Add newline_before_multiline_output · ruby/irb@9eb1801 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 9eb1801

Browse files
committed
Add newline_before_multiline_output
1 parent 6b053ca commit 9eb1801

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

lib/irb.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,13 @@ def prompt(prompt, ltype, indent, line_no) # :nodoc:
736736
end
737737

738738
def output_value # :nodoc:
739-
printf @context.return_format, @context.inspect_last_value
739+
str = @context.inspect_last_value
740+
multiline_p = str.each_line.take(2).length > 1
741+
if multiline_p && @context.newline_before_multiline_output?
742+
printf @context.return_format, "\n#{str}"
743+
else
744+
printf @context.return_format, str
745+
end
740746
end
741747

742748
# Outputs the local variables to this current session, including

lib/irb/context.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def initialize(irb, workspace = nil, input_method = nil)
133133
if @echo_on_assignment.nil?
134134
@echo_on_assignment = false
135135
end
136+
137+
@newline_before_multiline_output = IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]
138+
if @newline_before_multiline_output.nil?
139+
@newline_before_multiline_output = true
140+
end
136141
end
137142

138143
# The top-level workspace, see WorkSpace#main
@@ -253,6 +258,20 @@ def main
253258
# a = "omg"
254259
# #=> omg
255260
attr_accessor :echo_on_assignment
261+
# Whether a newline is put before multiline output.
262+
#
263+
# Uses IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] if available,
264+
# or defaults to +true+.
265+
#
266+
# "abc\ndef"
267+
# #=>
268+
# abc
269+
# def
270+
# IRB.CurrentContext.newline_before_multiline_output = false
271+
# "abc\ndef"
272+
# #=> abc
273+
# def
274+
attr_accessor :newline_before_multiline_output
256275
# Whether verbose messages are displayed or not.
257276
#
258277
# A copy of the default <code>IRB.conf[:VERBOSE]</code>
@@ -287,6 +306,7 @@ def main
287306
alias ignore_eof? ignore_eof
288307
alias echo? echo
289308
alias echo_on_assignment? echo_on_assignment
309+
alias newline_before_multiline_output? newline_before_multiline_output
290310

291311
# Returns whether messages are displayed or not.
292312
def verbose?

test/irb/test_context.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,36 @@ def test_echo_on_assignment_conf
216216
assert(irb.context.echo?, "echo? should be true by default")
217217
assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true when IRB.conf[:ECHO_ON_ASSIGNMENT] is set to true")
218218
end
219+
220+
def test_multiline_output_on_default_inspector
221+
main = Object.new
222+
def main.inspect
223+
"abc\ndef"
224+
end
225+
input = TestInputMethod.new([
226+
"self"
227+
])
228+
irb = IRB::Irb.new(IRB::WorkSpace.new(main), input)
229+
irb.context.return_format = "=> %s\n"
230+
231+
# The default
232+
irb.context.newline_before_multiline_output = true
233+
out, err = capture_io do
234+
irb.eval_input
235+
end
236+
assert_empty err
237+
assert_equal("=> \nabc\ndef\n",
238+
out)
239+
240+
# No newline before multiline output
241+
input.reset
242+
irb.context.newline_before_multiline_output = false
243+
out, err = capture_io do
244+
irb.eval_input
245+
end
246+
assert_empty err
247+
assert_equal("=> abc\ndef\n",
248+
out)
249+
end
219250
end
220251
end

0 commit comments

Comments
 (0)
0