module Doing::Pager

Pagination

Public Class Methods

page(text) click to toggle source

Page output. If @paginate is false, just dump to STDOUT

@param text [String] text to paginate

# File lib/doing/pager.rb, line 26
def page(text)
  unless @paginate
    puts text
    return
  end

  pager = which_pager
  Doing.logger.debug('Pager:', "Using #{pager}")

  read_io, write_io = IO.pipe

  input = $stdin

  pid = Kernel.fork do
    write_io.close
    input.reopen(read_io)
    read_io.close

    # Wait until we have input before we start the pager
    IO.select [input]

    begin
      exec(pager)
    rescue SystemCallError => e
      raise Errors::DoingStandardError, "Pager error, #{e}"
    end
  end

  begin
    read_io.close
    write_io.write(text)
    write_io.close
  rescue SystemCallError # => e
    # raise Errors::DoingStandardError, "Pager error, #{e}"
  end

  _, status = Process.waitpid2(pid)
  status.success?
end
paginate() click to toggle source

Boolean determines whether output is paginated

# File lib/doing/pager.rb, line 10
def paginate
  @paginate ||= false
end
paginate=(should_paginate) click to toggle source

Enable/disable pagination

@param should_paginate [Boolean] true to paginate

# File lib/doing/pager.rb, line 17
def paginate=(should_paginate)
  @paginate = should_paginate
end

Private Class Methods

find_executable(*commands) click to toggle source
# File lib/doing/pager.rb, line 83
def find_executable(*commands)
  execs = commands.empty? ? pagers : commands
  execs
    .remove_bad.uniq
    .find { |cmd| TTY::Which.exist?(cmd.split.first) }
end
git_pager() click to toggle source
# File lib/doing/pager.rb, line 68
def git_pager
  TTY::Which.exist?('git') ? `#{TTY::Which.which('git')} config --get-all core.pager` : nil
end
pagers() click to toggle source
# File lib/doing/pager.rb, line 72
def pagers
  [
    Doing.setting('editors.pager'),
    ENV['PAGER'],
    'less -FXr',
    ENV['GIT_PAGER'],
    git_pager,
    'more -r'
  ].remove_bad
end
which_pager() click to toggle source
# File lib/doing/pager.rb, line 90
def which_pager
  @which_pager ||= find_executable(*pagers)
end