Sha256: 4a1a786010df1560af79629d29c15666aca9172069bd5b17e67a3f7b8c19fecb

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

require "pty"

require_relative "kommando/error"
require_relative "kommando/version"
require_relative "kommando/buffer"

class Kommando

  def initialize(cmd, opts={})
    @cmd = cmd
    @stdout = Buffer.new

    @output = opts[:output] == true
  end

  def run
    command, *args = @cmd.split " "
    begin
      PTY.spawn(command, *args) do |stdout, stdin, pid|
        Thread.abort_on_exception = true

        thread_stdout = Thread.new do
          while true do
            break if stdout.eof?

            c = stdout.getc
            @stdout.append c if c
            print c if @output
          end
        end
        thread_stdout.join

        # http://stackoverflow.com/a/7263243
        Process.wait(pid)

        @code = $?.exitstatus
      end
    rescue => ex
      raise Kommando::Error, "Command '#{command}' not found"
    end

    true
  end

  def out
    @stdout.to_s
  end

  def code
    @code
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kommando-0.0.3 lib/kommando.rb