| 1 |
/* |
| 2 |
* input-with-timeout test |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <tads.h> |
| 6 |
|
| 7 |
main(args) |
| 8 |
{ |
| 9 |
local cnt; |
| 10 |
|
| 11 |
/* show our introductory text */ |
| 12 |
"Welcome to the timeout tester! Type QUIT to end the program.\b"; |
| 13 |
|
| 14 |
/* no timeouts so far */ |
| 15 |
cnt = 0; |
| 16 |
|
| 17 |
/* keep going until they enter 'quit' */ |
| 18 |
for (;;) |
| 19 |
{ |
| 20 |
local evt; |
| 21 |
local endTime; |
| 22 |
|
| 23 |
/* calculate the maximum time to go without adding any comments */ |
| 24 |
endTime = getTime(GetTimeTicks) + 10000; |
| 25 |
|
| 26 |
/* show our prompt */ |
| 27 |
"-> "; |
| 28 |
|
| 29 |
/* |
| 30 |
* get input until we reach the user enters the line of text, or |
| 31 |
* we reach the ending time |
| 32 |
*/ |
| 33 |
readLoop: |
| 34 |
while (getTime(GetTimeTicks) < endTime) |
| 35 |
{ |
| 36 |
/* get input, with a one-second timeout */ |
| 37 |
evt = inputLineTimeout(500); |
| 38 |
|
| 39 |
/* check what we have */ |
| 40 |
switch (evt[1]) |
| 41 |
{ |
| 42 |
case InEvtEof: |
| 43 |
/* error reading input - abort */ |
| 44 |
"\bError reading input!\b"; |
| 45 |
return; |
| 46 |
|
| 47 |
case InEvtTimeout: |
| 48 |
/* count the timeout, but just keep looping */ |
| 49 |
++cnt; |
| 50 |
break; |
| 51 |
|
| 52 |
case InEvtLine: |
| 53 |
/* we got a line of input - we're done */ |
| 54 |
break readLoop; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/* check why we stopped looping */ |
| 59 |
if (evt[1] == InEvtTimeout) |
| 60 |
{ |
| 61 |
/* |
| 62 |
* We timed out. Show a comment about it, and then go back |
| 63 |
* for more. |
| 64 |
*/ |
| 65 |
inputLineCancel(nil); |
| 66 |
"\bSo far it looks like you typed \"<<evt[2]>>\". |
| 67 |
No hurry; just keep typing as long as you'd like.\b"; |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
/* if they typed 'quit', we're done */ |
| 72 |
if (evt[2].toLower() == 'quit') |
| 73 |
{ |
| 74 |
"Done!\b"; |
| 75 |
break; |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* We read the input. Mention how many times we timed out |
| 80 |
* while they were typing. |
| 81 |
*/ |
| 82 |
"Thanks for the input. While you were typing, we processed |
| 83 |
a total of <<cnt>> timeout interruptions. Your input |
| 84 |
text was \"<<evt[2]>>\".\b"; |
| 85 |
|
| 86 |
/* reset the counter */ |
| 87 |
cnt = 0; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|