Skip to content

Commit

Permalink
Moved 1.8 expect.rb to correct location.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Jan 15, 2012
1 parent 23bce5b commit 077f61d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 65 deletions.
41 changes: 12 additions & 29 deletions lib/18/expect.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,35 @@
# This is originally from the pty extension that comes with MRI
# by default.

$expect_verbose = false

class IO
# Reads ios until pattern matches or the timeout is over. It returns
# an array with the read buffer, followed by the matches. If a block is given,
# the result is yielded to the block and returns nil. The optional timeout parameter defines,
# in seconds, the total time to wait for pattern. If it is over of eof is found, it
# returns/yields nil. However, the buffer in a timeout session is kept for the next expect call.
# The default timeout is 9999999 seconds.
def expect(pat,timeout=9999999)
buf = ''
case pat
when String
pat = Regexp.new Regexp.quote(pat)
e_pat = Regexp.new(Regexp.quote(pat))
when Regexp
# Nothing, we're good.
else
raise ArgumentError, "accepts a String or Regexp only"
e_pat = pat
end

buf = ''

verbose = $expect_verbose

while true
if !IO.select([self], nil, nil, timeout) or eof?
if !IO.select([self],nil,nil,timeout) or eof? then
result = nil
break
end

c = getc.chr
buf << c

if verbose
if $expect_verbose
STDOUT.print c
STDOUT.flush
end

if match = pat.match(buf)
result = [buf, *match.to_a[1..-1]]
if mat=e_pat.match(buf) then
result = [buf,*mat.to_a[1..-1]]
break
end
end

return result unless block_given?

yield result
if block_given? then
yield result
else
return result
end
nil
end
end
Expand Down
36 changes: 0 additions & 36 deletions lib/18/pty/expect.rb

This file was deleted.

0 comments on commit 077f61d

Please sign in to comment.