class TTY::Pager::Abstract
Constants
- UndefinedMethodError
Attributes
input[R]
output[R]
Public Class Methods
new(input: $stdin, output: $stdout, enabled: true, **_options)
click to toggle source
Create a pager
@param [IO] :input
the object to send input to
@param [IO] :output
the object to send output to
@param [Boolean] :enabled
disable/enable text paging
@api public
# File lib/tty/pager/abstract.rb, line 61 def initialize(input: $stdin, output: $stdout, enabled: true, **_options) @input = input @output = output @enabled = enabled end
page(text = nil, path: nil, **options, &block)
click to toggle source
Paginate content through null, basic or system pager.
@param [String] text
an optional blob of content
@param [String] path
a path to a file
@api public
# 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
Private Class Methods
validate_arguments(text, path, block)
click to toggle source
Disallow mixing input arguments
@raise [IvalidArgument]
@api private
# File lib/tty/pager/abstract.rb, line 39 def self.validate_arguments(text, path, block) message = if !text.nil? && !block.nil? "Cannot give text argument and block at the same time." elsif !text.nil? && !path.nil? "Cannot give text and :path arguments at the same time." elsif !path.nil? && !block.nil? "Cannot give :path argument and block at the same time." end raise(InvalidArgument, message) if message end
Public Instance Methods
close(*)
click to toggle source
# File lib/tty/pager/abstract.rb, line 127 def close(*) raise UndefinedMethodError end
enabled?()
click to toggle source
Check if pager is enabled
@return [Boolean]
@api public
# File lib/tty/pager/abstract.rb, line 72 def enabled? !!@enabled end
page(text = nil, path: nil)
click to toggle source
Page text
@example
page('some long text...')
@param [String] text
the text to paginate
@api public
# 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
puts(*)
click to toggle source
# File lib/tty/pager/abstract.rb, line 123 def puts(*) raise UndefinedMethodError end
try_write(*args)
click to toggle source
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.
@param [Array<String>] *args
strings to send to the pager
@return [Boolean]
the success status of writing to the pager process
@api public
# File lib/tty/pager/abstract.rb, line 112 def try_write(*args) write(*args) true rescue PagerClosed false end
write(*)
click to toggle source
# File lib/tty/pager/abstract.rb, line 119 def write(*) raise UndefinedMethodError end