Class: TTY::Pager::Abstract

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

Direct Known Subclasses

BasicPager, NullPager, SystemPager

Constant Summary collapse

UndefinedMethodError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stdout, enabled: true, **_options) ⇒ Abstract

Create a pager

Parameters:

  • :input (IO)

    the object to send input to

  • :output (IO)

    the object to send output to

  • :enabled (Boolean)

    disable/enable text paging



61
62
63
64
65
# File 'lib/tty/pager/abstract.rb', line 61

def initialize(input: $stdin, output: $stdout, enabled: true, **_options)
  @input   = input
  @output  = output
  @enabled = enabled
end

Class Method Details

.page(text = nil, path: nil, **options, &block) ⇒ Object

Paginate content through null, basic or system pager.

Parameters:

  • text (String) (defaults to: nil)

    an optional blob of content

  • path (String) (defaults to: nil)

    a path to a file



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tty/pager/abstract.rb', line 16

def self.page(text = nil, path: nil, **options, &block)
  validate_arguments(text, path, block)

  instance = new(**options)

  begin
    if block_given?
      block.call(instance)
    else
      instance.page(text, path: path)
    end
  rescue PagerClosed
    # do nothing
  ensure
    instance.close
  end
end

Instance Method Details

#closeObject



127
128
129
# File 'lib/tty/pager/abstract.rb', line 127

def close(*)
  raise UndefinedMethodError
end

#enabled?Boolean

Check if pager is enabled

Returns:

  • (Boolean)


72
73
74
# File 'lib/tty/pager/abstract.rb', line 72

def enabled?
  !!@enabled
end

#page(text = nil, path: nil) ⇒ Object

Page text

Examples:

page('some long text...')

Parameters:

  • text (String) (defaults to: nil)

    the text to paginate



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

def page(text = nil, path: nil)
  if path
    IO.foreach(path) do |line|
      write(line)
    end
  else
    write(text)
  end
rescue PagerClosed
  # do nothing
ensure
  close
end

#putsObject



123
124
125
# File 'lib/tty/pager/abstract.rb', line 123

def puts(*)
  raise UndefinedMethodError
end

#try_write(*args) ⇒ Boolean

Try writing to the pager. Return false if the pager was closed.

In case of system pager, send text to the pager process. Start a new process if it hasn’t been started yet.

Parameters:

  • *args (Array<String>)

    strings to send to the pager

Returns:

  • (Boolean)

    the success status of writing to the pager process



112
113
114
115
116
117
# File 'lib/tty/pager/abstract.rb', line 112

def try_write(*args)
  write(*args)
  true
rescue PagerClosed
  false
end

#writeObject



119
120
121
# File 'lib/tty/pager/abstract.rb', line 119

def write(*)
  raise UndefinedMethodError
end