Sha256: eb325c7c97c1e0e95f933e6cb68d8e3877964d5b690309a97fcbd2033842a06c
Contents?: true
Size: 1.41 KB
Versions: 4
Compression:
Stored size: 1.41 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 elsif `which tput`.empty? Rib.warn("tput is not available, disabling Rib::Paging") Rib::Paging.disable elsif ENV['TERM'] == 'dumb' || ENV['TERM'].nil? Rib.warn("Your terminal is dumb, disabling Rib::Paging") Rib::Paging.disable end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
rib-1.4.0 | lib/rib/extra/paging.rb |
rib-1.3.1 | lib/rib/extra/paging.rb |
rib-1.3.0 | lib/rib/extra/paging.rb |
rib-1.2.91 | lib/rib/extra/paging.rb |