diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-05-31 23:43:49 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-05-31 23:43:49 -0700 |
commit | 9cc69d69c6cd83ed4aa367b4a08ca379cae45805 (patch) | |
tree | b469fd1bf0b3e46258cd835f82da72ee3d26e310 /prototypes/browse/16-screen-state-broken.mu | |
parent | 2f16a74783d830caf2cc01bd67996317195c94c9 (diff) | |
download | mu-9cc69d69c6cd83ed4aa367b4a08ca379cae45805.tar.gz |
6456 - new directory for live-blogging prototypes
I don't have layers yet in Mu. But I can still create lots of versions of a program.
Diffstat (limited to 'prototypes/browse/16-screen-state-broken.mu')
-rw-r--r-- | prototypes/browse/16-screen-state-broken.mu | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/prototypes/browse/16-screen-state-broken.mu b/prototypes/browse/16-screen-state-broken.mu new file mode 100644 index 00000000..d80d8375 --- /dev/null +++ b/prototypes/browse/16-screen-state-broken.mu @@ -0,0 +1,188 @@ +# Incomplete second attempt at parsing headings. +# +# This 'OO' approach seems more scalable. We hoist out all the outer framework +# for deciding when to increment 'col', when to increment 'row' and when to +# start a new page in a whole new part of the screen. Now it gets encapsulated +# into a series of small helpers that can be called from multiple places. +# Objects as coroutines. +# +# In spite of these advances, I need to first wrestle with a parsing issue. +# This text has a heading: +# +# abc *def +# # ghi* +# +# Ugh, so I can't do this translation in a single pass. At the first asterisk +# there's just not enough information to know whether it starts a bold text or +# not. +# +# Then again, maybe I should just keep going and not try to be compatible with +# GitHub-Flavored Markdown. Require that new headings are also new paragraphs. + +fn main args: (addr array (addr array byte)) -> exit-status/ebx: int { + var filename/eax: (addr array byte) <- first-arg args + var file/esi: (addr buffered-file) <- load-file filename + enable-screen-grid-mode + enable-keyboard-immediate-mode + var nrows/eax: int <- copy 0 + var ncols/ecx: int <- copy 0 + nrows, ncols <- screen-size + var screen-position-state-storage: screen-position-state + var screen-position-state: (addr screen-position-state) = address screen-position-state-storage + init-screen-position-state screen-position-state, nrows, ncols + { + render file, screen-position-state + var key/eax: byte <- read-key + compare key, 0x71 # 'q' + loop-if-!= + } + enable-keyboard-type-mode + enable-screen-type-mode + exit-status <- copy 0 +} + +type screen-position-state { + nrows: int # const + ncols: int # const + toprow: int + botrow: int + leftcol: int + rightcol: int + row: int + col: int +} + +fn render in: (addr buffered-file), state: (addr screen-position-state) { + start-drawing state + render-normal in, state +} + +fn render-normal in: (addr buffered-file), state: (addr screen-position-state) { + { + # if done-drawing?(state) break + var done?/eax: boolean <- done-drawing? state + compare done?, 0 + break-if-!= + # + var c/eax: byte <- read-byte-buffered in + # if (c == EOF) break + compare c, 0xffffffff # EOF marker + break-if-= + # if (c == '*') start-bold, render-until-asterisk(in, state), reset + # else if (c == '_') start-bold, render-until-underscore(in, state), reset + # else if (c == '#') compute-color, start color, render-header-line(in, state), reset + # else add-char(state, c) + } +} + +fn render-until-asterisk in: (addr buffered-file), state: (addr screen-position-state) { + { + # if done-drawing?(state) break + var done?/eax: boolean <- done-drawing? state + compare done?, 0 + break-if-!= + # + var c/eax: byte <- read-byte-buffered in + # if (c == EOF) break + compare c, 0xffffffff # EOF marker + break-if-= + # if (c == '*') break + # else add-char(state, c) + } +} + +fn render-until-underscore in: (addr buffered-file), state: (addr screen-position-state) { + { + # if done-drawing?(state) break + var done?/eax: boolean <- done-drawing? state + compare done?, 0 + break-if-!= + # + var c/eax: byte <- read-byte-buffered in + # if (c == EOF) break + compare c, 0xffffffff # EOF marker + break-if-= + # if (c == '_') break + # else add-char(state, c) + } +} + +fn render-header-line in: (addr buffered-file), state: (addr screen-position-state) { + { + # if done-drawing?(state) break + var done?/eax: boolean <- done-drawing? state + compare done?, 0 + break-if-!= + # + var c/eax: byte <- read-byte-buffered in + # if (c == EOF) break + compare c, 0xffffffff # EOF marker + break-if-= + # if (c == '*') break + # else add-char(state, c) + } +} + +fn init-screen-position-state self: (addr screen-position-state), nrows: int, ncols: int { + # hardcoded parameters: + # top-margin + # page-margin + # text-width + var dest/eax: (addr int) <- copy 0 + # self->nrows = nrows + # self->ncols = ncols + # self->toprow = top-margin + # self->botrow = nrows + # self->leftcol = page-margin + # self->rightcol = self->leftcol + text-width + # start-drawing(self) +} + +fn start-drawing self: (addr screen-position-state) { + # self->row = toprow + # self->col = leftcol +} + +fn add-char self: (addr screen-position-state), c: byte { + # print c + # self->col++ + # if (self->col > self->rightcol) next-line(self) +} + +fn next-line self: (addr screen-position-state) { + # self->row++ + # if (self->row > self->botrow) next-page(self) +} + +fn next-page self: (addr screen-position-state) { + # self->leftcol = self->rightcol + 5 + # self->rightcol = self->leftcol + text-width +} + +fn done-drawing? self: (addr screen-position-state) -> result/eax: boolean { + # self->rightcol >= self->ncols +} + +fn first-arg args-on-stack: (addr array (addr array byte)) -> out/eax: (addr array byte) { + var args/eax: (addr array (addr array byte)) <- copy args-on-stack + var result/eax: (addr addr array byte) <- index args, 1 + out <- copy *result +} + +fn load-file filename: (addr array byte) -> out/esi: (addr buffered-file) { + var result: (handle buffered-file) + { + var tmp1/eax: (addr handle buffered-file) <- address result + open filename, 0, tmp1 + } + var tmp2/eax: (addr buffered-file) <- lookup result + out <- copy tmp2 +} + +fn dump in: (addr buffered-file) { + var c/eax: byte <- read-byte-buffered in + compare c, 0xffffffff # EOF marker + break-if-= + print-byte c + loop +} |