diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-09-07 22:57:52 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-09-07 22:57:52 -0700 |
commit | bebb90a2ee96511155155df6ece60950077d3a4c (patch) | |
tree | 2da626dc72c0c503e5defa536debb56484fc4ca2 | |
parent | 664910816ce599adf4bbb5ce842424c49ec70359 (diff) | |
download | mu-bebb90a2ee96511155155df6ece60950077d3a4c.tar.gz |
6753
-rw-r--r-- | apps/browse/main.mu | 95 | ||||
-rw-r--r-- | apps/browse/paginated-screen.mu | 88 |
2 files changed, 126 insertions, 57 deletions
diff --git a/apps/browse/main.mu b/apps/browse/main.mu index 98d69588..611de7f3 100644 --- a/apps/browse/main.mu +++ b/apps/browse/main.mu @@ -9,13 +9,14 @@ fn main args: (addr array addr array byte) -> exit-status/ebx: int { # enable-screen-grid-mode enable-keyboard-immediate-mode - # initialize screen state from screen size + # initialize screen state var paginated-screen-storage: paginated-screen var paginated-screen/eax: (addr paginated-screen) <- address paginated-screen-storage - init-paginated-screen paginated-screen - normal-text 0 + initialize-paginated-screen paginated-screen + normal-text paginated-screen + # { - render 0, fs, paginated-screen + render paginated-screen, fs var key/eax: byte <- read-key compare key, 0x71 # 'q' loop-if-!= @@ -25,18 +26,18 @@ fn main args: (addr array addr array byte) -> exit-status/ebx: int { exit-status <- copy 0 } -fn render screen: (addr screen), fs: (addr buffered-file), state: (addr paginated-screen) { - start-drawing state - render-normal screen, fs, state +fn render screen: (addr paginated-screen), fs: (addr buffered-file) { + start-drawing screen + render-normal screen, fs } -fn render-normal screen: (addr screen), fs: (addr buffered-file), state: (addr paginated-screen) { +fn render-normal screen: (addr paginated-screen), fs: (addr buffered-file) { var newline-seen?/esi: boolean <- copy 0 # false var start-of-paragraph?/edi: boolean <- copy 1 # true var previous-grapheme/ebx: grapheme <- copy 0 $render-normal:loop: { - # if done-drawing?(state) break - var done?/eax: boolean <- done-drawing? state + # if done-drawing?(screen) break + var done?/eax: boolean <- done-drawing? screen compare done?, 0 # false break-if-!= var c/eax: grapheme <- read-grapheme-buffered fs @@ -59,8 +60,8 @@ $render-normal:loop-body: { # otherwise render two newlines { break-if-= - add-grapheme state, 0xa # newline - add-grapheme state, 0xa # newline + add-grapheme screen, 0xa # newline + add-grapheme screen, 0xa # newline newline-seen? <- copy 0 # false start-of-paragraph? <- copy 1 # true break $render-normal:loop-body @@ -73,7 +74,7 @@ $render-normal:loop-body: { compare c, 0x23 # '#' { break-if-!= - render-header-line screen, fs, state + render-header-line screen, fs newline-seen? <- copy 1 # true break $render-normal:loop-body } @@ -94,10 +95,10 @@ $render-normal:flush-buffered-newline: { { compare c, 0x20 break-if-!= - add-grapheme state, 0xa # newline + add-grapheme screen, 0xa # newline break $render-normal:flush-buffered-newline } - add-grapheme state, 0x20 # space + add-grapheme screen, 0x20 # space # fall through to print c } ## end soft newline support @@ -115,8 +116,9 @@ $render-normal:whitespace-separated-regions: { compare c, 0x2a # '*' { break-if-!= - start-bold screen - render-until-asterisk fs, state + start-color-on-paginated-screen screen, 0xec, 7 # 236 = darkish gray + start-bold-on-paginated-screen screen + render-until-asterisk screen, fs normal-text screen break $render-normal:loop-body } @@ -124,31 +126,30 @@ $render-normal:whitespace-separated-regions: { compare c, 0x5f # '_' { break-if-!= - start-color screen, 0xec, 7 # 236 = darkish gray - start-bold screen - render-until-underscore fs, state - reset-formatting screen - start-color screen, 0xec, 7 # 236 = darkish gray + start-color-on-paginated-screen screen, 0xec, 7 # 236 = darkish gray + start-bold-on-paginated-screen screen + render-until-underscore screen, fs + normal-text screen break $render-normal:loop-body } } # - add-grapheme state, c + add-grapheme screen, c } # $render-normal:loop-body previous-grapheme <- copy c loop } # $render-normal:loop } -fn render-header-line screen: (addr screen), fs: (addr buffered-file), state: (addr paginated-screen) { +fn render-header-line screen: (addr paginated-screen), fs: (addr buffered-file) { $render-header-line:body: { # compute color based on number of '#'s var header-level/esi: int <- copy 1 # caller already grabbed one var c/eax: grapheme <- copy 0 { - # if done-drawing?(state) return + # if done-drawing?(screen) return { - var done?/eax: boolean <- done-drawing? state + var done?/eax: boolean <- done-drawing? screen compare done?, 0 # false break-if-!= $render-header-line:body } @@ -164,9 +165,9 @@ $render-header-line:body: { } start-heading screen, header-level { - # if done-drawing?(state) break + # if done-drawing?(screen) break { - var done?/eax: boolean <- done-drawing? state + var done?/eax: boolean <- done-drawing? screen compare done?, 0 # false break-if-!= } @@ -179,7 +180,7 @@ $render-header-line:body: { compare c, 0xa # newline break-if-= # - add-grapheme state, c + add-grapheme screen, c # loop } @@ -188,41 +189,41 @@ $render-header-line:body: { } # colors for a light background, going from bright to dark (meeting up with bold-text) -fn start-heading screen: (addr screen), header-level: int { +fn start-heading screen: (addr paginated-screen), header-level: int { $start-heading:body: { - start-bold screen + start-bold-on-paginated-screen screen compare header-level, 1 { break-if-!= - start-color screen, 0xa0, 7 + start-color-on-paginated-screen screen, 0xa0, 7 break $start-heading:body } compare header-level, 2 { break-if-!= - start-color screen, 0x7c, 7 + start-color-on-paginated-screen screen, 0x7c, 7 break $start-heading:body } compare header-level, 3 { break-if-!= - start-color screen, 0x58, 7 + start-color-on-paginated-screen screen, 0x58, 7 break $start-heading:body } compare header-level, 4 { break-if-!= - start-color screen, 0x34, 7 + start-color-on-paginated-screen screen, 0x34, 7 break $start-heading:body } - start-color screen, 0xe8, 7 + start-color-on-paginated-screen screen, 0xe8, 7 } } -fn render-until-asterisk fs: (addr buffered-file), state: (addr paginated-screen) { +fn render-until-asterisk screen: (addr paginated-screen), fs: (addr buffered-file) { { - # if done-drawing?(state) break - var done?/eax: boolean <- done-drawing? state + # if done-drawing?(screen) break + var done?/eax: boolean <- done-drawing? screen compare done?, 0 # false break-if-!= # @@ -234,16 +235,16 @@ fn render-until-asterisk fs: (addr buffered-file), state: (addr paginated-screen compare c, 0x2a # '*' break-if-= # - add-grapheme state, c + add-grapheme screen, c # loop } } -fn render-until-underscore fs: (addr buffered-file), state: (addr paginated-screen) { +fn render-until-underscore screen: (addr paginated-screen), fs: (addr buffered-file) { { - # if done-drawing?(state) break - var done?/eax: boolean <- done-drawing? state + # if done-drawing?(screen) break + var done?/eax: boolean <- done-drawing? screen compare done?, 0 # false break-if-!= # @@ -255,7 +256,7 @@ fn render-until-underscore fs: (addr buffered-file), state: (addr paginated-scre compare c, 0x5f # '_' break-if-= # - add-grapheme state, c + add-grapheme screen, c # loop } @@ -267,7 +268,7 @@ fn first-arg args-on-stack: (addr array addr array byte) -> out/eax: (addr array out <- copy *result } -fn normal-text screen: (addr screen) { - reset-formatting screen - start-color screen, 0xec, 7 # 236 = darkish gray +fn normal-text screen: (addr paginated-screen) { + reset-formatting-on-paginated-screen screen + start-color-on-paginated-screen screen, 0xec, 7 # 236 = darkish gray } diff --git a/apps/browse/paginated-screen.mu b/apps/browse/paginated-screen.mu index 564411f8..9da3e4d7 100644 --- a/apps/browse/paginated-screen.mu +++ b/apps/browse/paginated-screen.mu @@ -1,6 +1,9 @@ -# if a screen is too wide, split it up into a fixed size of pages +# If a screen is too wide, split it up into a fixed size of pages. +# We take control of drawing and moving the cursor, and delegate everything +# else. type paginated-screen { + screen: (handle screen) nrows: int # const ncols: int # const toprow: int @@ -11,15 +14,27 @@ type paginated-screen { col: int } -fn init-paginated-screen _self: (addr paginated-screen) { +fn initialize-fake-paginated-screen _self: (addr paginated-screen), nrows: int, ncols: int { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + allocate screen-ah + var screen-addr/eax: (addr screen) <- lookup *screen-ah + initialize-screen screen-addr, nrows, ncols + initialize-paginated-screen self +} + +fn initialize-paginated-screen _self: (addr paginated-screen) { # hardcoded parameters: # top-margin # page-margin # page-width var self/esi: (addr paginated-screen) <- copy _self - var nrows/eax: int <- copy 0xa - var ncols/ecx: int <- copy 0x20 - nrows, ncols <- screen-size 0 # Comment this out to debug with a tiny page. You'll also need to adjust rightcol below. + var screen-ah/eax: (addr handle screen) <- get self, screen + var _screen-addr/eax: (addr screen) <- lookup *screen-ah + var screen-addr/edi: (addr screen) <- copy _screen-addr + var nrows/eax: int <- copy 0 + var ncols/ecx: int <- copy 0 + nrows, ncols <- screen-size screen-addr var dest/edx: (addr int) <- copy 0 # self->nrows = nrows dest <- get self, nrows @@ -39,9 +54,9 @@ fn init-paginated-screen _self: (addr paginated-screen) { fn start-drawing _self: (addr paginated-screen) { var self/esi: (addr paginated-screen) <- copy _self + clear-paginated-screen self var tmp/eax: (addr int) <- copy 0 var tmp2/ecx: int <- copy 0 - clear-screen 0 # self->leftcol = page-margin tmp <- get self, leftcol copy-to *tmp, 5 # left-margin @@ -74,7 +89,9 @@ $add-grapheme:body: { break $add-grapheme:body } # print c - print-grapheme 0, c + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + print-grapheme screen-addr, c # self->col++ var tmp/eax: (addr int) <- get self, col increment *tmp @@ -167,7 +184,58 @@ $done-drawing?:body: { fn reposition-cursor _self: (addr paginated-screen) { var self/esi: (addr paginated-screen) <- copy _self - var r/eax: (addr int) <- get self, row - var c/ecx: (addr int) <- get self, col - move-cursor 0, *r *c + var r/ecx: (addr int) <- get self, row + var c/edx: (addr int) <- get self, col + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + move-cursor screen-addr, *r *c +} + +fn clear-paginated-screen _self: (addr paginated-screen) { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + clear-screen screen-addr +} + +fn start-color-on-paginated-screen _self: (addr paginated-screen), fg: int, bg: int { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + start-color screen-addr, fg, bg +} + +fn start-bold-on-paginated-screen _self: (addr paginated-screen) { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + start-bold screen-addr +} + +fn start-underline-on-paginated-screen _self: (addr paginated-screen) { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + start-underline screen-addr +} + +fn start-reverse-video-on-paginated-screen _self: (addr paginated-screen) { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + start-reverse-video screen-addr +} + +fn start-blinking-on-paginated-screen _self: (addr paginated-screen) { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + start-blinking screen-addr +} + +fn reset-formatting-on-paginated-screen _self: (addr paginated-screen) { + var self/esi: (addr paginated-screen) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + var screen-addr/eax: (addr screen) <- lookup *screen-ah + reset-formatting screen-addr } |