Class: ChefDK::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/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
29
# File 'lib/chef-dk/pager.rb', line 25

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

Instance Attribute Details

#pager_pidObject (readonly)

Returns the value of attribute pager_pid.



23
24
25
# File 'lib/chef-dk/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



76
77
78
# File 'lib/chef-dk/pager.rb', line 76

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)


82
83
84
# File 'lib/chef-dk/pager.rb', line 82

def have_tty?
  $stdout.tty?
end

#pager_enabled?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/chef-dk/pager.rb', line 31

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

#startObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef-dk/pager.rb', line 53

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



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

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

#waitObject



65
66
67
68
69
70
71
72
# File 'lib/chef-dk/pager.rb', line 65

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



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

def with_pager
  start
  begin
    yield self
  ensure
    wait
  end
end