Module: Pager

Defined in:
lib/pager.rb

Overview

Add a pager() method that can send text to a pager.

With “pager” referring to something like less or moar.

This file attempts to be as close as possible to what Git is doing. For reference, do “git help config”, search for “core.pager” and compare that text to the page() method below.

Constant Summary collapse

DONT_USE_PAGER_VAR =

Checking for this variable before launching $PAGER should prevent us from fork bombing if somebody sets the PAGER environment variable to point to us.

'_RIFF_PREVENT_PAGING_LOOP'

Instance Method Summary collapse

Instance Method Details

#dont_use_pager?Boolean

If $DONT_USE_PAGER_VAR is set, we shouldn’t use $PAGER

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/pager.rb', line 33

def dont_use_pager?
  return true if ENV[DONT_USE_PAGER_VAR]
  return true if ENV['PAGER'].nil?
  return true if ENV['PAGER'].empty?
  return false
end

#less(text) ⇒ Object



28
29
30
# File 'lib/pager.rb', line 28

def less(text)
  pipe_text_into_command(text, 'less')
end

#page(text) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pager.rb', line 40

def page(text)
  if !$stdout.isatty
    print text
  elsif dont_use_pager?
    less(text)
  elsif ENV['PAGER']
    pipe_text_into_command(text, ENV['PAGER'])
  else
    less(text)
  end
end

#pipe_text_into_command(text, command) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pager.rb', line 14

def pipe_text_into_command(text, command)
  env = {
    DONT_USE_PAGER_VAR => '1'
  }

  # Set LESS=FRX unless $LESS already has a value
  env['LESS'] = 'FRX' unless ENV['LESS']

  # Set LV=-c unless $LV already has a value
  env['LV'] = '-c' unless ENV['LV']

  IO.popen(env, command, 'w') { |pager| pager.print text }
end