class ChefCLI::Pager

def child_stdin

def child_stdin
  pipe[0]
end

def env

Other tags:
    Api: - private
def env
  ENV
end

def have_tty?

Other tags:
    Api: - private
def have_tty?
  $stdout.tty?
end

def initialize(enable_pager: true)

def initialize(enable_pager: true)
  @enable_pager = enable_pager
  @pipe = nil
end

def pager_enabled?

def pager_enabled?
  !!(@enable_pager && have_tty? && env["PAGER"])
end

def pager_env

def pager_env
  { "LESS" => "-FRX", "LV" => "-c" }
end

def parent_stdout

def parent_stdout
  pipe[1]
end

def pipe

def pipe
  @pipe ||= IO.pipe
end

def start

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

def ui

def ui
  @ui ||=
    if pager_enabled?
      UI.new(out: parent_stdout)
    else
      UI.new
    end
end

def wait

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

def with_pager

def with_pager
  start
  begin
    yield self
  ensure
    wait
  end
end