Module: Rib::Paging

Extended by:
Plugin
Defined in:
lib/rib/extra/paging.rb

Instance Attribute Summary

Attributes included from Plugin

#disabled

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


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

#pagerObject



40
41
42
# File 'lib/rib/extra/paging.rb', line 40

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

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