Class: Termup::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/termup/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcess

Returns a new instance of Process.



5
6
7
# File 'lib/termup/process.rb', line 5

def initialize
  lookup_pid
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



3
4
5
# File 'lib/termup/process.rb', line 3

def pid
  @pid
end

Instance Method Details

#app_nameObject



17
18
19
# File 'lib/termup/process.rb', line 17

def app_name
  @app_name ||= ExecJS.eval "Application(#{@pid}).name()"
end

#iterm?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/termup/process.rb', line 13

def iterm?
  app_name == 'iTerm'
end

#lookup_pidObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/termup/process.rb', line 21

def lookup_pid
  pid = ::Process.ppid
  pids = []

  # Go up the process tree to find term-like process
  while pid > 1 do
    pids << pid if term_like_pids.include?(pid)
    pid = `ps -p #{pid} -o ppid=`.strip.to_i
  end

  abort 'terminal pid not found!' if pids.empty?

  @pid = pids.last
end

#term_like_pidsObject



36
37
38
# File 'lib/termup/process.rb', line 36

def term_like_pids
  @term_like_pids ||= `ps x | grep Term`.split("\n").reject{|i| i =~ /grep/ }.map{|i| i.match(/\d+/).to_s.to_i }
end

#terminal?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/termup/process.rb', line 9

def terminal?
  app_name == 'Terminal'
end