class ITermCLI::Terminal::NewSession

Constants

SOURCE

Public Instance Methods

call(command_and_args, options = {}) click to toggle source

command should be [“command”, “arg”] or [“command arg”]

# File lib/iterm_cli/terminal/new_session.rb, line 17
def call(command_and_args, options = {})
  options = {name: nil, debug: false}.merge(options)
  name = options[:name]
  debug = options[:debug]

  command = join_command(command_and_args)
  executable = command.shellsplit.first

  unless which(executable)
    warn "No such file or directory: #{executable}"
    exit 1
  end

  name ||= executable.split("/").last

  script_lines = []
  script_lines.concat(set_env_lines)
  script_lines << "cd #{Dir.pwd.shellescape}"
  script_lines << command

  if debug
    puts script_lines
  end

  script_filename = write_script(script_lines.join("\n"))
  osascript(SOURCE, command: "/bin/sh #{script_filename.shellescape}", name: name)
end

Private Instance Methods

join_command(command_and_args) click to toggle source
# File lib/iterm_cli/terminal/new_session.rb, line 50
def join_command(command_and_args)
  case command_and_args.size
  when 0
    ENV["SHELL"] + " -l"
  when 1
    command_and_args[0]
  else
    command_and_args.shelljoin
  end
end
set_env_lines() click to toggle source
# File lib/iterm_cli/terminal/new_session.rb, line 61
def set_env_lines
  ENV.reject {|k, _v| k == "_" }.map {|kv| "export " + kv.map(&:shellescape).join("=") }
end
which(executable) click to toggle source
# File lib/iterm_cli/terminal/new_session.rb, line 46
def which(executable)
  system("which", executable, out: "/dev/null", err: "/dev/null")
end
write_script(script) click to toggle source
# File lib/iterm_cli/terminal/new_session.rb, line 65
def write_script(script)
  filename = Dir.mktmpdir("iterm-new-session") + "/iterm-new-session.sh"
  File.write(filename, script)
  filename
end