Class: ChefCLI::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enable_pager: true) ⇒ Pager

Returns a new instance of Pager.



25
26
27
28
# File 'lib/chef-cli/pager.rb', line 25

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

Instance Attribute Details

#pager_pidObject (readonly)

Returns the value of attribute pager_pid.



23
24
25
# File 'lib/chef-cli/pager.rb', line 23

def pager_pid
  @pager_pid
end

Instance Method Details

#envObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is just public so we can stub it for testing



75
76
77
# File 'lib/chef-cli/pager.rb', line 75

def env
  ENV
end

#have_tty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is just public so we can stub it for testing

Returns:

  • (Boolean)


81
82
83
# File 'lib/chef-cli/pager.rb', line 81

def have_tty?
  $stdout.tty?
end

#pager_enabled?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/chef-cli/pager.rb', line 30

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

#startObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef-cli/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

#uiObject



34
35
36
37
38
39
40
41
# File 'lib/chef-cli/pager.rb', line 34

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

#waitObject



64
65
66
67
68
69
70
71
# File 'lib/chef-cli/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_pagerObject



43
44
45
46
47
48
49
50
# File 'lib/chef-cli/pager.rb', line 43

def with_pager
  start
  begin
    yield self
  ensure
    wait
  end
end