class Dtachr::Runner

Public Class Methods

new(args) click to toggle source
# File lib/dtachr.rb, line 31
def initialize(args)
  @opts = Docopt::docopt(Dtachr::DOC, {
    :argv => args,
    :version => Dtachr::VERSION
  })
  @socket = @opts['--socket'] || gen_socket
  @command = @opts['<parts>'].join(' ')
  @message = @opts.fetch('--message', nil)
  @message ||= default_message
rescue Docopt::Exit => e
  puts e.message
end

Public Instance Methods

call() click to toggle source
# File lib/dtachr.rb, line 44
def call
  return unless @opts
  execute("dtach -n #{@socket} #{@command}")
  @socket_abs = File.absolute_path(@socket)
  pid = fork do
    require 'socket'
    sock = UNIXSocket.new(@socket_abs)
    begin
      sock.recv_io
    rescue SocketError => err
      execute(notify_command)
      Process.exit(0)
    end
  end
  Process.detach(pid)
end

Private Instance Methods

default_message() click to toggle source
# File lib/dtachr.rb, line 63
def default_message
  "'#{@command}' finished."
end
execute(command) click to toggle source
# File lib/dtachr.rb, line 67
def execute(command)
  `#{command}`
end
gen_socket() click to toggle source
# File lib/dtachr.rb, line 71
def gen_socket
  candidates = [('a'..'z'), ('A'..'Z'), (0..9)].map { |i| i.to_a }.flatten
  name = (0...32).map { candidates[rand(candidates.length)] }.join
  if (File.exists?(name)) then gen_socket else name end
end
notify_command() click to toggle source
# File lib/dtachr.rb, line 77
def notify_command
  notifier = "terminal-notifier"
  message = "-message \"#{@message}\""
  title = @opts['--title'].dup if !@opts['--title'].nil?
  title.insert(0, '-title ') if !title.nil? && title.length > 0
  [notifier, title, message].join(' ')
end