about summary refs log tree commit diff stats
path: root/baremetal/shell
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-02-11 19:34:55 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-02-11 19:34:55 -0800
commitd1bcd1421f7a0e87105499a738bae3b3a5c92de9 (patch)
tree550a6070a56dac6ed41f0a8161ee19976ee476c0 /baremetal/shell
parent884c92984fd1341eed3725263434534dd915a795 (diff)
downloadmu-d1bcd1421f7a0e87105499a738bae3b3a5c92de9.tar.gz
7723 - baremetal: rendering string values
Diffstat (limited to 'baremetal/shell')
-rw-r--r--baremetal/shell/value.mu51
1 files changed, 48 insertions, 3 deletions
diff --git a/baremetal/shell/value.mu b/baremetal/shell/value.mu
index 4a14c758..64b1d772 100644
--- a/baremetal/shell/value.mu
+++ b/baremetal/shell/value.mu
@@ -4,9 +4,6 @@ type value {
   number-data: float  # if type = 0
   text-data: (handle array byte)  # if type = 1
   array-data: (handle array value)  # if type = 2
-  file-data: (handle buffered-file)  # if type = 3
-  filename: (handle array byte)  # if type = 3
-  screen-data: (handle screen)  # if type = 4
 }
 
 fn initialize-value-with-integer _self: (addr value), n: int {
@@ -82,3 +79,51 @@ fn test-render-number {
   check-ints-equal new-x, 3, "F - test-render-number: result"
   # we won't bother testing the background colors; lots of flexibility there
 }
+
+fn initialize-value-with-string _self: (addr value), s: (addr array byte) {
+  var self/esi: (addr value) <- copy _self
+  var type/eax: (addr int) <- get self, type
+  copy-to *type, 1/string
+  var dest/eax: (addr handle array byte) <- get self, text-data
+  copy-array-object s, dest
+}
+
+fn render-string screen: (addr screen), _val: (addr array byte), x: int, y: int -> _/eax: int {
+  var val/esi: (addr array byte) <- copy _val
+  compare val, 0
+  {
+    break-if-!=
+    return x
+  }
+  var orig-len/ecx: int <- length val
+  # truncate to 12 graphemes
+  # TODO: more sophisticated interactive rendering
+  var truncated: (handle array byte)
+  var truncated-ah/eax: (addr handle array byte) <- address truncated
+  substring val, 0, 0xc, truncated-ah
+  var _truncated-string/eax: (addr array byte) <- lookup *truncated-ah
+  var truncated-string/edx: (addr array byte) <- copy _truncated-string
+  var len/ebx: int <- length truncated-string
+  draw-code-point screen, 0x22/double-quote, x, y, 7/fg, 0/bg
+  increment x
+  var new-x/eax: int <- draw-text-rightward-over-full-screen screen, truncated-string, x, y, 7/fg, 0/bg
+  compare len, orig-len
+  {
+    break-if-=
+    new-x <- draw-text-rightward-over-full-screen screen, "...", new-x, y, 7/fg, 0/bg
+  }
+  draw-code-point screen, 0x22/double-quote, new-x, y, 7/fg, 0/bg
+  new-x <- increment
+  return new-x
+}
+
+fn test-render-string {
+  # setup: screen
+  var screen-on-stack: screen
+  var screen/edi: (addr screen) <- address screen-on-stack
+  initialize-screen screen, 0x20, 4
+  # strings render with quotes
+  var new-x/eax: int <- render-string screen, "abc", 0/x, 0/y
+  check-screen-row screen, 0/y, "\"abc\"", "F - test-render-string"
+  check-ints-equal new-x, 5, "F - test-render-string: result"
+}