7793 - baremetal/shell: menu

This commit is contained in:
Kartik K. Agaram 2021-02-22 23:59:35 -08:00
parent 9a96662dff
commit 2812e20608
4 changed files with 96 additions and 51 deletions

View File

@ -130,12 +130,12 @@ fn draw-text-rightward-over-full-screen screen: (addr screen), text: (addr array
return result return result
} }
fn draw-text-rightward-from-cursor screen: (addr screen), text: (addr array byte), xmax: int, color: int, background-color: int -> _/eax: int { fn draw-text-rightward-from-cursor screen: (addr screen), text: (addr array byte), xmax: int, color: int, background-color: int {
var cursor-x/eax: int <- copy 0 var cursor-x/eax: int <- copy 0
var cursor-y/ecx: int <- copy 0 var cursor-y/ecx: int <- copy 0
cursor-x, cursor-y <- cursor-position screen cursor-x, cursor-y <- cursor-position screen
var result/eax: int <- draw-text-rightward screen, text, cursor-x, xmax, cursor-y, color, background-color cursor-x <- draw-text-rightward screen, text, cursor-x, xmax, cursor-y, color, background-color
return result set-cursor-position screen, cursor-x, cursor-y
} }
fn render-grapheme screen: (addr screen), g: grapheme, xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { fn render-grapheme screen: (addr screen), g: grapheme, xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int {

View File

@ -80,12 +80,12 @@ fn emit-stack-from-top _self: (addr grapheme-stack), out: (addr stream byte) {
# We implicitly render everything editable in a single color, and assume the # We implicitly render everything editable in a single color, and assume the
# cursor is a single other color. # cursor is a single other color.
fn render-gap-buffer-wrapping-right-then-down screen: (addr screen), _gap: (addr gap-buffer), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, render-cursor?: boolean -> _/eax: int, _/ecx: int { fn render-gap-buffer-wrapping-right-then-down screen: (addr screen), _gap: (addr gap-buffer), xmin: int, ymin: int, xmax: int, ymax: int, render-cursor?: boolean -> _/eax: int, _/ecx: int {
var gap/esi: (addr gap-buffer) <- copy _gap var gap/esi: (addr gap-buffer) <- copy _gap
var left/edx: (addr grapheme-stack) <- get gap, left var left/edx: (addr grapheme-stack) <- get gap, left
var x2/eax: int <- copy 0 var x2/eax: int <- copy 0
var y2/ecx: int <- copy 0 var y2/ecx: int <- copy 0
x2, y2 <- render-stack-from-bottom-wrapping-right-then-down screen, left, xmin, ymin, xmax, ymax, x, y x2, y2 <- render-stack-from-bottom-wrapping-right-then-down screen, left, xmin, ymin, xmax, ymax, xmin, ymin
var right/edx: (addr grapheme-stack) <- get gap, right var right/edx: (addr grapheme-stack) <- get gap, right
x2, y2 <- render-stack-from-top-wrapping-right-then-down screen, right, xmin, ymin, xmax, ymax, x2, y2, render-cursor? x2, y2 <- render-stack-from-top-wrapping-right-then-down screen, right, xmin, ymin, xmax, ymax, x2, y2, render-cursor?
# decide whether we still need to print a cursor # decide whether we still need to print a cursor
@ -113,7 +113,7 @@ fn render-gap-buffer screen: (addr screen), gap: (addr gap-buffer), x: int, y: i
var height/ebx: int <- copy _height var height/ebx: int <- copy _height
var x2/eax: int <- copy 0 var x2/eax: int <- copy 0
var y2/ecx: int <- copy 0 var y2/ecx: int <- copy 0
x2, y2 <- render-gap-buffer-wrapping-right-then-down screen, gap, x, y, width, height, x, y, render-cursor? x2, y2 <- render-gap-buffer-wrapping-right-then-down screen, gap, x, y, width, height, render-cursor?
return x2 # y2? yolo return x2 # y2? yolo
} }

View File

@ -5,8 +5,11 @@ fn main {
var sandbox-storage: sandbox var sandbox-storage: sandbox
var sandbox/esi: (addr sandbox) <- address sandbox-storage var sandbox/esi: (addr sandbox) <- address sandbox-storage
initialize-sandbox sandbox initialize-sandbox sandbox
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size 0/screen
{ {
render-sandbox 0/screen, sandbox, 2/x, 2/y render-sandbox 0/screen, sandbox, 2/x, 2/y, width, height
{ {
var key/eax: byte <- read-key 0/keyboard var key/eax: byte <- read-key 0/keyboard
compare key, 0 compare key, 0

View File

@ -38,15 +38,15 @@ fn allocate-sandbox-with _out: (addr handle sandbox), s: (addr array byte) {
## ##
fn render-sandbox screen: (addr screen), _self: (addr sandbox), _x: int, _y: int { fn render-sandbox screen: (addr screen), _self: (addr sandbox), xmin: int, ymin: int, xmax: int, ymax: int {
clear-screen screen clear-screen screen
var self/esi: (addr sandbox) <- copy _self var self/esi: (addr sandbox) <- copy _self
# data # data
var data-ah/eax: (addr handle gap-buffer) <- get self, data var data-ah/eax: (addr handle gap-buffer) <- get self, data
var _data/eax: (addr gap-buffer) <- lookup *data-ah var _data/eax: (addr gap-buffer) <- lookup *data-ah
var data/edx: (addr gap-buffer) <- copy _data var data/edx: (addr gap-buffer) <- copy _data
var x/eax: int <- copy _x var x/eax: int <- copy xmin
var y/ecx: int <- copy _y var y/ecx: int <- copy ymin
var cursor-in-sandbox?/ebx: boolean <- copy 0/false var cursor-in-sandbox?/ebx: boolean <- copy 0/false
{ {
var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace? var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
@ -54,28 +54,70 @@ fn render-sandbox screen: (addr screen), _self: (addr sandbox), _x: int, _y: int
break-if-!= break-if-!=
cursor-in-sandbox? <- copy 1/true cursor-in-sandbox? <- copy 1/true
} }
x, y <- render-gap-buffer-wrapping-right-then-down screen, data, x, y, 0x20/xmax, 0x20/ymax, x, y, cursor-in-sandbox? x, y <- render-gap-buffer-wrapping-right-then-down screen, data, x, y, xmax, ymax, cursor-in-sandbox?
y <- increment y <- increment
# trace # trace
var trace-ah/eax: (addr handle trace) <- get self, trace var trace-ah/eax: (addr handle trace) <- get self, trace
var _trace/eax: (addr trace) <- lookup *trace-ah var _trace/eax: (addr trace) <- lookup *trace-ah
var trace/edx: (addr trace) <- copy _trace var trace/edx: (addr trace) <- copy _trace
var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace? var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
y <- render-trace screen, trace, _x, y, 0x20/xmax, 0x20/ymax, *cursor-in-trace? y <- render-trace screen, trace, xmin, y, xmax, ymax, *cursor-in-trace?
# value # value
var value-ah/eax: (addr handle stream byte) <- get self, value $render-sandbox:value: {
var _value/eax: (addr stream byte) <- lookup *value-ah var value-ah/eax: (addr handle stream byte) <- get self, value
var value/esi: (addr stream byte) <- copy _value var _value/eax: (addr stream byte) <- lookup *value-ah
var done?/eax: boolean <- stream-empty? value var value/esi: (addr stream byte) <- copy _value
compare done?, 0/false var done?/eax: boolean <- stream-empty? value
compare done?, 0/false
break-if-!=
var x/eax: int <- copy 0
x, y <- draw-text-wrapping-right-then-down screen, "=> ", xmin, y, xmax, ymax, xmin, y, 7/fg, 0/bg
var x2/edx: int <- copy x
var dummy/eax: int <- draw-stream-rightward screen, value, x2, xmax, y, 7/fg=grey, 0/bg
}
# render menu
var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
compare *cursor-in-trace?, 0/false
{ {
break-if-= break-if-=
render-trace-menu screen
return return
} }
var x/eax: int <- copy 0 render-sandbox-menu screen
x, y <- draw-text-wrapping-right-then-down screen, "=> ", _x, y, 0x20/xmax, 0x20/ymax, _x, y, 7/fg, 0/bg }
var x2/edx: int <- copy x
var dummy/eax: int <- draw-stream-rightward screen, value, x2, 0x30/xmax, y, 7/fg=grey, 0/bg fn render-sandbox-menu screen: (addr screen) {
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size screen
var y/ecx: int <- copy height
y <- decrement
set-cursor-position screen, 0/x, y
draw-text-rightward-from-cursor screen, " ctrl-s ", width, 0/fg, 7/bg=grey
draw-text-rightward-from-cursor screen, " run sandbox ", width, 7/fg, 0/bg
draw-text-rightward-from-cursor screen, " ctrl-d ", width, 0/fg, 7/bg=grey
draw-text-rightward-from-cursor screen, " cursor down ", width, 7/fg, 0/bg
draw-text-rightward-from-cursor screen, " ctrl-u ", width, 0/fg, 7/bg=grey
draw-text-rightward-from-cursor screen, " cursor up ", width, 7/fg, 0/bg
draw-text-rightward-from-cursor screen, " tab ", width, 0/fg, 9/bg=blue
draw-text-rightward-from-cursor screen, " move to trace ", width, 7/fg, 0/bg
}
fn render-trace-menu screen: (addr screen) {
var width/eax: int <- copy 0
var height/ecx: int <- copy 0
width, height <- screen-size screen
var y/ecx: int <- copy height
y <- decrement
set-cursor-position screen, 0/x, y
draw-text-rightward-from-cursor screen, " ctrl-s ", width, 0/fg, 7/bg=grey
draw-text-rightward-from-cursor screen, " run sandbox ", width, 7/fg, 0/bg
draw-text-rightward-from-cursor screen, " ctrl-d ", width, 0/fg, 7/bg=grey
draw-text-rightward-from-cursor screen, " cursor down ", width, 7/fg, 0/bg
draw-text-rightward-from-cursor screen, " ctrl-u ", width, 0/fg, 7/bg=grey
draw-text-rightward-from-cursor screen, " cursor up ", width, 7/fg, 0/bg
draw-text-rightward-from-cursor screen, " tab ", width, 0/fg, 3/bg=cyan
draw-text-rightward-from-cursor screen, " move to sandbox ", width, 7/fg, 0/bg
} }
fn edit-sandbox _self: (addr sandbox), key: byte { fn edit-sandbox _self: (addr sandbox), key: byte {
@ -109,28 +151,18 @@ fn edit-sandbox _self: (addr sandbox), key: byte {
# arrow keys # arrow keys
var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace? var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
{ {
compare g, 0x4/ctrl-d compare g, 9/tab
break-if-!= break-if-!=
# ctrl-d: cursor down (into trace if it makes sense) # if cursor in input, switch to trace
# if cursor in input, check if we need to switch to trace
{ {
compare *cursor-in-trace?, 0/false compare *cursor-in-trace?, 0/false
break-if-!= break-if-!=
var data-ah/eax: (addr handle gap-buffer) <- get self, data
var data/eax: (addr gap-buffer) <- lookup *data-ah
var at-bottom?/eax: boolean <- cursor-on-final-line? data
compare at-bottom?, 0/false
break-if-=
var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
copy-to *cursor-in-trace?, 1/true copy-to *cursor-in-trace?, 1/true
return return
} }
} # if cursor in trace, switch to input
{ copy-to *cursor-in-trace?, 0/false
compare g, 0x15/ctrl-u return
break-if-!=
# ctrl-u: cursor up
# if cursor in trace, check if we need to switch to trace
} }
# if cursor in trace, send cursor to trace # if cursor in trace, send cursor to trace
{ {
@ -173,9 +205,9 @@ fn test-run-integer {
# setup: screen # setup: screen
var screen-on-stack: screen var screen-on-stack: screen
var screen/edi: (addr screen) <- address screen-on-stack var screen/edi: (addr screen) <- address screen-on-stack
initialize-screen screen, 0xa, 4 initialize-screen screen, 0x80/width, 0x10/height
# #
render-sandbox screen, sandbox, 0/x, 0/y render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
check-screen-row screen, 0/y, "1 ", "F - test-run-integer/0" check-screen-row screen, 0/y, "1 ", "F - test-run-integer/0"
check-screen-row screen, 1/y, "... ", "F - test-run-integer/1" check-screen-row screen, 1/y, "... ", "F - test-run-integer/1"
check-screen-row screen, 2/y, "=> 1 ", "F - test-run-integer/2" check-screen-row screen, 2/y, "=> 1 ", "F - test-run-integer/2"
@ -193,9 +225,9 @@ fn test-run-error-invalid-integer {
# setup: screen # setup: screen
var screen-on-stack: screen var screen-on-stack: screen
var screen/edi: (addr screen) <- address screen-on-stack var screen/edi: (addr screen) <- address screen-on-stack
initialize-screen screen, 0x10, 4 initialize-screen screen, 0x80/width, 0x10/height
# #
render-sandbox screen, sandbox, 0/x, 0/y render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
check-screen-row screen, 0/y, "1a ", "F - test-run-error-invalid-integer/0" check-screen-row screen, 0/y, "1a ", "F - test-run-error-invalid-integer/0"
check-screen-row screen, 1/y, "... ", "F - test-run-error-invalid-integer/0" check-screen-row screen, 1/y, "... ", "F - test-run-error-invalid-integer/0"
check-screen-row screen, 2/y, "invalid number ", "F - test-run-error-invalid-integer/2" check-screen-row screen, 2/y, "invalid number ", "F - test-run-error-invalid-integer/2"
@ -213,23 +245,33 @@ fn test-run-move-cursor-into-trace {
# setup: screen # setup: screen
var screen-on-stack: screen var screen-on-stack: screen
var screen/edi: (addr screen) <- address screen-on-stack var screen/edi: (addr screen) <- address screen-on-stack
initialize-screen screen, 0x10, 8 initialize-screen screen, 0x80/width, 0x10/height
# #
render-sandbox screen, sandbox, 0/x, 0/y render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
check-screen-row screen, 0/y, "12 ", "F - test-run-move-cursor-into-trace/pre-0" check-screen-row screen, 0/y, "12 ", "F - test-run-move-cursor-into-trace/pre-0"
check-background-color-in-screen-row screen, 7/bg=cursor, 0/y, " | ", "F - test-run-move-cursor-into-trace/pre-0/cursor" check-background-color-in-screen-row screen, 7/bg=cursor, 0/y, " | ", "F - test-run-move-cursor-into-trace/pre-0/cursor"
check-screen-row screen, 1/y, "... ", "F - test-run-move-cursor-into-trace/pre-1" check-screen-row screen, 1/y, "... ", "F - test-run-move-cursor-into-trace/pre-1"
check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, " ", "F - test-run-move-cursor-into-trace/pre-1/cursor" check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, " ", "F - test-run-move-cursor-into-trace/pre-1/cursor"
check-screen-row screen, 2/y, "=> 12 ", "F - test-run-move-cursor-into-trace/pre-2" check-screen-row screen, 2/y, "=> 12 ", "F - test-run-move-cursor-into-trace/pre-2"
check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ", "F - test-run-move-cursor-into-trace/pre-2/cursor" check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ", "F - test-run-move-cursor-into-trace/pre-2/cursor"
# move cursor down # move cursor into trace
edit-sandbox sandbox, 4/ctrl-d edit-sandbox sandbox, 9/tab
# #
render-sandbox screen, sandbox, 0/x, 0/y render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
check-screen-row screen, 0/y, "12 ", "F - test-run-move-cursor-into-trace/0" check-screen-row screen, 0/y, "12 ", "F - test-run-move-cursor-into-trace/trace-0"
check-background-color-in-screen-row screen, 7/bg=cursor, 0/y, " ", "F - test-run-move-cursor-into-trace/0/cursor" check-background-color-in-screen-row screen, 7/bg=cursor, 0/y, " ", "F - test-run-move-cursor-into-trace/trace-0/cursor"
check-screen-row screen, 1/y, "... ", "F - test-run-move-cursor-into-trace/1" check-screen-row screen, 1/y, "... ", "F - test-run-move-cursor-into-trace/trace-1"
check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "||| ", "F - test-run-move-cursor-into-trace/1/cursor" check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "||| ", "F - test-run-move-cursor-into-trace/trace-1/cursor"
check-screen-row screen, 2/y, " ", "F - test-run-move-cursor-into-trace/2" check-screen-row screen, 2/y, " ", "F - test-run-move-cursor-into-trace/trace-2"
check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ", "F - test-run-move-cursor-into-trace/2/cursor" check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ", "F - test-run-move-cursor-into-trace/trace-2/cursor"
# move cursor into input
edit-sandbox sandbox, 9/tab
#
render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
check-screen-row screen, 0/y, "12 ", "F - test-run-move-cursor-into-trace/input-0"
check-background-color-in-screen-row screen, 7/bg=cursor, 0/y, " | ", "F - test-run-move-cursor-into-trace/input-0/cursor"
check-screen-row screen, 1/y, "... ", "F - test-run-move-cursor-into-trace/input-1"
check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, " ", "F - test-run-move-cursor-into-trace/input-1/cursor"
check-screen-row screen, 2/y, " ", "F - test-run-move-cursor-into-trace/input-2"
check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ", "F - test-run-move-cursor-into-trace/input-2/cursor"
} }