mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Add multipul patterns function tio.expects() to Lua API
Add tio.expects() which has a table of patterns in the arguments to
support "OR" pattern waiting.
The return values of tio.expects() are:
1st: index of the matched pattern in the table. nil if unmatched.
2nd: a table of captures if one of the patterns are matched.
nil if any of the patterns are unmatched and timeout.
3rd: all received strings in tio.expects() to treat the strings after
match.
Add all received string to tio.expect()'s return values too.
For example,
idx, captures, all_received = tio.expects( {"\nOK", "\nERROR"}, 1000 )
captures, all_received = tio.expect( "\nPROMPT>", 1000 )
This commit is contained in:
parent
b5944275d3
commit
56c378f5f9
1 changed files with 24 additions and 6 deletions
26
src/script.c
26
src/script.c
|
|
@ -60,13 +60,31 @@ static char script_init[] =
|
|||
" local str = ''\n"
|
||||
" while true do\n"
|
||||
" local c = tio.read(1, timeout)\n"
|
||||
" if c then\n"
|
||||
" if c == nil then\n"
|
||||
" return nil, str\n"
|
||||
" end\n"
|
||||
" str = str .. c\n"
|
||||
" if string.match(str, pattern) then\n"
|
||||
" return string.match(str, pattern)\n"
|
||||
" return string.match(str, pattern), str\n"
|
||||
" end\n"
|
||||
" end\n"
|
||||
"end\n"
|
||||
"tio.expects = function(patterns, timeout)\n"
|
||||
" local str = ''\n"
|
||||
" if type(patterns) ~= 'table' then\n"
|
||||
" patterns = { patterns }\n"
|
||||
" end\n"
|
||||
" while true do\n"
|
||||
" local c = tio.read(1, timeout)\n"
|
||||
" if c == nil then\n"
|
||||
" return nil, nil, str\n"
|
||||
" end\n"
|
||||
" str = str .. c\n"
|
||||
" for idx, pat in ipairs(patterns) do\n"
|
||||
" local captured = { string.match(str, pat) }\n"
|
||||
" if #captured > 0 then\n"
|
||||
" return idx, captured, str\n"
|
||||
" end\n"
|
||||
" else\n"
|
||||
" return nil, str\n"
|
||||
" end\n"
|
||||
" end\n"
|
||||
"end\n"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue