lib/tty/pager.rb in tty-pager-0.12.1 vs lib/tty/pager.rb in tty-pager-0.13.0
- old
+ new
@@ -1,103 +1,101 @@
# frozen_string_literal: true
-require 'tty-screen'
+require_relative "pager/basic"
+require_relative "pager/null"
+require_relative "pager/system"
+require_relative "pager/version"
-require_relative 'pager/basic'
-require_relative 'pager/null'
-require_relative 'pager/system'
-require_relative 'pager/version'
-
module TTY
- class Pager
+ module Pager
Error = Class.new(StandardError)
- # 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.
- #
- # @api private
- def self.select_pager(enabled, commands)
- if !enabled
- NullPager
- elsif SystemPager.exec_available?(*commands)
- SystemPager
- else
- BasicPager
- end
- end
+ # Raised when pager is closed
+ PagerClosed = Class.new(Error)
- # Create a pager
- #
- # @param [Hash] options
- # @option options [Proc] :prompt
- # a proc object that accepts page number
- # @option options [IO] :input
- # the object to send input to
- # @option options [IO] :output
- # the object to send output to
- # @option options [Boolean] :enabled
- # disable/enable text paging
- #
- # @api public
- def initialize(**options)
- @input = options.fetch(:input) { $stdin }
- @output = options.fetch(:output) { $stdout }
- @enabled = options.fetch(:enabled) { true }
- commands = Array(options[:command])
+ # Raised when user provides unnexpected argument
+ InvalidArgument = Class.new(Error)
- if self.class == TTY::Pager
- @pager = self.class.select_pager(@enabled, commands).new(options)
+ module ClassMethods
+ # Create a pager
+ #
+ # @param [Boolean] :enabled
+ # disable/enable text paging
+ # @param [String] :command
+ # the paging command
+ # @param [IO] :input
+ # the object to send input to
+ # @param [IO] :output
+ # the object to send output to
+ # @param [Proc] :prompt
+ # a proc object that accepts page number
+ # @param [Integer] :width
+ # the terminal width
+ # @param [Integer] :height
+ # the terminal height
+ #
+ # @api public
+ def new(enabled: true, command: nil, **options)
+ select_pager(enabled: enabled, command: command).new(
+ enabled: enabled, command: command, **options)
end
- end
- # Check if pager is enabled
- #
- # @return [Boolean]
- #
- # @api public
- def enabled?
- !!@enabled
- end
+ # Paginate content through null, basic or system pager.
+ #
+ # @example
+ # TTY::Pager.page do |pager|
+ # pager.write "some text"
+ # end
+ #
+ # @param [String] :text
+ # an optional blob of content
+ # @param [String] :path
+ # a path to a file
+ # @param [Boolean] :enabled
+ # whether or not to use null pager
+ # @param [String] :command
+ # the paging command
+ # @param [IO] :input
+ # the object to send input to
+ # @param [IO] :output
+ # the object to send output to
+ #
+ # @api public
+ 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
- # Page the given text through the available pager
- #
- # @param [String] text
- # the text to run through a pager
- #
- # @yield [Integer] page number
- #
- # @return [TTY::Pager]
- #
- # @api public
- def page(text, &callback)
- pager.page(text, &callback)
- self
- end
+ # 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.
+ #
+ # @param [Boolean] enabled
+ # whether or not to allow paging
+ # @param [String] command
+ # the command to run if available
+ #
+ # @api private
+ def select_pager(enabled: true, command: nil)
+ commands = Array(command)
- # The terminal height
- #
- # @api public
- def page_height
- TTY::Screen.height
+ if !enabled
+ NullPager
+ elsif SystemPager.exec_available?(*commands)
+ SystemPager
+ else
+ BasicPager
+ end
+ end
end
- # The terminal width
- #
- # @api public
- def page_width
- TTY::Screen.width
- end
+ extend ClassMethods
- protected
-
- attr_reader :output
-
- attr_reader :input
-
- attr_reader :pager
+ private_class_method :select_pager
end # Pager
end # TTY