https://github.com/akkartik/mu/blob/master/apps/browse.mu
1
2
3
4
5
6
7
8
9 fn main args-on-stack: (addr array (addr array byte)) -> exit-status/ebx: int {
10 +-- 20 lines: # var file/esi: (addr buffered-file) = open args-on-stack[1] for reading --------------------------------------------------------------------------------------------------
30 enable-screen-grid-mode
31 var nrows/eax: int <- copy 0
32 var ncols/ecx: int <- copy 0
33 nrows, ncols <- screen-size
34 enable-keyboard-immediate-mode
35 {
36 render file, nrows, ncols
37 var key/eax: byte <- read-key
38 compare key, 0x71
39 loop-if-!=
40 }
41 enable-keyboard-type-mode
42 enable-screen-type-mode
43 exit-status <- copy 0
44 }
45
46 type render-state {
47 current-state: int
48 }
49
50
51 fn render in: (addr buffered-file), nrows: int, ncols: int {
52
53
54
55
56
57
58
59
60
61 var _r: render-state
62 var r/edi: (addr render-state) <- address _r
63 var toprow/eax: int <- copy 2
64 var botrow/ecx: int <- copy nrows
65 var leftcol/edx: int <- copy 5
66 var rightcol/ebx: int <- copy leftcol
67 rightcol <- add 0x40
68 start-color-on-screen 0xec, 7
69 {
70 compare rightcol, ncols
71 break-if->=
72 render-page in, toprow, leftcol, botrow, rightcol, r
73 leftcol <- copy rightcol
74 leftcol <- add 5
75 rightcol <- copy leftcol
76 rightcol <- add 0x40
77 loop
78 }
79 }
80
81 fn render-page in: (addr buffered-file), toprow: int, leftcol: int, botrow: int, rightcol: int, r: (addr render-state) {
82 clear toprow, leftcol, botrow, rightcol
83
84 var row/ecx: int <- copy toprow
85 $line-loop: {
86 compare row, botrow
87 break-if->=
88 var col/edx: int <- copy leftcol
89 move-cursor-on-screen row, col
90 {
91 compare col, rightcol
92 break-if->=
93 var c/eax: byte <- read-byte-buffered in
94 compare c, 0xffffffff
95 break-if-= $line-loop
96 update-attributes c, r
97 compare c, 0xa
98 break-if-=
99 print-byte-to-screen c
100 col <- increment
101 loop
102 }
103 row <- increment
104 loop
105 }
106 }
107
108 fn update-attributes c: byte, _r: (addr render-state) {
109 var r/edi: (addr render-state) <- copy _r
110 var state/esi: (addr int) <- get r, current-state
111 $update-attributes:check-state: {
112 compare *state, 0
113 {
114 break-if-!=
115 compare c, 0x2a
116 {
117 break-if-!=
118
119 start-bold-on-screen
120 copy-to *state, 1
121 break $update-attributes:check-state
122 }
123 compare c, 0x5f
124 {
125 break-if-!=
126
127 start-bold-on-screen
128 copy-to *state, 1
129 break $update-attributes:check-state
130 }
131 break $update-attributes:check-state
132 }
133 {
134 break-if-=
135 compare c, 0x2a
136 {
137 break-if-!=
138
139 reset-formatting-on-screen
140 copy-to *state, 0
141 break $update-attributes:check-state
142 }
143 compare c, 0x5f
144 {
145 break-if-!=
146
147 reset-formatting-on-screen
148 copy-to *state, 0
149 break $update-attributes:check-state
150 }
151 break $update-attributes:check-state
152 }
153 }
154 }
155
156 fn clear toprow: int, leftcol: int, botrow: int, rightcol: int {
157 var row/ecx: int <- copy toprow
158 {
159 compare row, botrow
160 break-if->=
161 var col/edx: int <- copy leftcol
162 move-cursor-on-screen row, col
163 {
164 compare col, rightcol
165 break-if->=
166 print-string-to-screen " "
167 col <- increment
168 loop
169 }
170 row <- increment
171 loop
172 }
173 }
174
175 fn dump in: (addr buffered-file) {
176 var c/eax: byte <- read-byte-buffered in
177 compare c, 0xffffffff
178 break-if-=
179 print-byte-to-screen c
180 loop
181 }