Class: TTY::Terminal::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/terminal/pager.rb

Overview

A class responsible for paging text inside terminal

Direct Known Subclasses

BasicPager, SystemPager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil) ⇒ Pager

Initialize a Pager

Parameters:

  • text (String) (defaults to: nil)

    the text to page



20
21
22
23
# File 'lib/tty/terminal/pager.rb', line 20

def initialize(text = nil)
  @text    = text
  @enabled = true
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



8
9
10
# File 'lib/tty/terminal/pager.rb', line 8

def enabled
  @enabled
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Class Method Details

.available(*commands) ⇒ String

Find first available system command for paging

Parameters:

  • commands (Array[String])

Returns:

  • (String)


51
52
53
54
# File 'lib/tty/terminal/pager.rb', line 51

def self.available(*commands)
  commands = commands.empty? ? executables : commands
  commands.compact.uniq.find { |cmd| System.exists?(cmd) }
end

.available?Boolean

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.

Check if paging command exists

Returns:

  • (Boolean)


59
60
61
# File 'lib/tty/terminal/pager.rb', line 59

def self.available?
  !!available
end

.command(*commands) ⇒ String

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.

Finds command to execute pager from shell commands unless configured is provided.

Parameters:

  • commands (Array[String])

Returns:

  • (String)


71
72
73
74
75
76
77
# File 'lib/tty/terminal/pager.rb', line 71

def self.command(*commands)
  @command = if @command && commands.empty?
    @command
  else
    available(*commands)
  end
end

.executablesArray[String]

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.

List possible executables for output paging

Returns:

  • (Array[String])


39
40
41
42
# File 'lib/tty/terminal/pager.rb', line 39

def self.executables
  [ENV['GIT_PAGER'], `git config --get-all core.pager`.split.first,
   ENV['PAGER'], 'less -isr', 'more', 'cat', 'pager']
end

.page(text) ⇒ Object

Pages output using configured pager

Parameters:

  • text (String)

    the text to page



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

def self.page(text)
  return unless TTY.shell.tty?

  if System.unix? && available?
    SystemPager.new(text).page
  else
    BasicPager.new(text).page
  end
end

Instance Method Details

#enabled?Boolean

Check if pager is enabled

Returns:

  • (Boolean)


30
31
32
# File 'lib/tty/terminal/pager.rb', line 30

def enabled?
  !!@enabled
end