about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-01-24 21:26:26 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-01-24 21:26:26 -0800
commit3a0be565881da3da34b2b5cd1bb2b7c6a88a4910 (patch)
treed77ab8b6bcf9786ca6b9901ed4131870dd014f72
parentcbb2018f9c79a8601823a3daca378a206c0aab67 (diff)
downloadmu-3a0be565881da3da34b2b5cd1bb2b7c6a88a4910.tar.gz
605 - example program for line-oriented input
We need the hack of printing characters typed to screen right after we
see them. But only when it's the real screen. And there's no way to
write a test for that because it explicitly shouldn't happen for fake
terminals :(

Never mind, we'll be able to test it when we provide some mechanism for
suppressing print. The equivalent of 'stty -echo'.
-rw-r--r--buffered-stdin.mu27
-rw-r--r--mu.arc17
-rw-r--r--stdin.mu3
3 files changed, 45 insertions, 2 deletions
diff --git a/buffered-stdin.mu b/buffered-stdin.mu
new file mode 100644
index 00000000..167b268b
--- /dev/null
+++ b/buffered-stdin.mu
@@ -0,0 +1,27 @@
+; reads lines, prints when you hit 'enter'
+; dies if you wait a while, because so far we never free memory
+(function main [
+  (default-space:space-address <- new space:literal 30:literal)
+  (cursor-mode)
+  (clear-screen)
+  ; hook up stdin
+  (stdin:channel-address <- init-channel 1:literal)
+  (fork-helper send-keys-to-stdin:fn nil:literal/globals nil:literal/limit nil:literal/keyboard stdin:channel-address)
+  (buffered-stdin:channel-address <- init-channel 1:literal)
+  ; buffer stdin
+  (fork-helper buffer-stdin:fn nil:literal/globals nil:literal/limit stdin:channel-address buffered-stdin:channel-address)
+  ; now read characters from the buffer until a 'enter' is typed
+  (s:string-address <- new "? ")
+  (print-string nil:literal/terminal s:string-address)
+  { begin
+    (x:tagged-value stdin:channel-address/deref <- read buffered-stdin:channel-address)
+    (c:character <- maybe-coerce x:tagged-value character:literal)
+;?     (print-primitive-to-host (("AAA " literal))) ;? 0
+;?     (print-primitive-to-host c:character) ;? 0
+;?     (print-primitive-to-host (("\n" literal))) ;? 0
+    (done?:boolean <- equal c:character ((#\newline literal)))
+    (break-if done?:boolean)
+    (print-character nil:literal/terminal c:character)
+    (loop)
+  }
+])
diff --git a/mu.arc b/mu.arc
index 705bbfe5..986c39a0 100644
--- a/mu.arc
+++ b/mu.arc
@@ -1968,6 +1968,19 @@
   ; real keyboard input is infrequent; avoid polling it too much
   (sleep for-some-cycles:literal 1:literal)
   (c:character <- read-key-from-host)
+  ; when we read from a real keyboard we print to screen as well
+  ; later we'll need ways to suppress this
+  ; exceptions for the charterm library
+  { begin
+    (newline?:boolean <- equal c:character ((return literal)))
+    (break-unless newline?:character)
+    (cursor-to-next-line)
+    (reply ((#\newline literal)))
+  }
+  { begin
+    (break-unless c:character)
+    (print-primitive-to-host c:character)
+  }
   (reply c:character)
 )
 
@@ -2001,7 +2014,7 @@
       (line:buffer-address <- append line:buffer-address c:character)
       (line-contents:string-address <- get line:buffer-address/deref data:offset)
 ;?       (print-primitive-to-host c:character) ;? 0
-;?       (print-primitive-to-host (("\n" literal))) ;? 0
+;?       (print-primitive-to-host (("\n" literal))) ;? 2
       (line-done?:boolean <- equal c:character ((#\newline literal)))
       (break-if line-done?:boolean)
       (eof?:boolean <- equal c:character ((#\null literal)))
@@ -2305,7 +2318,7 @@
 
 ;; load all provided files and start at 'main'
 (reset)
-;? (new-trace "main")
+;? (new-trace "main") ;? 3
 (awhen (pos "--" argv)
   (map add-code:readfile (cut argv (+ it 1)))
 ;?   (= dump-trace* (obj whitelist '("run")))
diff --git a/stdin.mu b/stdin.mu
index a651ce1b..088ec1b0 100644
--- a/stdin.mu
+++ b/stdin.mu
@@ -1,3 +1,6 @@
+; reads and prints keys until you hit 'q'
+; no need to hit 'enter', and 'enter' has no special meaning
+; dies if you wait a while, because so far we never free memory
 (function main [
   (default-space:space-address <- new space:literal 30:literal)
   (cursor-mode)