diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-06-01 08:13:03 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-06-01 08:13:03 -0700 |
commit | 42ee204acf5ff8ab5abb5844a01bf644b928d391 (patch) | |
tree | fda370a4e6695440bfcdf227a244d085914bbf41 | |
parent | 9cc69d69c6cd83ed4aa367b4a08ca379cae45805 (diff) | |
download | mu-42ee204acf5ff8ab5abb5844a01bf644b928d391.tar.gz |
6457 - revert apps/browse.mu to a working state
-rw-r--r-- | apps/browse.mu | 256 |
1 files changed, 61 insertions, 195 deletions
diff --git a/apps/browse.mu b/apps/browse.mu index 353c49b4..f442d2a4 100644 --- a/apps/browse.mu +++ b/apps/browse.mu @@ -10,15 +10,12 @@ 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 display-state-storage: display-state - var display-state: (addr display-state) = address display-state-storage - init-display-state display-state, nrows, ncols + enable-keyboard-immediate-mode { - render file, display-state + render file, nrows, ncols var key/eax: byte <- read-key compare key, 0x71 # 'q' loop-if-!= @@ -28,143 +25,10 @@ fn main args: (addr array (addr array byte)) -> exit-status/ebx: int { exit-status <- copy 0 } -type display-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 display-state) { - start-drawing state - render-normal in, state -} - -fn render-normal in: (addr buffered-file), state: (addr display-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 display-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 display-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 display-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-display-state self: (addr display-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) +type render-state { + current-state: int # enum 0: normal, 1: bold } -fn start-drawing self: (addr display-state) { - # self->row = toprow - # self->col = leftcol -} - -fn add-char self: (addr display-state), c: byte { - # print c - # self->col++ - # if (self->col > self->rightcol) next-line(self) -} - -fn next-line self: (addr display-state) { - # self->row++ - # if (self->row > self->botrow) next-page(self) -} - -fn next-page self: (addr display-state) { - # self->leftcol = self->rightcol + 5 - # self->rightcol = self->leftcol + text-width -} - -fn done-drawing? self: (addr display-state) -> result/eax: boolean { - # self->rightcol >= self->ncols -} - -# screen manipulation: -# properties: width, height -# reset attributes -# clear -# color -# bold -# underline -# print char - -# pages: -# properties: screen, row, col -# methods for new pages -# new page -# new line - # decide how to lay out pages on screen fn render in: (addr buffered-file), nrows: int, ncols: int { # Fit multiple pages on screen on separate columns, each wide enough to read @@ -176,6 +40,8 @@ fn render in: (addr buffered-file), nrows: int, ncols: int { # top-margin # page-margin # text-width + var _r: render-state + var r/edi: (addr render-state) <- address _r var toprow/eax: int <- copy 2 # top-margin var botrow/ecx: int <- copy nrows var leftcol/edx: int <- copy 5 # page-margin @@ -185,8 +51,7 @@ fn render in: (addr buffered-file), nrows: int, ncols: int { { compare rightcol, ncols break-if->= - clear toprow, leftcol, botrow, rightcol - render-page in, toprow, leftcol, botrow, rightcol + render-page in, toprow, leftcol, botrow, rightcol, r leftcol <- copy rightcol leftcol <- add 5 # page-margin rightcol <- copy leftcol @@ -195,69 +60,22 @@ fn render in: (addr buffered-file), nrows: int, ncols: int { } } -fn render-page in: (addr buffered-file), toprow: int, leftcol: int, botrow: int, rightcol: int { +fn render-page in: (addr buffered-file), toprow: int, leftcol: int, botrow: int, rightcol: int, r: (addr render-state) { + clear toprow, leftcol, botrow, rightcol + # render screen rows var row/ecx: int <- copy toprow -$line-loop: { +$line-loop: { compare row, botrow break-if->= var col/edx: int <- copy leftcol move-cursor row, col -$char-loop: { + { compare col, rightcol break-if->= var c/eax: byte <- read-byte-buffered in compare c, 0xffffffff # EOF marker break-if-= $line-loop -$change-state: { - compare *state, 0 # normal - { - break-if-!= - compare c, 0x2a # '*' - { - break-if-!= - # r->current-state == 0 && c == '*' => bold text - start-bold - copy-to *state, 1 - break $change-state - } - compare c, 0x5f # '_' - { - break-if-!= - # r->current-state == 0 && c == '_' => bold text - start-bold - copy-to *state, 1 - break $change-state - } - break $change-state - } - compare *state, 1 # bold - { - break-if-!= - compare c, 0x2a # '*' - { - break-if-!= - # r->current-state == 1 && c == '*' => print c, then normal text - print-byte c - col <- increment - reset-formatting - start-color 0xec, 7 # 236 = darkish gray - copy-to *state, 0 - loop $char-loop - } - compare c, 0x5f # '_' - { - break-if-!= - print-byte c - col <- increment - # r->current-state == 1 && c == '_' => print c, then normal text - reset-formatting - start-color 0xec, 7 # 236 = darkish gray - copy-to *state, 0 - loop $char-loop - } - break $change-state - } - } + update-attributes c, r compare c, 0xa # newline break-if-= # no need to print newlines print-byte c @@ -269,6 +87,54 @@ $change-state: { } } +fn update-attributes c: byte, _r: (addr render-state) { + var r/edi: (addr render-state) <- copy _r + var state/esi: (addr int) <- get r, current-state +$update-attributes:check-state: { + compare *state, 0 # normal + { + break-if-!= + compare c, 0x2a # '*' + { + break-if-!= + # r->current-state == 0 && c == '*' + start-bold + copy-to *state, 1 + break $update-attributes:check-state + } + compare c, 0x5f # '_' + { + break-if-!= + # r->current-state == 0 && c == '_' + start-bold + copy-to *state, 1 + break $update-attributes:check-state + } + break $update-attributes:check-state + } + { + break-if-= + compare c, 0x2a # '*' + { + break-if-!= + # r->current-state == 1 && c == '*' + reset-formatting + copy-to *state, 0 + break $update-attributes:check-state + } + compare c, 0x5f # '_' + { + break-if-!= + # r->current-state == 1 && c == '_' + reset-formatting + copy-to *state, 0 + break $update-attributes:check-state + } + break $update-attributes:check-state + } + } +} + fn clear toprow: int, leftcol: int, botrow: int, rightcol: int { var row/ecx: int <- copy toprow { |