class TTY::Pager::BasicPager

A basic pager is used to work on systems where system pager is not supported.

@api public

Constants

PAGE_BREAK

Public Class Methods

new(**options) click to toggle source

Create a basic pager

@option options [Integer] :height

the terminal height

@option options [Integer] :width

the terminal width

@api public

Calls superclass method TTY::Pager::new
# File lib/tty/pager/basic.rb, line 24
def initialize(**options)
  super
  @height  = options.fetch(:height) { page_height }
  @width   = options.fetch(:width)  { page_width }
  @prompt  = options.fetch(:prompt) { default_prompt }
  prompt_height = PAGE_BREAK.lines.to_a.size
  @height -= prompt_height
end

Public Instance Methods

default_prompt() click to toggle source

Default prompt for paging

@return [Proc]

@api private

# File lib/tty/pager/basic.rb, line 38
def default_prompt
  proc { |page_num| output.puts Strings.wrap(PAGE_BREAK % page_num, @width) }
end
page(text, &callback) click to toggle source

Page text

@api public

# File lib/tty/pager/basic.rb, line 45
def page(text, &callback)
  page_num = 1
  leftover = []
  lines_left = @height

  text.lines.each do |line|
    chunk = []
    if !leftover.empty?
      chunk = leftover
      leftover = []
    end
    wrapped_line = Strings.wrap(line, @width)
    wrapped_line.lines.each do |line_part|
      if lines_left > 0
        chunk << line_part
        lines_left -= 1
      else
        leftover << line_part
      end
    end
    output.print(chunk.join)

    if lines_left == 0
      break unless continue_paging?(page_num)
      lines_left = @height
      if leftover.size > 0
        lines_left -= leftover.size
      end
      page_num += 1
      return !callback.call(page_num) unless callback.nil?
    end
  end

  if leftover.size > 0
    output.print(leftover.join)
  end
end

Private Instance Methods

continue_paging?(page_num) click to toggle source

@api private

# File lib/tty/pager/basic.rb, line 86
def continue_paging?(page_num)
  instance_exec(page_num, &@prompt)
  !@input.gets.chomp[/q/i]
end