Class: Pry::Pager

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

Overview

A pager is an IO-like object that accepts text and either prints it immediately, prints it one page at a time, or streams it to an external program to print one page at a time.

Defined Under Namespace

Classes: NullPager, PageTracker, SimplePager, StopPaging, SystemPager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_pry_) ⇒ Pager

Returns a new instance of Pager



12
13
14
# File 'lib/pry/pager.rb', line 12

def initialize(_pry_)
  @_pry_ = _pry_
end

Instance Attribute Details

#_pry_Object (readonly)

Returns the value of attribute pry



10
11
12
# File 'lib/pry/pager.rb', line 10

def _pry_
  @_pry_
end

Instance Method Details

#best_availableObject (private)

Return an instance of the "best" available pager class -- SystemPager if possible, SimplePager if SystemPager isn't available, and NullPager if the user has disabled paging. All pagers accept output with #puts, #print, #write, and #<<. You must call #close when you're done writing output to a pager, and you must rescue Pry::Pager::StopPaging. These requirements can be avoided by using .open instead.

Parameters:

  • output (#<<)

    ($stdout) An object to send output to.



52
53
54
55
56
57
58
59
60
# File 'lib/pry/pager.rb', line 52

def best_available
  if !_pry_.config.pager
    NullPager.new(_pry_.output)
  elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby?
    SimplePager.new(_pry_.output)
  else
    SystemPager.new(_pry_.output)
  end
end

#enabled?Boolean (private)

Returns:

  • (Boolean)


41
# File 'lib/pry/pager.rb', line 41

def enabled?; !!@enabled; end

#openObject

Yields a pager object (NullPager, SimplePager, or SystemPager). All pagers accept output with #puts, #print, #write, and #<<.

Parameters:

  • output (IO)

    ($stdout) An object to send output to.



31
32
33
34
35
36
37
# File 'lib/pry/pager.rb', line 31

def open
  pager = best_available
  yield pager
rescue StopPaging
ensure
  pager.close if pager
end

#outputObject (private)



43
# File 'lib/pry/pager.rb', line 43

def output; @output; end

#page(text) ⇒ Object

Send the given text through the best available pager (if Pry.config.pager is enabled). If you want to send text through in chunks as you generate it, use open to get a writable object instead.

Parameters:

  • text (String)

    A piece of text to run through a pager.

  • output (IO)

    ($stdout) An object to send output to.



22
23
24
25
26
# File 'lib/pry/pager.rb', line 22

def page(text)
  open do |pager|
    pager << text
  end
end