class PauseOutput::OutputPager

A class to manage paged output.

Public Class Methods

new(options={}) click to toggle source

Set up the initial values.

# File lib/pause_output/output_pager.rb, line 11
def initialize(options={})
  @options = options
  @lines   = 0
  @chars   = 0
end

Public Instance Methods

<<(obj) click to toggle source

Write out an object as a string.

# File lib/pause_output/output_pager.rb, line 27
def <<(obj)
  write(obj.to_s)
end
write(str) click to toggle source

Write out a general string with page pauses.

# File lib/pause_output/output_pager.rb, line 18
def write(str)
  until str.empty?
    pre,mid,str = str.partition("\n")
    write_str(pre) unless pre.empty?
    writeln unless mid.empty?
  end
end

Private Instance Methods

chars_per_line() click to toggle source

How many characters fit on a line?

# File lib/pause_output/output_pager.rb, line 101
def chars_per_line
  result = @options[:page_width].to_i
  result = MiniTerm.width if result < 20
  result
end
count_lines() click to toggle source

A new line is out, count it!

# File lib/pause_output/output_pager.rb, line 59
def count_lines
  @chars = 0
  @lines += 1

  if @lines >= (lines_per_page - 1)
    case pause.downcase
    when one_line
      @lines -= 1
    when skip_all
      @lines = 0
      raise PauseOutputStop
    when no_pause
      @lines = -1_000_000
    else
      @lines = 0
    end
  end
end
lines_per_page() click to toggle source

How many lines fit on a page?

# File lib/pause_output/output_pager.rb, line 94
def lines_per_page
  result = @options[:page_height].to_i
  result = MiniTerm.height if result < 2
  result
end
no_pause() click to toggle source

Keystroke to disable pauses.

# File lib/pause_output/output_pager.rb, line 124
def no_pause
  @options.key?(:no_pause) ? @options[:no_pause] : "n"
end
one_line() click to toggle source

Keystroke to advance one line.

# File lib/pause_output/output_pager.rb, line 114
def one_line
  @options.key?(:one_line) ? @options[:one_line] : " "
end
pause() click to toggle source

Pause waiting for the user.

# File lib/pause_output/output_pager.rb, line 79
def pause
  msg = pause_message
  $pause_output_out.write(msg)

  MiniTerm.raw do |term|
    result = term.get_raw_char
    term.flush
    result
  end

ensure
  $pause_output_out.write("\r" + " " * msg.length + "\r")
end
pause_message() click to toggle source

Get the text of the pause message.

# File lib/pause_output/output_pager.rb, line 108
def pause_message
  @options.key?(:page_msg) ? @options[:page_msg] :
    "Press space (line), n (no pause), q(uit) or other (page):"
end
skip_all() click to toggle source

Keystroke to quit processing.

# File lib/pause_output/output_pager.rb, line 119
def skip_all
  @options.key?(:skip_all) ? @options[:skip_all] : "q"
end
write_str(str) click to toggle source

Write out a simple string with no embedded new-lines.

# File lib/pause_output/output_pager.rb, line 34
def write_str(str)
  loop do
    len = str.length

    if @chars + len < chars_per_line
      $pause_output_out.write(str)
      @chars += len
      return
    else
      tipping_point = chars_per_line - @chars
      $pause_output_out.write(str[0, tipping_point])
      count_lines

      str = (str[tipping_point..-1])
    end
  end
end
writeln() click to toggle source

Write out a new-line.

# File lib/pause_output/output_pager.rb, line 53
def writeln
  $pause_output_out.write("\n")
  count_lines
end