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
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2012 Jakub Pastuszek. See LICENSE.txt for further details.