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

const_missing, disable, disabled?, enable, enabled?, extended

Instance Method Details

#one_screen?(str) ⇒ 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)


22
23
24
25
26
# File 'lib/rib/extra/paging.rb', line 22

def one_screen? str
  cols, lines = `tput cols`.to_i, `tput lines`.to_i
  (str.count("\n") + 2) <= lines && # count last line and prompt
    str.gsub(/\e\[[^m]*m/, '').size <= cols * lines
end

#page_result(str) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rib/extra/paging.rb', line 28

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

#pagerObject



36
37
38
# File 'lib/rib/extra/paging.rb', line 36

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

#puts(str = '') ⇒ Object

Print if the it fits one screen, paging it through a pager otherwise.



11
12
13
14
15
16
17
18
# File 'lib/rib/extra/paging.rb', line 11

def puts str=''
  return super if Paging.disabled?
  if one_screen?(str)
    super
  else
    page_result(str)
  end
end