tty-process-ctl

This gem was created to enable control of interactive terminal applications. It is using pseudo TTY to communicate with the process via simple API.

For more advanced gem see also: github.com/avdi/greenletters

Usage

You can install the gem with:

gem install tty-process-ctl

In your code require the gem with:

require 'tty-process-ctl'

Reading program output

Here we run ls command and iterate its output:

TTYProcessCtl.new('ls').each do |line|
      puts line
end

Result:

Gemfile      LICENSE.txt  Rakefile     lib
Gemfile.lock README.rdoc  examples     spec

Sending commands

This example show how to send command to irb process. Output can be skipped with wait_until and iterated until pattern matches with each_until:

irb = TTYProcessCtl.new('irb')

# send command
irb.send_command('2 + 2')

# wait until prompt line was printed
irb.wait_until(/:001 >/)

# print all output lines until we get to the result line (including)
irb.each_until(/=>/) do |line|
      puts line
end

# ask irb to quit
irb.send_command('quit')

# wait irb to exit
irb.wait_exit

Result:

=> 4

Timeout support

Normally each and wait methods will wait infinitely for event to happen. They accept options hash as additional argument where :timeout key value will be respected as number of seconds after which the method will rise TTYProcessCtl::Timeout exception if awaited event did not happen:

TTYProcessCtl.new('sleep 10').wait_until(/Done/, timeout: 1) => TTYProcessCtl::Timeout

Chaining

All each, wait and flush methods can be chained:

TTYProcessCtl.new('echo "abc\ndef\nghi"').wait_until(/def/).each do |line|
      puts line
end

Result:

ghi

Contributing to tty-process-ctl

Copyright © 2012 Jakub Pastuszek. See LICENSE.txt for further details.