Module: PseudoTerminal::Client
- Included in:
- PseudoTerminal
- Defined in:
- lib/pseudo-terminal/client.rb
Instance Attribute Summary collapse
-
#b ⇒ Object
Returns the value of attribute b.
-
#r ⇒ Object
Returns the value of attribute r.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#w ⇒ Object
Returns the value of attribute w.
Instance Method Summary collapse
- #<<(command, &block) ⇒ Object
- #initialize(opt = {}) ⇒ Object
- #is_running? ⇒ Boolean
- #kill! ⇒ Object
- #put(command, &block) ⇒ Object
- #read(&block) ⇒ Object
Instance Attribute Details
#b ⇒ Object
Returns the value of attribute b.
5 6 7 |
# File 'lib/pseudo-terminal/client.rb', line 5 def b @b end |
#r ⇒ Object
Returns the value of attribute r.
5 6 7 |
# File 'lib/pseudo-terminal/client.rb', line 5 def r @r end |
#timeout ⇒ Object
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/pseudo-terminal/client.rb', line 5 def timeout @timeout end |
#w ⇒ Object
Returns the value of attribute w.
5 6 7 |
# File 'lib/pseudo-terminal/client.rb', line 5 def w @w end |
Instance Method Details
#<<(command, &block) ⇒ Object
18 19 20 |
# File 'lib/pseudo-terminal/client.rb', line 18 def << command, &block put command, &block end |
#initialize(opt = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pseudo-terminal/client.rb', line 7 def initialize opt={} opt[:ready] ||= '> ' opt[:sh] ||= "env PS1='#{opt[:ready]}' TERM=dumb sh -i" opt[:timeout] ||= 1 @timeout = opt[:timeout] @r, @w, @pid = PTY.spawn opt[:sh] @b = PseudoTerminal::Buffer.new opt[:ready] read @b.masks << @b.lines.pop while @b.lines.empty? == false end |
#is_running? ⇒ Boolean
27 28 29 30 31 32 33 34 |
# File 'lib/pseudo-terminal/client.rb', line 27 def is_running? begin Process.getpgid @pid true rescue Errno::ESRCH false end end |
#kill! ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/pseudo-terminal/client.rb', line 36 def kill! @r.close @w.close begin Process.wait @pid rescue PTY::ChildExited end end |
#put(command, &block) ⇒ Object
22 23 24 25 |
# File 'lib/pseudo-terminal/client.rb', line 22 def put command, &block @w.puts command read &block end |
#read(&block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pseudo-terminal/client.rb', line 45 def read &block begin read_loop &block rescue IO::WaitReadable t = IO.select([@r], nil, nil, @timeout) retry unless t.nil? @b.lines rescue IOError nil end end |