summaryrefslogtreecommitdiff
path: root/lib/irb.rb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2020-08-29 20:48:25 +0900
committeraycabta <aycabta@gmail.com>2020-09-14 02:13:11 +0900
commite468d9f49ca34f713c030c623f655a40370e186d (patch)
treed478651b666f38a917863d6bf78c911e7e5f2eeb /lib/irb.rb
parent5d841f563144a4864f0f60af2935e3eb82ee110a (diff)
[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
Diffstat (limited to 'lib/irb.rb')
-rw-r--r--lib/irb.rb26
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index ed2b336..e020aa6 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -10,6 +10,7 @@
#
#
require "ripper"
+require "reline"
require_relative "irb/init"
require_relative "irb/context"
@@ -538,7 +539,15 @@ module IRB
begin
line.untaint if RUBY_VERSION < '2.7'
@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 SystemExit, SignalException
raise
@@ -737,9 +746,22 @@ module IRB
p
end
- def output_value # :nodoc:
+ def output_value(omit = false) # :nodoc:
str = @context.inspect_last_value
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?
printf @context.return_format, "\n#{str}"
else