diff options
| author | Stan Lo <stan001212@gmail.com> | 2023-11-30 15:22:17 +0000 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2023-11-30 15:22:22 +0000 |
| commit | f193f96d31ffcd02d91f135ba765d92c23b52c55 (patch) | |
| tree | 758eb77f0fc698b691dfa9275bdd3d48c12af3bf /lib | |
| parent | cc393b4f80be7eccdc84785f684dc7899b0510e4 (diff) | |
[ruby/irb] Page evaluation result's output
(https://github.com/ruby/irb/pull/784)
* Page evaluation result's output
This will make it easier to work with long output that exceeds the terminal's height.
* Use consistent TERM in rendering tests
This makes sure we get consistent result on all platforms.
https://github.com/ruby/irb/commit/4fedce93d3
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/irb.rb | 8 | ||||
| -rw-r--r-- | lib/irb/pager.rb | 23 |
2 files changed, 17 insertions, 14 deletions
diff --git a/lib/irb.rb b/lib/irb.rb index 66149eb455..1ba335c087 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -20,6 +20,7 @@ require_relative "irb/color" require_relative "irb/version" require_relative "irb/easter-egg" require_relative "irb/debug" +require_relative "irb/pager" # IRB stands for "interactive Ruby" and is a tool to interactively execute Ruby # expressions read from the standard input. @@ -859,11 +860,12 @@ module IRB end end end + if multiline_p && @context.newline_before_multiline_output? - printf @context.return_format, "\n#{str}" - else - printf @context.return_format, str + str = "\n" + str end + + Pager.page_content(format(@context.return_format, str), retain_content: true) end # Outputs the local variables to this current session, including diff --git a/lib/irb/pager.rb b/lib/irb/pager.rb index e38d97e3c7..a0db5e93b4 100644 --- a/lib/irb/pager.rb +++ b/lib/irb/pager.rb @@ -7,9 +7,9 @@ module IRB PAGE_COMMANDS = [ENV['RI_PAGER'], ENV['PAGER'], 'less', 'more'].compact.uniq class << self - def page_content(content) + def page_content(content, **options) if content_exceeds_screen_height?(content) - page do |io| + page(**options) do |io| io.puts content end else @@ -17,8 +17,8 @@ module IRB end end - def page - if IRB.conf[:USE_PAGER] && STDIN.tty? && pager = setup_pager + def page(retain_content: false) + if IRB.conf[:USE_PAGER] && STDIN.tty? && pager = setup_pager(retain_content: retain_content) begin pid = pager.pid yield pager @@ -55,19 +55,20 @@ module IRB pageable_height * screen_width < Reline::Unicode.calculate_width(content, true) end - def setup_pager + def setup_pager(retain_content:) require 'shellwords' - PAGE_COMMANDS.each do |pager| - pager = Shellwords.split(pager) - next if pager.empty? + PAGE_COMMANDS.each do |pager_cmd| + cmd = Shellwords.split(pager_cmd) + next if cmd.empty? - if pager.first == 'less' - pager << '-R' unless pager.include?('-R') + if cmd.first == 'less' + cmd << '-R' unless cmd.include?('-R') + cmd << '-X' if retain_content && !cmd.include?('-X') end begin - io = IO.popen(pager, 'w') + io = IO.popen(cmd, 'w') rescue next end |
