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 0
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 0, 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 0, 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 var g/eax: grapheme <- copy c
100 print-grapheme 0, g
101 col <- increment
102 loop
103 }
104 row <- increment
105 loop
106 }
107 }
108
109 fn update-attributes c: byte, _r: (addr render-state) {
110 var r/edi: (addr render-state) <- copy _r
111 var state/esi: (addr int) <- get r, current-state
112 $update-attributes:check-state: {
113 compare *state, 0
114 {
115 break-if-!=
116 compare c, 0x2a
117 {
118 break-if-!=
119
120 start-bold 0
121 copy-to *state, 1
122 break $update-attributes:check-state
123 }
124 compare c, 0x5f
125 {
126 break-if-!=
127
128 start-bold 0
129 copy-to *state, 1
130 break $update-attributes:check-state
131 }
132 break $update-attributes:check-state
133 }
134 {
135 break-if-=
136 compare c, 0x2a
137 {
138 break-if-!=
139
140 reset-formatting 0
141 copy-to *state, 0
142 break $update-attributes:check-state
143 }
144 compare c, 0x5f
145 {
146 break-if-!=
147
148 reset-formatting 0
149 copy-to *state, 0
150 break $update-attributes:check-state
151 }
152 break $update-attributes:check-state
153 }
154 }
155 }
156
157 fn clear toprow: int, leftcol: int, botrow: int, rightcol: int {
158 var row/ecx: int <- copy toprow
159 {
160 compare row, botrow
161 break-if->=
162 var col/edx: int <- copy leftcol
163 move-cursor 0, row, col
164 {
165 compare col, rightcol
166 break-if->=
167 print-string 0, " "
168 col <- increment
169 loop
170 }
171 row <- increment
172 loop
173 }
174 }
175
176 fn dump in: (addr buffered-file) {
177 var c/eax: byte <- read-byte-buffered in
178 compare c, 0xffffffff
179 break-if-=
180 var g/eax: grapheme <- copy c
181 print-grapheme 0, g
182 loop
183 }