1
2
3
4
5
6 def! main [
7 local-scope
8 open-console
9 env:&:environment <- new-programming-environment 0/filesystem, 0/screen
10 render-all 0/screen, env, render
11 event-loop 0/screen, 0/console, env, 0/filesystem
12
13 ]
14
15 container environment [
16 recipes:&:editor
17 current-sandbox:&:editor
18 sandbox-in-focus?:bool
19 ]
20
21 def new-programming-environment resources:&:resources, screen:&:screen, test-sandbox-editor-contents:text -> result:&:environment [
22 local-scope
23 load-ingredients
24 width:num <- screen-width screen
25 result <- new environment:type
26
27 initial-recipe-contents:text <- slurp resources, [lesson/recipes.mu]
28 divider:num, _ <- divide-with-remainder width, 2
29 recipes:&:editor <- new-editor initial-recipe-contents, 0/left, divider/right
30
31 sandbox-left:num <- add divider, 1
32 current-sandbox:&:editor <- new-editor test-sandbox-editor-contents, sandbox-left, width/right
33 *result <- put *result, recipes:offset, recipes
34 *result <- put *result, current-sandbox:offset, current-sandbox
35 *result <- put *result, sandbox-in-focus?:offset, 0/false
36 <programming-environment-initialization>
37 ]
38
39 def event-loop screen:&:screen, console:&:console, env:&:environment, resources:&:resources -> screen:&:screen, console:&:console, env:&:environment, resources:&:resources [
40 local-scope
41 load-ingredients
42 recipes:&:editor <- get *env, recipes:offset
43 current-sandbox:&:editor <- get *env, current-sandbox:offset
44 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
45 {
46 ¦
47 ¦ +next-event
48 ¦ e:event, found?:bool, quit?:bool, console <- read-event console
49 ¦ loop-unless found?
50 ¦ break-if quit?
51 ¦ trace 10, [app], [next-event]
52 ¦ <handle-event>
53 ¦
54 ¦ {
55 ¦ ¦ k:num, is-keycode?:bool <- maybe-convert e:event, keycode:variant
56 ¦ ¦ break-unless is-keycode?
57 ¦ ¦ <global-keypress>
58 ¦ }
59 ¦ {
60 ¦ ¦ c:char, is-unicode?:bool <- maybe-convert e:event, text:variant
61 ¦ ¦ break-unless is-unicode?
62 ¦ ¦ <global-type>
63 ¦ }
64 ¦
65 ¦ {
66 ¦ ¦ t:touch-event, is-touch?:bool <- maybe-convert e:event, touch:variant
67 ¦ ¦ break-unless is-touch?
68 ¦ ¦
69 ¦ ¦
70 ¦ ¦ touch-type:num <- get t, type:offset
71 ¦ ¦ is-left-click?:bool <- equal touch-type, 65513/mouse-left
72 ¦ ¦ loop-unless is-left-click?, +next-event
73 ¦ ¦ click-row:num <- get t, row:offset
74 ¦ ¦ click-column:num <- get t, column:offset
75 ¦ ¦
76 ¦ ¦ <global-touch>
77 ¦ ¦
78 ¦ ¦ _ <- move-cursor-in-editor screen, recipes, t
79 ¦ ¦ sandbox-in-focus?:bool <- move-cursor-in-editor screen, current-sandbox, t
80 ¦ ¦ *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
81 ¦ ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
82 ¦ ¦ loop +next-event
83 ¦ }
84 ¦
85 ¦
86 ¦ {
87 ¦ ¦ r:resize-event, is-resize?:bool <- maybe-convert e:event, resize:variant
88 ¦ ¦ break-unless is-resize?
89 ¦ ¦ env, screen <- resize screen, env
90 ¦ ¦ screen <- render-all screen, env, render-without-moving-cursor
91 ¦ ¦ loop +next-event
92 ¦ }
93 ¦
94 ¦ {
95 ¦ ¦ sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
96 ¦ ¦ {
97 ¦ ¦ ¦ break-if sandbox-in-focus?
98 ¦ ¦ ¦ render?:bool <- handle-keyboard-event screen, recipes, e:event
99 ¦ ¦ ¦ break-unless render?
100 ¦ ¦ ¦ screen <- render-all screen, env, render
101 ¦ ¦ }
102 ¦ ¦ {
103 ¦ ¦ ¦ break-unless sandbox-in-focus?
104 ¦ ¦ ¦ render?:bool <- handle-keyboard-event screen, current-sandbox, e:event
105 ¦ ¦ ¦ break-unless render?
106 ¦ ¦ ¦ screen <- render-sandbox-side screen, env, render
107 ¦ ¦ }
108 ¦ ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
109 ¦ }
110 ¦ loop
111 }
112 ]
113
114 def resize screen:&:screen, env:&:environment -> env:&:environment, screen:&:screen [
115 local-scope
116 load-ingredients
117 clear-screen screen
118 width:num <- screen-width screen
119 divider:num, _ <- divide-with-remainder width, 2
120
121 recipes:&:editor <- get *env, recipes:offset
122 right:num <- subtract divider, 1
123 *recipes <- put *recipes, right:offset, right
124
125 *recipes <- put *recipes, cursor-row:offset, 1
126 *recipes <- put *recipes, cursor-column:offset, 0
127
128 current-sandbox:&:editor <- get *env, current-sandbox:offset
129 left:num <- add divider, 1
130 *current-sandbox <- put *current-sandbox, left:offset, left
131 right:num <- subtract width, 1
132 *current-sandbox <- put *current-sandbox, right:offset, right
133
134 *current-sandbox <- put *current-sandbox, cursor-row:offset, 1
135 *current-sandbox <- put *current-sandbox, cursor-column:offset, left
136 ]
137
138
139
140
141 def render-without-moving-cursor screen:&:screen, editor:&:editor -> last-row:num, last-column:num, screen:&:screen, editor:&:editor [
142 local-scope
143 load-ingredients
144 return-unless editor, 1/top, 0/left
145 left:num <- get *editor, left:offset
146 screen-height:num <- screen-height screen
147 right:num <- get *editor, right:offset
148 curr:&:duplex-list:char <- get *editor, top-of-screen:offset
149 prev:&:duplex-list:char <- copy curr
150 curr <- next curr
151 color:num <- copy 7/white
152 row:num <- copy 1/top
153 column:num <- copy left
154
155 old-before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
156
157
158 *editor <- put *editor, cursor-row:offset, row
159 *editor <- put *editor, cursor-column:offset, column
160 top-of-screen:&:duplex-list:char <- get *editor, top-of-screen:offset
161 *editor <- put *editor, before-cursor:offset, top-of-screen
162 screen <- move-cursor screen, row, column
163 {
164 ¦ +next-character
165 ¦ break-unless curr
166 ¦ off-screen?:bool <- greater-or-equal row, screen-height
167 ¦ break-if off-screen?
168 ¦
169 ¦
170 ¦
171 ¦ {
172 ¦ ¦ at-cursor?:bool <- equal old-before-cursor, prev
173 ¦ ¦ break-unless at-cursor?
174 ¦ ¦ *editor <- put *editor, cursor-row:offset, row
175 ¦ ¦ *editor <- put *editor, cursor-column:offset, column
176 ¦ ¦ *editor <- put *editor, before-cursor:offset, old-before-cursor
177 ¦ }
178 ¦ c:char <- get *curr, value:offset
179 ¦ <character-c-received>
180 ¦ {
181 ¦ ¦
182 ¦ ¦ newline?:bool <- equal c, 10/newline
183 ¦ ¦ break-unless newline?
184 ¦ ¦
185 ¦ ¦ clear-line-until screen, right
186 ¦ ¦
187 ¦ ¦ row <- add row, 1
188 ¦ ¦ column <- copy left
189 ¦ ¦ screen <- move-cursor screen, row, column
190 ¦ ¦ curr <- next curr
191 ¦ ¦ prev <- next prev
192 ¦ ¦ loop +next-character
193 ¦ }
194 ¦ {
195 ¦ ¦
196 ¦ ¦
197 ¦ ¦ at-right?:bool <- equal column, right
198 ¦ ¦ break-unless at-right?
199 ¦ ¦
200 ¦ ¦ wrap-icon:char <- copy 8617/loop-back-to-left
201 ¦ ¦ print screen, wrap-icon, 245/grey
202 ¦ ¦ column <- copy left
203 ¦ ¦ row <- add row, 1
204 ¦ ¦ screen <- move-cursor screen, row, column
205 ¦ ¦
206 ¦ ¦ loop +next-character
207 ¦ }
208 ¦ print screen, c, color
209 ¦ curr <- next curr
210 ¦ prev <- next prev
211 ¦ column <- add column, 1
212 ¦ loop
213 }
214
215 *editor <- put *editor, bottom-of-screen:offset, curr
216 *editor <- put *editor, bottom:offset, row
217 return row, column
218 ]
219
220 scenario point-at-multiple-editors [
221 local-scope
222 trace-until 100/app
223 assume-screen 30/width, 5/height
224
225 assume-resources [
226 ¦ [lesson/recipes.mu] <- [
227 ¦ ¦ |abc|
228 ¦ ]
229 ]
230 env:&:environment <- new-programming-environment resources, screen, [def]
231
232 assume-console [
233 ¦ left-click 1, 1
234 ¦ left-click 1, 17
235 ]
236
237 run [
238 ¦ event-loop screen, console, env, resources
239 ¦ recipes:&:editor <- get *env, recipes:offset
240 ¦ 5:num/raw <- get *recipes, cursor-column:offset
241 ¦ sandbox:&:editor <- get *env, current-sandbox:offset
242 ¦ 7:num/raw <- get *sandbox, cursor-column:offset
243 ]
244 memory-should-contain [
245 ¦ 5 <- 1
246 ¦ 7 <- 17
247 ]
248 ]
249
250 scenario edit-multiple-editors [
251 local-scope
252 trace-until 100/app
253 assume-screen 30/width, 5/height
254
255 assume-resources [
256 ¦ [lesson/recipes.mu] <- [
257 ¦ ¦ |abc|
258 ¦ ]
259 ]
260 env:&:environment <- new-programming-environment resources, screen, [def]
261 render-all screen, env, render
262
263 assume-console [
264 ¦ left-click 1, 1
265 ¦ type [0]
266 ¦ left-click 1, 17
267 ¦ type [1]
268 ]
269 run [
270 ¦ event-loop screen, console, env, resources
271 ¦ recipes:&:editor <- get *env, recipes:offset
272 ¦ 5:num/raw <- get *recipes, cursor-column:offset
273 ¦ sandbox:&:editor <- get *env, current-sandbox:offset
274 ¦ 7:num/raw <- get *sandbox, cursor-column:offset
275 ]
276 screen-should-contain [
277 ¦ . run (F4) . # this line has a different background, but we don't test that yet
278 ¦ .a0bc ╎d1ef .
279 ¦ . ╎──────────────.
280 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
281 ¦ . ╎ .
282 ]
283 memory-should-contain [
284 ¦ 5 <- 2
285 ¦ 7 <- 18
286 ]
287
288 run [
289 ¦ cursor:char <- copy 9251/␣
290 ¦ print screen, cursor
291 ]
292 screen-should-contain [
293 ¦ . run (F4) .
294 ¦ .a0bc ╎d1␣f .
295 ¦ . ╎──────────────.
296 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
297 ¦ . ╎ .
298 ]
299 ]
300
301 scenario editor-in-focus-keeps-cursor [
302 local-scope
303 trace-until 100/app
304 assume-screen 30/width, 5/height
305 assume-resources [
306 ¦ [lesson/recipes.mu] <- [
307 ¦ ¦ |abc|
308 ¦ ]
309 ]
310 env:&:environment <- new-programming-environment resources, screen, [def]
311 render-all screen, env, render
312
313 assume-console []
314 run [
315 ¦ event-loop screen, console, env, resources
316 ¦ cursor:char <- copy 9251/␣
317 ¦ print screen, cursor
318 ]
319
320 screen-should-contain [
321 ¦ . run (F4) .
322 ¦ .␣bc ╎def .
323 ¦ . ╎──────────────.
324 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
325 ¦ . ╎ .
326 ]
327
328 assume-console [
329 ¦ type [z]
330 ]
331 run [
332 ¦ event-loop screen, console, env, resources
333 ¦ cursor:char <- copy 9251/␣
334 ¦ print screen, cursor
335 ]
336
337 screen-should-contain [
338 ¦ . run (F4) .
339 ¦ .z␣bc ╎def .
340 ¦ . ╎──────────────.
341 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
342 ¦ . ╎ .
343 ]
344 ]
345
346 scenario backspace-in-sandbox-editor-joins-lines [
347 local-scope
348 trace-until 100/app
349 assume-screen 30/width, 5/height
350 assume-resources [
351 ]
352
353 test-sandbox-editor-contents:text <- new [abc
354 def]
355 env:&:environment <- new-programming-environment resources, screen, test-sandbox-editor-contents
356 render-all screen, env, render
357 screen-should-contain [
358 ¦ . run (F4) .
359 ¦ . ╎abc .
360 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎def .
361 ¦ . ╎──────────────.
362 ¦ . ╎ .
363 ]
364
365 assume-console [
366 ¦ left-click 2, 16
367 ¦ press backspace
368 ]
369 run [
370 ¦ event-loop screen, console, env, resources
371 ¦ cursor:char <- copy 9251/␣
372 ¦ print screen, cursor
373 ]
374
375 screen-should-contain [
376 ¦ . run (F4) .
377 ¦ . ╎abc␣ef .
378 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎──────────────.
379 ¦ . ╎ .
380 ]
381 ]
382
383 def render-all screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
384 local-scope
385 load-ingredients
386 trace 10, [app], [render all]
387
388 trace 11, [app], [render top menu]
389 width:num <- screen-width screen
390 draw-horizontal screen, 0, 0/left, width, 32/space, 0/black, 238/grey
391 button-start:num <- subtract width, 20
392 button-on-screen?:bool <- greater-or-equal button-start, 0
393 assert button-on-screen?, [screen too narrow for menu]
394 screen <- move-cursor screen, 0/row, button-start
395 print screen, [ run (F4) ], 255/white, 161/reddish
396
397 trace 11, [app], [render divider]
398 divider:num, _ <- divide-with-remainder width, 2
399 height:num <- screen-height screen
400 draw-vertical screen, divider, 1/top, height, 9482/vertical-dotted
401
402 screen <- render-recipes screen, env, render-editor
403 screen <- render-sandbox-side screen, env, render-editor
404 <render-components-end>
405
406 recipes:&:editor <- get *env, recipes:offset
407 current-sandbox:&:editor <- get *env, current-sandbox:offset
408 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
409 screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
410 ]
411
412 def render-recipes screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
413 local-scope
414 load-ingredients
415 trace 11, [app], [render recipes]
416 recipes:&:editor <- get *env, recipes:offset
417
418 left:num <- get *recipes, left:offset
419 right:num <- get *recipes, right:offset
420 row:num, column:num, screen <- call render-editor, screen, recipes
421 clear-line-until screen, right
422 row <- add row, 1
423 <render-recipe-components-end>
424
425 draw-horizontal screen, row, left, right, 9480/horizontal-dotted
426 row <- add row, 1
427 clear-screen-from screen, row, left, left, right
428 ]
429
430
431 def render-sandbox-side screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
432 local-scope
433 load-ingredients
434 current-sandbox:&:editor <- get *env, current-sandbox:offset
435 left:num <- get *current-sandbox, left:offset
436 right:num <- get *current-sandbox, right:offset
437 row:num, column:num, screen, current-sandbox <- call render-editor, screen, current-sandbox
438 clear-line-until screen, right
439 row <- add row, 1
440
441 draw-horizontal screen, row, left, right
442 row <- add row, 1
443 clear-screen-from screen, row, left, left, right
444 ]
445
446 def update-cursor screen:&:screen, recipes:&:editor, current-sandbox:&:editor, sandbox-in-focus?:bool, env:&:environment -> screen:&:screen [
447 local-scope
448 load-ingredients
449 <update-cursor-special-cases>
450 {
451 ¦ break-if sandbox-in-focus?
452 ¦ cursor-row:num <- get *recipes, cursor-row:offset
453 ¦ cursor-column:num <- get *recipes, cursor-column:offset
454 }
455 {
456 ¦ break-unless sandbox-in-focus?
457 ¦ cursor-row:num <- get *current-sandbox, cursor-row:offset
458 ¦ cursor-column:num <- get *current-sandbox, cursor-column:offset
459 }
460 screen <- move-cursor screen, cursor-row, cursor-column
461 ]
462
463
464
465
466 after <global-type> [
467 {
468 ¦ switch-side?:bool <- equal c, 14/ctrl-n
469 ¦ break-unless switch-side?
470 ¦ sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
471 ¦ sandbox-in-focus? <- not sandbox-in-focus?
472 ¦ *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
473 ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
474 ¦ loop +next-event
475 }
476 ]
477
478
479
480 def draw-vertical screen:&:screen, col:num, y:num, bottom:num -> screen:&:screen [
481 local-scope
482 load-ingredients
483 style:char, style-found?:bool <- next-ingredient
484 {
485 ¦ break-if style-found?
486 ¦ style <- copy 9474/vertical
487 }
488 color:num, color-found?:bool <- next-ingredient
489 {
490 ¦
491 ¦ break-if color-found?
492 ¦ color <- copy 245/grey
493 }
494 {
495 ¦ continue?:bool <- lesser-than y, bottom
496 ¦ break-unless continue?
497 ¦ screen <- move-cursor screen, y, col
498 ¦ print screen, style, color
499 ¦ y <- add y, 1
500 ¦ loop
501 }
502 ]