Sha256: f98129745002c6e403f9ffe2469336ab5c1928f4dbbbf9207a8100988602af08

Contents?: true

Size: 790 Bytes

Versions: 2

Compression:

Stored size: 790 Bytes

Contents

module Termup
  class Process
    attr_reader :pid

    def initialize
      lookup_pid
    end

    def terminal?
      app_name == 'Terminal'
    end

    def iterm?
      app_name == 'iTerm'
    end

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

    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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
termup-3.0.1 lib/termup/process.rb
termup-3.0.0 lib/termup/process.rb