class TTY::Pager::BasicPager

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

@api public

Constants

DEFAULT_PROMPT

Default prompt for paging

@return [Proc]

@api private

PAGE_BREAK

Public Class Methods

new(height: TTY::Screen.height, width: TTY::Screen.width, prompt: DEFAULT_PROMPT, **options) click to toggle source

Create a basic pager

@param [Integer] :height

the terminal height

@param [Integer] :width

the terminal width

@param [Proc] :prompt

a proc object that accepts page number

@api public

Calls superclass method TTY::Pager::Abstract::new
# File lib/tty/pager/basic.rb, line 36
def initialize(height: TTY::Screen.height, width: TTY::Screen.width,
               prompt: DEFAULT_PROMPT, **options)
  super(**options)
  @width   = width
  @prompt  = prompt
  prompt_height = Strings.wrap(prompt.call(100).to_s, width).lines.count
  @page_cursor = PageCursor.new(height - prompt_height)

  reset
end

Public Instance Methods

<<(*args)
Alias for: write
close() click to toggle source

Stop the pager, wait for it to clean up

@api public

# File lib/tty/pager/basic.rb, line 76
def close
  reset
  true
end
puts(text) click to toggle source

Print a line of text to the pager, prompting on page end.

@raise [PagerClosed]

if the pager was closed

@api public

# File lib/tty/pager/basic.rb, line 69
def puts(text)
  send_text(:puts, text)
end
write(*args) click to toggle source

Write text to the pager, prompting on page end.

@raise [PagerClosed]

if the pager was closed

@return [TTY::Pager::BasicPager]

@api public

# File lib/tty/pager/basic.rb, line 55
def write(*args)
  args.each do |text|
    send_text(:write, text)
  end
  self
end
Also aliased as: <<

Private Instance Methods

continue_paging?(input) click to toggle source

Check if paging should be continued

@param [Integer] page

the page number

@return [Boolean]

@api private

# File lib/tty/pager/basic.rb, line 221
def continue_paging?(input)
  if getchar.chomp[/q/i]
    raise PagerClosed.new("The pager tool was closed")
  end
end
create_chunk_from(line) click to toggle source

Convert line to a chunk of text to fit display

@param [String] line

@return [String]

@api private

# File lib/tty/pager/basic.rb, line 167
def create_chunk_from(line)
  chunk = []

  if !@leftover.empty?
    chunk.concat(@leftover)
    @leftover.clear
  end

  Strings.wrap(line, @width).lines.each do |line_part|
    if !@page_cursor.page_break?
      chunk << line_part
      @page_cursor.down_by(1)
    else
      @leftover << line_part
    end
  end

  chunk.join
end
getchar() click to toggle source

Find available character reading method

@api private

# File lib/tty/pager/basic.rb, line 230
def getchar
  input.respond_to?(:getch) ? input.getch : input.getc
end
next_page() click to toggle source

Switch over to the next page

@api private

# File lib/tty/pager/basic.rb, line 199
def next_page
  @page_cursor.next_page
  if @leftover.size > 0
    @page_cursor.down_by(@leftover.size)
  end
end
page_break_prompt() click to toggle source

Dispaly prompt at page break

@api private

# File lib/tty/pager/basic.rb, line 209
def page_break_prompt
  Strings.wrap(@prompt.call(@page_cursor.page_num), @width)
end
remaining_content() click to toggle source

Any remaining content

@return [String]

@api private

# File lib/tty/pager/basic.rb, line 192
def remaining_content
  @leftover.join
end
reset() click to toggle source

Reset internal state

@api private

# File lib/tty/pager/basic.rb, line 86
def reset
  @page_cursor.reset
  @leftover = []
end
send_text(write_method, text) click to toggle source

The lower-level common implementation of printing methods

@return [Boolean]

the success status of writing to the screen

@api private

# File lib/tty/pager/basic.rb, line 140
def send_text(write_method, text)
  text.lines.each do |line|
    chunk = create_chunk_from(line)

    output.public_send(write_method, chunk)

    next unless @page_cursor.page_break?

    output.puts(page_break_prompt)

    continue_paging?(input)

    next_page
  end

  if !remaining_content.empty?
    output.public_send(write_method, remaining_content)
  end
end