Sha256: c5cecaf35531bd492bff415d85ac51d635d9c278b9b817c1f5fcf1593a0cf7f7

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

require 'rib'

module Rib::Paging
  extend Rib::Plugin
  Shell.use(self)

  # --------------- Rib API ---------------

  # Print result if the it fits one screen,
  # paging it through a pager otherwise.
  def print_result result
    return super if Paging.disabled?
    output = format_result(result)
    if one_screen?(output)
      puts output
    else
      page_result(output)
    end
  rescue StandardError, SyntaxError => e
    Rib.warn("Error while printing result:\n  #{format_error(e)}")
  end

  # `less -F` can't cat the output, so we need to detect by ourselves.
  # `less -X` would mess up the buffers, so it's not desired, either.
  def one_screen? output
    cols, lines = `tput cols`.to_i, `tput lines`.to_i
    output.count("\n") <= lines &&
      output.gsub(/\e\[[^m]*m/, '').size <= cols * lines
  end

  def page_result output
    less = IO.popen(pager, 'w')
    less.write(output)
    less.close_write
  rescue Errno::EPIPE
    # less quit without consuming all the input
  end

  def pager
    ENV['PAGER'] || 'less -R'
  end
end

pager = ENV['PAGER'] || 'less'

if `which #{pager}`.empty?
  Rib.warn("#{pager} is not available, disabling Rib::Paging")
  Rib::Paging.disable
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rib-1.2.7 lib/rib/extra/paging.rb
rib-1.2.6 lib/rib/extra/paging.rb
rib-1.2.5 lib/rib/extra/paging.rb
rib-1.2.4 lib/rib/extra/paging.rb
rib-1.2.3 lib/rib/extra/paging.rb