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
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
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
Stop the pager, wait for it to clean up
@api public
# File lib/tty/pager/basic.rb, line 76 def close reset true end
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 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
Private Instance Methods
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
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
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
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
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
Any remaining content
@return [String]
@api private
# File lib/tty/pager/basic.rb, line 192 def remaining_content @leftover.join end
Reset internal state
@api private
# File lib/tty/pager/basic.rb, line 86 def reset @page_cursor.reset @leftover = [] end
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