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



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

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

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Class Method Details

.available(*commands) ⇒ String

Find first available system command for paging

Parameters:

  • commands (Array[String])

Returns:

  • (String)


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

def self.available(*commands)
  commands = commands.empty? ? self.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)


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

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)


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

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])


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

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



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

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)


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

def enabled?
  !!@enabled
end