Class: ChefDK::Pager
- Inherits:
-
Object
- Object
- ChefDK::Pager
- Defined in:
- lib/chef-dk/pager.rb
Instance Attribute Summary collapse
-
#pager_pid ⇒ Object
readonly
Returns the value of attribute pager_pid.
Instance Method Summary collapse
-
#env ⇒ Object
private
This is just public so we can stub it for testing.
-
#have_tty? ⇒ Boolean
private
This is just public so we can stub it for testing.
-
#initialize(enable_pager: true) ⇒ Pager
constructor
A new instance of Pager.
- #pager_enabled? ⇒ Boolean
- #start ⇒ Object
- #ui ⇒ Object
- #wait ⇒ Object
- #with_pager ⇒ Object
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_pid ⇒ Object (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
#env ⇒ Object
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
82 83 84 |
# File 'lib/chef-dk/pager.rb', line 82 def have_tty? $stdout.tty? end |
#pager_enabled? ⇒ Boolean
31 32 33 |
# File 'lib/chef-dk/pager.rb', line 31 def pager_enabled? !!(@enable_pager && have_tty? && env["PAGER"]) end |
#start ⇒ Object
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 |
#ui ⇒ Object
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 |
#wait ⇒ Object
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_pager ⇒ Object
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 |