[ruby/irb] Add OMIT_ON_ASSIGNMENT
Omit the results evaluated at assignment if they are too long. The behavior of ECHO_ON_ASSIGNMENT being on by default is hard to understand, so I change it to off by default. Instead, we turn OMIT_ON_ASSIGNMENT on by default. The result is displayed on assignment, but it will always be short and within one line of the screen. https://github.com/ruby/irb/commit/c5ea79d5ce
This commit is contained in:
parent
5d841f5631
commit
e468d9f49c
26
lib/irb.rb
26
lib/irb.rb
@ -10,6 +10,7 @@
|
|||||||
#
|
#
|
||||||
#
|
#
|
||||||
require "ripper"
|
require "ripper"
|
||||||
|
require "reline"
|
||||||
|
|
||||||
require_relative "irb/init"
|
require_relative "irb/init"
|
||||||
require_relative "irb/context"
|
require_relative "irb/context"
|
||||||
@ -538,7 +539,15 @@ module IRB
|
|||||||
begin
|
begin
|
||||||
line.untaint if RUBY_VERSION < '2.7'
|
line.untaint if RUBY_VERSION < '2.7'
|
||||||
@context.evaluate(line, line_no, exception: exc)
|
@context.evaluate(line, line_no, exception: exc)
|
||||||
output_value if @context.echo? && (@context.echo_on_assignment? || !assignment_expression?(line))
|
if @context.echo?
|
||||||
|
if assignment_expression?(line)
|
||||||
|
if @context.echo_on_assignment?
|
||||||
|
output_value(@context.omit_on_assignment?)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
output_value
|
||||||
|
end
|
||||||
|
end
|
||||||
rescue Interrupt => exc
|
rescue Interrupt => exc
|
||||||
rescue SystemExit, SignalException
|
rescue SystemExit, SignalException
|
||||||
raise
|
raise
|
||||||
@ -737,9 +746,22 @@ module IRB
|
|||||||
p
|
p
|
||||||
end
|
end
|
||||||
|
|
||||||
def output_value # :nodoc:
|
def output_value(omit = false) # :nodoc:
|
||||||
str = @context.inspect_last_value
|
str = @context.inspect_last_value
|
||||||
multiline_p = str.include?("\n")
|
multiline_p = str.include?("\n")
|
||||||
|
if omit
|
||||||
|
if multiline_p
|
||||||
|
str.gsub!(/(\A.*?\n).*/m, "\\1...")
|
||||||
|
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
|
||||||
|
lines, _ = Reline::Unicode.split_by_width(str, winwidth - diff_size - 3)
|
||||||
|
str = "%s...\e[0m" % lines.first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
if multiline_p && @context.newline_before_multiline_output?
|
if multiline_p && @context.newline_before_multiline_output?
|
||||||
printf @context.return_format, "\n#{str}"
|
printf @context.return_format, "\n#{str}"
|
||||||
else
|
else
|
||||||
|
@ -131,7 +131,12 @@ module IRB
|
|||||||
|
|
||||||
@echo_on_assignment = IRB.conf[:ECHO_ON_ASSIGNMENT]
|
@echo_on_assignment = IRB.conf[:ECHO_ON_ASSIGNMENT]
|
||||||
if @echo_on_assignment.nil?
|
if @echo_on_assignment.nil?
|
||||||
@echo_on_assignment = false
|
@echo_on_assignment = true
|
||||||
|
end
|
||||||
|
|
||||||
|
@omit_on_assignment = IRB.conf[:OMIT_ON_ASSIGNMENT]
|
||||||
|
if @omit_on_assignment.nil?
|
||||||
|
@omit_on_assignment = true
|
||||||
end
|
end
|
||||||
|
|
||||||
@newline_before_multiline_output = IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]
|
@newline_before_multiline_output = IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]
|
||||||
@ -251,13 +256,27 @@ module IRB
|
|||||||
attr_accessor :echo
|
attr_accessor :echo
|
||||||
# Whether to echo for assignment expressions
|
# Whether to echo for assignment expressions
|
||||||
#
|
#
|
||||||
# Uses <code>IRB.conf[:ECHO_ON_ASSIGNMENT]</code> if available, or defaults to +false+.
|
# Uses <code>IRB.conf[:ECHO_ON_ASSIGNMENT]</code> if available, or defaults to +true+.
|
||||||
#
|
#
|
||||||
# a = "omg"
|
# a = "omg"
|
||||||
# IRB.CurrentContext.echo_on_assignment = true
|
|
||||||
# a = "omg"
|
|
||||||
# #=> omg
|
# #=> omg
|
||||||
|
# IRB.CurrentContext.echo_on_assignment = false
|
||||||
|
# a = "omg"
|
||||||
attr_accessor :echo_on_assignment
|
attr_accessor :echo_on_assignment
|
||||||
|
# Whether to omit echo for assignment expressions
|
||||||
|
#
|
||||||
|
# Uses <code>IRB.conf[:OMIT_ON_ASSIGNMENT]</code> if available, or defaults to +true+.
|
||||||
|
#
|
||||||
|
# a = [1] * 10
|
||||||
|
# #=> [1, 1, 1, 1, 1, 1, 1, 1, ...
|
||||||
|
# [1] * 10
|
||||||
|
# #=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
||||||
|
# IRB.CurrentContext.omit_on_assignment = false
|
||||||
|
# a = [1] * 10
|
||||||
|
# #=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
||||||
|
# [1] * 10
|
||||||
|
# #=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
||||||
|
attr_accessor :omit_on_assignment
|
||||||
# Whether a newline is put before multiline output.
|
# Whether a newline is put before multiline output.
|
||||||
#
|
#
|
||||||
# Uses <code>IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]</code> if available,
|
# Uses <code>IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]</code> if available,
|
||||||
@ -306,6 +325,7 @@ module IRB
|
|||||||
alias ignore_eof? ignore_eof
|
alias ignore_eof? ignore_eof
|
||||||
alias echo? echo
|
alias echo? echo
|
||||||
alias echo_on_assignment? echo_on_assignment
|
alias echo_on_assignment? echo_on_assignment
|
||||||
|
alias omit_on_assignment? omit_on_assignment
|
||||||
alias newline_before_multiline_output? newline_before_multiline_output
|
alias newline_before_multiline_output? newline_before_multiline_output
|
||||||
|
|
||||||
# Returns whether messages are displayed or not.
|
# Returns whether messages are displayed or not.
|
||||||
|
@ -52,6 +52,7 @@ module IRB # :nodoc:
|
|||||||
@CONF[:IGNORE_EOF] = false
|
@CONF[:IGNORE_EOF] = false
|
||||||
@CONF[:ECHO] = nil
|
@CONF[:ECHO] = nil
|
||||||
@CONF[:ECHO_ON_ASSIGNMENT] = nil
|
@CONF[:ECHO_ON_ASSIGNMENT] = nil
|
||||||
|
@CONF[:OMIT_ON_ASSIGNMENT] = nil
|
||||||
@CONF[:VERBOSE] = nil
|
@CONF[:VERBOSE] = nil
|
||||||
|
|
||||||
@CONF[:EVAL_HISTORY] = nil
|
@CONF[:EVAL_HISTORY] = nil
|
||||||
@ -177,6 +178,10 @@ module IRB # :nodoc:
|
|||||||
@CONF[:ECHO_ON_ASSIGNMENT] = true
|
@CONF[:ECHO_ON_ASSIGNMENT] = true
|
||||||
when "--noecho-on-assignment"
|
when "--noecho-on-assignment"
|
||||||
@CONF[:ECHO_ON_ASSIGNMENT] = false
|
@CONF[:ECHO_ON_ASSIGNMENT] = false
|
||||||
|
when "--omit-on-assignment"
|
||||||
|
@CONF[:OMIT_ON_ASSIGNMENT] = true
|
||||||
|
when "--noomit-on-assignment"
|
||||||
|
@CONF[:OMIT_ON_ASSIGNMENT] = false
|
||||||
when "--verbose"
|
when "--verbose"
|
||||||
@CONF[:VERBOSE] = true
|
@CONF[:VERBOSE] = true
|
||||||
when "--noverbose"
|
when "--noverbose"
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
require_relative 'src_encoding'
|
require_relative 'src_encoding'
|
||||||
require_relative 'magic-file'
|
require_relative 'magic-file'
|
||||||
require_relative 'completion'
|
require_relative 'completion'
|
||||||
|
require 'io/console'
|
||||||
require 'reline'
|
require 'reline'
|
||||||
|
|
||||||
module IRB
|
module IRB
|
||||||
@ -36,6 +37,14 @@ module IRB
|
|||||||
end
|
end
|
||||||
public :gets
|
public :gets
|
||||||
|
|
||||||
|
def winsize
|
||||||
|
if instance_variable_defined?(:@stdout)
|
||||||
|
@stdout.winsize
|
||||||
|
else
|
||||||
|
[24, 80]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Whether this input method is still readable when there is no more data to
|
# Whether this input method is still readable when there is no more data to
|
||||||
# read.
|
# read.
|
||||||
#
|
#
|
||||||
|
@ -30,6 +30,10 @@ module TestIRB
|
|||||||
def reset
|
def reset
|
||||||
@line_no = 0
|
@line_no = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def winsize
|
||||||
|
[10, 20]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -213,6 +217,75 @@ module TestIRB
|
|||||||
assert_equal("", out)
|
assert_equal("", out)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_omit_on_assignment
|
||||||
|
input = TestInputMethod.new([
|
||||||
|
"a = [1] * 100\n",
|
||||||
|
"a\n",
|
||||||
|
])
|
||||||
|
value = [1] * 100
|
||||||
|
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("=> #{value.inspect}\n", out)
|
||||||
|
|
||||||
|
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.inspect[0..(input.winsize.last - 9)]}...\e[0m\n=> #{value.inspect}\n", out)
|
||||||
|
|
||||||
|
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("=> #{value.inspect}\n=> #{value.inspect}\n", out)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
def test_echo_on_assignment_conf
|
def test_echo_on_assignment_conf
|
||||||
# Default
|
# Default
|
||||||
IRB.conf[:ECHO] = nil
|
IRB.conf[:ECHO] = nil
|
||||||
@ -221,22 +294,26 @@ module TestIRB
|
|||||||
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
||||||
|
|
||||||
assert(irb.context.echo?, "echo? should be true by default")
|
assert(irb.context.echo?, "echo? should be true by default")
|
||||||
refute(irb.context.echo_on_assignment?, "echo_on_assignment? should be false by default")
|
assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true by default")
|
||||||
|
assert(irb.context.omit_on_assignment?, "omit_on_assignment? should be true by default")
|
||||||
|
|
||||||
# Explicitly set :ECHO to false
|
# Explicitly set :ECHO to false
|
||||||
IRB.conf[:ECHO] = false
|
IRB.conf[:ECHO] = false
|
||||||
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
||||||
|
|
||||||
refute(irb.context.echo?, "echo? should be false when IRB.conf[:ECHO] is set to false")
|
refute(irb.context.echo?, "echo? should be false when IRB.conf[:ECHO] is set to false")
|
||||||
refute(irb.context.echo_on_assignment?, "echo_on_assignment? should be false by default")
|
assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true by default")
|
||||||
|
assert(irb.context.omit_on_assignment?, "omit_on_assignment? should be true by default")
|
||||||
|
|
||||||
# Explicitly set :ECHO_ON_ASSIGNMENT to true
|
# Explicitly set :ECHO_ON_ASSIGNMENT to true
|
||||||
IRB.conf[:ECHO] = nil
|
IRB.conf[:ECHO] = nil
|
||||||
IRB.conf[:ECHO_ON_ASSIGNMENT] = true
|
IRB.conf[:ECHO_ON_ASSIGNMENT] = false
|
||||||
|
IRB.conf[:OMIT_ON_ASSIGNMENT] = false
|
||||||
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
||||||
|
|
||||||
assert(irb.context.echo?, "echo? should be true by default")
|
assert(irb.context.echo?, "echo? should be true by default")
|
||||||
assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true when IRB.conf[:ECHO_ON_ASSIGNMENT] is set to true")
|
refute(irb.context.echo_on_assignment?, "echo_on_assignment? should be false when IRB.conf[:ECHO_ON_ASSIGNMENT] is set to false")
|
||||||
|
refute(irb.context.omit_on_assignment?, "omit_on_assignment? should be false when IRB.conf[:OMIT_ON_ASSIGNMENT] is set to false")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_multiline_output_on_default_inspector
|
def test_multiline_output_on_default_inspector
|
||||||
|
Loading…
x
Reference in New Issue
Block a user