Class: Ace::Tmux::Molecules::LocalProcessInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/tmux/molecules/local_process_inspector.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_runner: nil) ⇒ LocalProcessInspector

Returns a new instance of LocalProcessInspector.



9
10
11
# File 'lib/ace/tmux/molecules/local_process_inspector.rb', line 9

def initialize(command_runner: nil)
  @command_runner = command_runner || method(:run_command)
end

Instance Method Details

#find_descendant_command(root_pid, allowed_commands:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ace/tmux/molecules/local_process_inspector.rb', line 13

def find_descendant_command(root_pid, allowed_commands:)
  normalized_root = normalize_pid(root_pid)
  allowed = Array(allowed_commands).map { |command| normalize_command(command) }.reject(&:empty?)
  return nil if normalized_root.nil? || allowed.empty?

  queue = [normalized_root]
  visited = {}

  until queue.empty?
    parent_pid = queue.shift
    next if visited[parent_pid]

    visited[parent_pid] = true

    child_processes(parent_pid).each do |child|
      return child[:command] if allowed.include?(child[:command])

      queue << child[:pid]
    end
  end

  nil
end