InternalPipeStream.st
changeset 5141 046c31b9f275
parent 4326 5aca213f1a71
child 5163 bb5f55706bab
--- a/InternalPipeStream.st	Sat Aug 24 19:20:40 2019 +0200
+++ b/InternalPipeStream.st	Sat Aug 24 19:21:37 2019 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2002 by Claus Gittinger
               All Rights Reserved
@@ -90,17 +92,26 @@
     elizasOutput nextPutAll:'>'.
 
     [top realized] whileTrue:[
-        |line answer matchingRule|
+        |line answer matchingRule matches what|
 
-        line := userInput nextLine.
-        ((line isEmptyOrNil and:[userInput atEnd]) or:[ #('quit' 'exit' 'end' 'bye') includes:line ]) ifTrue:[
-            top destroy.
-            ^ self
+        ((userInput readWaitWithTimeout:1) not and:[top realized]) ifTrue:[
+            line := userInput nextLine.
+            ((line isEmptyOrNil and:[userInput atEnd]) or:[ #('quit' 'exit' 'end' 'bye') includes:line ]) ifTrue:[
+                top destroy.
+                ^ self
+            ].
+            (matches := line subExpressionsInRegex:'I am (.+)' caseSensitive:false) size == 1 ifTrue:[
+                answer := #( 
+                            'Why are you %1?'
+                            'What makes you think that you are %1?'
+                            'Who told you, that you are %1?'
+                          ) atRandom bindWithArguments:matches
+            ] ifFalse:[
+                answer := 'Tell me more.'.
+            ].
+            elizasOutput nextPutLine:answer.
+            elizasOutput nextPutAll:'>'.
         ].
-
-        answer := 'Tell me more.'.
-        elizasOutput nextPutLine:answer.
-        elizasOutput nextPutAll:'>'.
     ].
                                                                 [exEnd]
 "
@@ -170,6 +181,18 @@
     "
 !
 
+readWaitWithTimeoutMs:millis
+    "suspend the current process, until the receiver
+     becomes ready for reading or a timeout (in milliseconds) expired.
+     If data is already available, return immediate.
+     With nil millis, wait forever.
+     Return true if a timeout occurred (i.e. false, if data is available).
+     The other threads are not affected by the wait."
+
+    (closed and:[queue isEmpty]) ifTrue:[^ false].
+    ^ queue readWaitWithTimeoutMs:millis
+!
+
 size
     ^ queue size
 ! !