class ChefDK::Pager

Attributes

pager_pid[R]

Public Class Methods

new(enable_pager: true) click to toggle source
# File lib/chef-dk/pager.rb, line 25
def initialize(enable_pager: true)
  @enable_pager = enable_pager
  @pipe = nil
end

Public Instance Methods

env() click to toggle source

@api private This is just public so we can stub it for testing

# File lib/chef-dk/pager.rb, line 75
def env
  ENV
end
have_tty?() click to toggle source

@api private This is just public so we can stub it for testing

# File lib/chef-dk/pager.rb, line 81
def have_tty?
  $stdout.tty?
end
pager_enabled?() click to toggle source
# File lib/chef-dk/pager.rb, line 30
def pager_enabled?
  !!(@enable_pager && have_tty? && env["PAGER"])
end
start() click to toggle source
# File lib/chef-dk/pager.rb, line 52
def start
  return false unless pager_enabled?

  # Ignore CTRL-C because it can cause the parent to die before the
  # pager which causes wonky behavior in the terminal
  Kernel.trap(:INT, "IGNORE")

  @pager_pid = Process.spawn(pager_env, env["PAGER"], in: child_stdin)

  child_stdin.close
end
ui() click to toggle source
# File lib/chef-dk/pager.rb, line 34
def ui
  @ui ||=
    if pager_enabled?
      UI.new(out: parent_stdout)
    else
      UI.new
    end
end
wait() click to toggle source
# File lib/chef-dk/pager.rb, line 64
def wait
  return false unless pager_enabled?

  # Sends EOF to the PAGER
  parent_stdout.close
  # wait or else we'd kill the pager when we exit
  Process.waitpid(pager_pid)
end
with_pager() { |self| ... } click to toggle source
# File lib/chef-dk/pager.rb, line 43
def with_pager
  start
  begin
    yield self
  ensure
    wait
  end
end

Private Instance Methods

child_stdin() click to toggle source
# File lib/chef-dk/pager.rb, line 87
def child_stdin
  pipe[0]
end
pager_env() click to toggle source
# File lib/chef-dk/pager.rb, line 99
def pager_env
  { "LESS" => "-FRX", "LV" => "-c" }
end
parent_stdout() click to toggle source
# File lib/chef-dk/pager.rb, line 91
def parent_stdout
  pipe[1]
end
pipe() click to toggle source
# File lib/chef-dk/pager.rb, line 95
def pipe
  @pipe ||= IO.pipe
end