Sha256: 8d2d785d4f58e2f3eaf64a3233df191c637c14a791571a3a09c0b52715e9eebb

Contents?: true

Size: 1.07 KB

Versions: 34

Compression:

Stored size: 1.07 KB

Contents

require 'thread'

class Shell

  PROMPT = "_-=READY=-_"

  def initialize(shell = "/bin/bash", delay = 1, &block)
    @cb = block
    @shell = shell
    @delay = delay
    @parent_to_child_read, @parent_to_child_write = IO.pipe
    @child_to_parent_read, @child_to_parent_write = IO.pipe
    @child_pid = fork do
      @parent_to_child_write.close
      @child_to_parent_read.close
      $stdin.reopen(@parent_to_child_read)
      $stdout.reopen(@child_to_parent_write)
      $stderr.reopen(@child_to_parent_write)
      exec("/bin/bash")
    end
    buffer = ""
    semaphore = Mutex.new
    Thread.new do
      while true
        c = @child_to_parent_read.read(1)
        semaphore.synchronize { buffer += c }
      end
    end
    Thread.new do
      ch = ""
      while true do
        sleep @delay
        semaphore.synchronize {
          if buffer == ch and ch != ""
            @cb.call buffer
            buffer = ""
          end
          ch = buffer
        }
      end
    end
  end

  def puts(str)
    @parent_to_child_write.puts(str)
  end

  def kill
    @child_pid.kill
  end
end

Version data entries

34 entries across 34 versions & 13 rubygems

Version Path
brontes3d-xmpp4r-0.4 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
bryanl-xmpp4r-0.3.2 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
edavey-xmpp4r-0.4.1 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
edavey-xmpp4r-0.4.2 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
edavey-xmpp4r-0.4 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
heipei-xmpp4r-0.3.2 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
ln-xmpp4r-0.5 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
mojodna-xmpp4r-0.4.0.2 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
mojodna-xmpp4r-0.4.0.3 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
seanohalpin-xmpp4r-0.4.1 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
xmpp4r-0.5.6 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
xmpp4r-0.5.5 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
gmcmillan-xmpp4r-0.6.2 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
gmcmillan-xmpp4r-0.6.1 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
gmcmillan-xmpp4r-0.6 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
gmcmillan-xmpp4r-0.5 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
mad-p-xmpp4r-0.6.3 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
mad-p-xmpp4r-0.6.2 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
mad-p-xmpp4r-0.6.1 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
mad-p-xmpp4r-0.6.0 data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb