about summary refs log tree commit diff stats
path: root/516read-line.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-10-13 23:14:20 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-10-13 23:14:20 -0700
commitdb0363462f42b05561853bd295fb4d39909a9f6c (patch)
treefd73bad30978e911ad2905700ef65103b444bbce /516read-line.mu
parentd280bd8dac5555a6dd8fdc34b8e423f120f05887 (diff)
downloadmu-db0363462f42b05561853bd295fb4d39909a9f6c.tar.gz
primitive: read line from keyboard
Blocking.
Diffstat (limited to '516read-line.mu')
-rw-r--r--516read-line.mu20
1 files changed, 20 insertions, 0 deletions
diff --git a/516read-line.mu b/516read-line.mu
new file mode 100644
index 00000000..1bb79c5a
--- /dev/null
+++ b/516read-line.mu
@@ -0,0 +1,20 @@
+# read line from keyboard into stream while also echoing to screen
+# abort on stream overflow
+fn read-line-from-keyboard keyboard: (addr keyboard), out: (addr stream byte), screen: (addr screen), fg: int, bg: int {
+  clear-stream out
+  {
+    draw-cursor screen, 0x20/space
+    var key/eax: byte <- read-key keyboard
+    compare key, 0xa/newline
+    break-if-=
+    compare key, 0
+    loop-if-=
+    var key2/eax: int <- copy key
+    append-byte out, key2
+    var c/eax: code-point <- copy key2
+    draw-code-point-at-cursor-over-full-screen screen, c, fg bg
+    loop
+  }
+  # clear cursor
+  draw-code-point-at-cursor-over-full-screen screen, 0x20/space, fg bg
+}