Class: IRB::Pager

Inherits:
Object show all
Defined in:
lib/irb/pager.rb

Overview

The implementation of this class is borrowed from RDoc’s lib/rdoc/ri/driver.rb. Please do NOT use this class directly outside of IRB.

Constant Summary collapse

PAGE_COMMANDS =
[ENV['RI_PAGER'], ENV['PAGER'], 'less', 'more'].compact.uniq

Class Method Summary collapse

Class Method Details

.page(retain_content: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/irb/pager.rb', line 20

def page(retain_content: false)
  if should_page? && pager = setup_pager(retain_content: retain_content)
    begin
      pid = pager.pid
      yield pager
    ensure
      pager.close
    end
  else
    yield $stdout
  end
# When user presses Ctrl-C, IRB would raise `IRB::Abort`
# But since Pager is implemented by running paging commands like `less` in another process with `IO.popen`,
# the `IRB::Abort` exception only interrupts IRB's execution but doesn't affect the pager
# So to properly terminate the pager with Ctrl-C, we need to catch `IRB::Abort` and kill the pager process
rescue IRB::Abort
  begin
    Process.kill("TERM", pid) if pid
  rescue Errno::ESRCH
    # Pager process already terminated
  end
  nil
rescue Errno::EPIPE
end

.page_content(content, **options) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/irb/pager.rb', line 10

def page_content(content, **options)
  if content_exceeds_screen_height?(content)
    page(**options) do |io|
      io.puts content
    end
  else
    $stdout.puts content
  end
end