Module: TTY::Pager::ClassMethods

Included in:
TTY::Pager
Defined in:
lib/tty/pager.rb

Instance Method Summary collapse

Instance Method Details

#new(enabled: true, command: nil, **options) ⇒ Object

Create a pager

Parameters:

  • :enabled (Boolean)

    disable/enable text paging

  • :command (String)

    the paging command

  • :input (IO)

    the object to send input to

  • :output (IO)

    the object to send output to

  • :prompt (Proc)

    a proc object that accepts page number

  • :width (Integer)

    the terminal width

  • :height (Integer)

    the terminal height


37
38
39
40
# File 'lib/tty/pager.rb', line 37

def new(enabled: true, command: nil, **options)
  select_pager(enabled: enabled, command: command).new(
    enabled: enabled, command: command, **options)
end

#page(text = nil, path: nil, enabled: true, command: nil, **options, &block) ⇒ Object

Paginate content through null, basic or system pager.

Examples:

TTY::Pager.page do |pager|
  pager.write "some text"
end

Parameters:

  • :text (String)

    an optional blob of content

  • :path (String)

    a path to a file

  • :enabled (Boolean)

    whether or not to use null pager

  • :command (String)

    the paging command

  • :input (IO)

    the object to send input to

  • :output (IO)

    the object to send output to


63
64
65
66
67
68
# File 'lib/tty/pager.rb', line 63

def page(text = nil, path: nil, enabled: true, command: nil,
         **options, &block)
  select_pager(enabled: enabled, command: command).
    page(text, path: path, enabled: enabled, command: command,
         **options, &block)
end

#select_pager(enabled: true, command: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Select an appriopriate pager

If the user disabled paging then a NullPager is returned, otherwise a check is performed to find native system command to perform pagination with SystemPager. Finally, if no system command is found, a BasicPager is used which is a pure Ruby implementation known to work on any platform.

Parameters:

  • enabled (Boolean) (defaults to: true)

    whether or not to allow paging

  • command (String) (defaults to: nil)

    the command to run if available


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tty/pager.rb', line 84

def select_pager(enabled: true, command: nil)
  commands = Array(command)

  if !enabled
    NullPager
  elsif SystemPager.exec_available?(*commands)
    SystemPager
  else
    BasicPager
  end
end