Sha256: 938604d4468b684413ae8bce59d4af48361193fd7c7443dbbd7210948b01690e

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

# encoding: utf-8

module TTY
  class Terminal
    # A class responsible for paging text
    class BasicPager < Pager
      PROMPT_HEIGHT = 3

      PAGE_BREAK = '--- Press enter/return to continue (or q to quit) ---'

      # Use ruby to page output text
      #
      # @api public
      def page
        text_lines = text.lines.to_a

        text_lines.each_slice(page_size) do |chunk|
          TTY.shell.say chunk.join
          break if chunk.size < page_size
          break if !continue?(text_lines)
        end
      end

      private

      # Check whether to progress with paging
      #
      # @param [Array[String]] text_lines
      #
      # @return [Boolean]
      #
      # @api private
      def continue?(text_lines)
        if text_lines.size > page_size
          question = TTY.shell.ask "\n#{PAGE_BREAK}"
          return false if question.read_string[/q/i]
        end
        true
      end

      # Determine current page size
      #
      # @api private
      def page_size
        @page_size ||= TTY.terminal.height - PROMPT_HEIGHT
      end
    end # BasicPager
  end # Terminal
end # TTY

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tty-0.1.3 lib/tty/terminal/pager/basic.rb
tty-0.1.2 lib/tty/terminal/pager/basic.rb
tty-0.1.1 lib/tty/terminal/pager/basic.rb
tty-0.1.0 lib/tty/terminal/pager/basic.rb