Sha256: 96e3eea142ef5f1a0ea0c805c7737b58b406ed44cf6aa82029bf94d730b0bedc

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

#!/usr/bin/env ruby
require 'pty'
require 'open3'
require 'readline'

def run_as_tty(bin, args)
  STDOUT.sync = true
  status = PTY.spawn(bin, *args) do |stdout, stdin, pid|
    stdout.sync = true
    Thread.new do
      loop do
        STDOUT.print stdout.getc
      end
    end
    Thread.new do
      loop do
        input = Readline.readline("", true)
        if input.nil?
          stdout.flush
          stdout.close
          stdin.close
        end
        stdin.puts input.strip
      end
    end
    begin
      Process::waitpid(pid) rescue nil
    rescue SystemExit, Interrupt
      Process.kill('INT', pid)
      Process::waitpid(pid) rescue nil
      retry
    rescue EOFError
    rescue IOError, Errno::EIO
    end
  end
  status
end

def run_as_daemon(bin, args)
  process = nil
  Open3.popen3(bin, *ARGV) do |stdin, stdout, stderr, thread|
    Thread.new do
      while !stdin.closed? do
        input = Readline.readline("", true).strip
        stdin.puts input
      end
    end

    errThread = Thread.new do
      while !stderr.eof?  do
        putc stderr.readchar
      end
    end

    outThread = Thread.new do
      while !stdout.eof?  do
        putc stdout.readchar
      end
    end

    begin
      Process::waitpid(thread.pid) rescue nil
      errThread.join
      outThread.join
      process = thread.value
    rescue SystemExit, Interrupt
      retry
    end
  end
  return process.exitstatus if process
end

bin = "#{Gem.bin_path('voltos', 'voltos')}-cli"
if STDIN.tty?
  status = run_as_tty(bin, ARGV)
else
  status = run_as_daemon(bin, ARGV)
end
exit(status.to_i)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voltos-0.3.0.rc13 exe/voltos