Module: Rib::Paging
- Extended by:
- Plugin
- Defined in:
- lib/rib/extra/paging.rb
Instance Attribute Summary
Attributes included from Plugin
Instance Method Summary collapse
-
#one_screen?(output) ⇒ Boolean
‘less -F` can’t cat the output, so we need to detect by ourselves.
- #page_result(output) ⇒ Object
- #pager ⇒ Object
-
#print_result(result) ⇒ Object
Print result if the it fits one screen, paging it through a pager otherwise.
Methods included from Plugin
disable, disabled?, enable, enabled?, extended
Instance Method Details
#one_screen?(output) ⇒ Boolean
‘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.
26 27 28 29 30 |
# File 'lib/rib/extra/paging.rb', line 26 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 |
#page_result(output) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/rib/extra/paging.rb', line 32 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 |
#pager ⇒ Object
40 41 42 |
# File 'lib/rib/extra/paging.rb', line 40 def pager ENV['PAGER'] || 'less -R' end |
#print_result(result) ⇒ Object
Print result if the it fits one screen, paging it through a pager otherwise.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rib/extra/paging.rb', line 12 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 |