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
48 render-all-on-no-more-events?:bool <- copy 0/false
49 {
50
51 +next-event
52 e:event, found?:bool, quit?:bool, console <- read-event console
53 loop-unless found?
54 break-if quit?
55 trace 10, [app], [next-event]
56 <handle-event>
57
58 {
59 k:num, is-keycode?:bool <- maybe-convert e:event, keycode:variant
60 break-unless is-keycode?
61 <global-keypress>
62 }
63 {
64 c:char, is-unicode?:bool <- maybe-convert e:event, text:variant
65 break-unless is-unicode?
66 <global-type>
67 }
68
69 {
70 t:touch-event, is-touch?:bool <- maybe-convert e:event, touch:variant
71 break-unless is-touch?
72
73
74 touch-type:num <- get t, type:offset
75 is-left-click?:bool <- equal touch-type, 65513/mouse-left
76 loop-unless is-left-click?, +next-event
77 click-row:num <- get t, row:offset
78 click-column:num <- get t, column:offset
79
80 <global-touch>
81
82 _ <- move-cursor-in-editor screen, recipes, t
83 sandbox-in-focus?:bool <- move-cursor-in-editor screen, current-sandbox, t
84 *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
85 screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
86 loop +next-event
87 }
88
89
90 {
91 r:resize-event, is-resize?:bool <- maybe-convert e:event, resize:variant
92 break-unless is-resize?
93
94 more-events?:bool <- has-more-events? console
95 {
96 break-unless more-events?
97 render-all-on-no-more-events? <- copy 1/true
98 }
99 {
100 break-if more-events?
101 env, screen <- resize screen, env
102 screen <- render-all screen, env, render-without-moving-cursor
103 render-all-on-no-more-events? <- copy 0/false
104 }
105 loop +next-event
106 }
107
108 {
109 hide-screen screen
110 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
111 {
112 break-if sandbox-in-focus?
113 render?:bool <- handle-keyboard-event screen, recipes, e:event
114
115
116 more-events?:bool <- has-more-events? console
117 {
118 break-unless more-events?
119 render-all-on-no-more-events? <- copy 1/true
120 jump +finish-event
121 }
122 {
123 break-if more-events?
124 {
125 break-unless render-all-on-no-more-events?
126
127 screen <- render-all screen, env, render
128 render-all-on-no-more-events? <- copy 0/false
129 jump +finish-event
130 }
131
132 {
133 break-unless render?
134 screen <- render-recipes screen, env, render
135 jump +finish-event
136 }
137 }
138 }
139 {
140 break-unless sandbox-in-focus?
141 render?:bool <- handle-keyboard-event screen, current-sandbox, e:event
142
143
144 more-events?:bool <- has-more-events? console
145 {
146 break-unless more-events?
147 render-all-on-no-more-events? <- copy 1/true
148 jump +finish-event
149 }
150 {
151 break-if more-events?
152 {
153 break-unless render-all-on-no-more-events?
154
155 screen <- render-all screen, env, render
156 render-all-on-no-more-events? <- copy 0/false
157 jump +finish-event
158 }
159
160 {
161 break-unless render?
162 screen <- render-sandbox-side screen, env, render
163 jump +finish-event
164 }
165 }
166 }
167 +finish-event
168 screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
169 show-screen screen
170 }
171 loop
172 }
173 ]
174
175 def resize screen:&:screen, env:&:environment -> env:&:environment, screen:&:screen [
176 local-scope
177 load-ingredients
178 clear-screen screen
179 width:num <- screen-width screen
180 divider:num, _ <- divide-with-remainder width, 2
181
182 recipes:&:editor <- get *env, recipes:offset
183 right:num <- subtract divider, 1
184 *recipes <- put *recipes, right:offset, right
185
186 *recipes <- put *recipes, cursor-row:offset, 1
187 *recipes <- put *recipes, cursor-column:offset, 0
188
189 current-sandbox:&:editor <- get *env, current-sandbox:offset
190 left:num <- add divider, 1
191 *current-sandbox <- put *current-sandbox, left:offset, left
192 right:num <- subtract width, 1
193 *current-sandbox <- put *current-sandbox, right:offset, right
194
195 *current-sandbox <- put *current-sandbox, cursor-row:offset, 1
196 *current-sandbox <- put *current-sandbox, cursor-column:offset, left
197 ]
198
199
200
201
202 def render-without-moving-cursor screen:&:screen, editor:&:editor -> last-row:num, last-column:num, screen:&:screen, editor:&:editor [
203 local-scope
204 load-ingredients
205 return-unless editor, 1/top, 0/left
206 left:num <- get *editor, left:offset
207 screen-height:num <- screen-height screen
208 right:num <- get *editor, right:offset
209 curr:&:duplex-list:char <- get *editor, top-of-screen:offset
210 prev:&:duplex-list:char <- copy curr
211 curr <- next curr
212 +render-loop-initialization
213 color:num <- copy 7/white
214 row:num <- copy 1/top
215 column:num <- copy left
216
217 old-before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
218
219
220 *editor <- put *editor, cursor-row:offset, row
221 *editor <- put *editor, cursor-column:offset, column
222 top-of-screen:&:duplex-list:char <- get *editor, top-of-screen:offset
223 *editor <- put *editor, before-cursor:offset, top-of-screen
224 screen <- move-cursor screen, row, column
225 {
226 +next-character
227 break-unless curr
228 off-screen?:bool <- greater-or-equal row, screen-height
229 break-if off-screen?
230
231
232
233 {
234 at-cursor?:bool <- equal old-before-cursor, prev
235 break-unless at-cursor?
236 *editor <- put *editor, cursor-row:offset, row
237 *editor <- put *editor, cursor-column:offset, column
238 *editor <- put *editor, before-cursor:offset, old-before-cursor
239 }
240 c:char <- get *curr, value:offset
241 <character-c-received>
242 {
243
244 newline?:bool <- equal c, 10/newline
245 break-unless newline?
246
247 clear-line-until screen, right
248
249 row <- add row, 1
250 column <- copy left
251 screen <- move-cursor screen, row, column
252 curr <- next curr
253 prev <- next prev
254 loop +next-character
255 }
256 {
257
258
259 at-right?:bool <- equal column, right
260 break-unless at-right?
261
262 wrap-icon:char <- copy 8617/loop-back-to-left
263 print screen, wrap-icon, 245/grey
264 column <- copy left
265 row <- add row, 1
266 screen <- move-cursor screen, row, column
267
268 loop +next-character
269 }
270 print screen, c, color
271 curr <- next curr
272 prev <- next prev
273 column <- add column, 1
274 loop
275 }
276
277 *editor <- put *editor, bottom-of-screen:offset, curr
278 *editor <- put *editor, bottom:offset, row
279 return row, column
280 ]
281
282 scenario point-at-multiple-editors [
283 local-scope
284 trace-until 100/app
285 assume-screen 30/width, 5/height
286
287 assume-resources [
288 [lesson/recipes.mu] <- [
289 |abc|
290 ]
291 ]
292 env:&:environment <- new-programming-environment resources, screen, [def]
293
294 assume-console [
295 left-click 1, 1
296 left-click 1, 17
297 ]
298
299 run [
300 event-loop screen, console, env, resources
301 recipes:&:editor <- get *env, recipes:offset
302 5:num/raw <- get *recipes, cursor-column:offset
303 sandbox:&:editor <- get *env, current-sandbox:offset
304 7:num/raw <- get *sandbox, cursor-column:offset
305 ]
306 memory-should-contain [
307 5 <- 1
308 7 <- 17
309 ]
310 ]
311
312 scenario edit-multiple-editors [
313 local-scope
314 trace-until 100/app
315 assume-screen 30/width, 5/height
316
317 assume-resources [
318 [lesson/recipes.mu] <- [
319 |abc|
320 ]
321 ]
322 env:&:environment <- new-programming-environment resources, screen, [def]
323 render-all screen, env, render
324
325 assume-console [
326 left-click 1, 1
327 type [0]
328 left-click 1, 17
329 type [1]
330 ]
331 run [
332 event-loop screen, console, env, resources
333 recipes:&:editor <- get *env, recipes:offset
334 5:num/raw <- get *recipes, cursor-column:offset
335 sandbox:&:editor <- get *env, current-sandbox:offset
336 7:num/raw <- get *sandbox, cursor-column:offset
337 ]
338 screen-should-contain [
339 . run (F4) . # this line has a different background, but we don't test that yet
340 .a0bc ╎d1ef .
341 . ╎──────────────.
342 .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
343 . ╎ .
344 ]
345 memory-should-contain [
346 5 <- 2
347 7 <- 18
348 ]
349
350 run [
351 cursor:char <- copy 9251/␣
352 print screen, cursor
353 ]
354 screen-should-contain [
355 . run (F4) .
356 .a0bc ╎d1␣f .
357 . ╎──────────────.
358 .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
359 . ╎ .
360 ]
361 ]
362
363 scenario editor-in-focus-keeps-cursor [
364 local-scope
365 trace-until 100/app
366 assume-screen 30/width, 5/height
367 assume-resources [
368 [lesson/recipes.mu] <- [
369 |abc|
370 ]
371 ]
372 env:&:environment <- new-programming-environment resources, screen, [def]
373 render-all screen, env, render
374
375 assume-console []
376 run [
377 event-loop screen, console, env, resources
378 cursor:char <- copy 9251/␣
379 print screen, cursor
380 ]
381
382 screen-should-contain [
383 . run (F4) .
384 .␣bc ╎def .
385 . ╎──────────────.
386 .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
387 . ╎ .
388 ]
389
390 assume-console [
391 type [z]
392 ]
393 run [
394 event-loop screen, console, env, resources
395 cursor:char <- copy 9251/␣
396 print screen, cursor
397 ]
398
399 screen-should-contain [
400 . run (F4) .
401 .z␣bc ╎def .
402 . ╎──────────────.
403 .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
404 . ╎ .
405 ]
406 ]
407
408 scenario backspace-in-sandbox-editor-joins-lines [
409 local-scope
410 trace-until 100/app
411 assume-screen 30/width, 5/height
412 assume-resources [
413 ]
414
415 test-sandbox-editor-contents:text <- new [abc
416 def]
417 env:&:environment <- new-programming-environment resources, screen, test-sandbox-editor-contents
418 render-all screen, env, render
419 screen-should-contain [
420 . run (F4) .
421 . ╎abc .
422 .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎def .
423 . ╎──────────────.
424 . ╎ .
425 ]
426
427 assume-console [
428 left-click 2, 16
429 press backspace
430 ]
431 run [
432 event-loop screen, console, env, resources
433 cursor:char <- copy 9251/␣
434 print screen, cursor
435 ]
436
437 screen-should-contain [
438 . run (F4) .
439 . ╎abc␣ef .
440 .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎──────────────.
441 . ╎ .
442 ]
443 ]
444
445 def render-all screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
446 local-scope
447 load-ingredients
448 trace 10, [app], [render all]
449 hide-screen screen
450
451 trace 11, [app], [render top menu]
452 width:num <- screen-width screen
453 draw-horizontal screen, 0, 0/left, width, 32/space, 0/black, 238/grey
454 button-start:num <- subtract width, 20
455 button-on-screen?:bool <- greater-or-equal button-start, 0
456 assert button-on-screen?, [screen too narrow for menu]
457 screen <- move-cursor screen, 0/row, button-start
458 print screen, [ run (F4) ], 255/white, 161/reddish
459
460 trace 11, [app], [render divider]
461 divider:num, _ <- divide-with-remainder width, 2
462 height:num <- screen-height screen
463 draw-vertical screen, divider, 1/top, height, 9482/vertical-dotted
464
465 screen <- render-recipes screen, env, render-editor
466 screen <- render-sandbox-side screen, env, render-editor
467 <render-components-end>
468
469 recipes:&:editor <- get *env, recipes:offset
470 current-sandbox:&:editor <- get *env, current-sandbox:offset
471 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
472 screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
473
474 show-screen screen
475 ]
476
477 def render-recipes screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
478 local-scope
479 load-ingredients
480 trace 11, [app], [render recipes]
481 recipes:&:editor <- get *env, recipes:offset
482
483 left:num <- get *recipes, left:offset
484 right:num <- get *recipes, right:offset
485 row:num, column:num, screen <- call render-editor, screen, recipes
486 clear-line-until screen, right
487 row <- add row, 1
488 <render-recipe-components-end>
489
490 draw-horizontal screen, row, left, right, 9480/horizontal-dotted
491 row <- add row, 1
492 clear-screen-from screen, row, left, left, right
493 ]
494
495
496 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 [
497 local-scope
498 load-ingredients
499 current-sandbox:&:editor <- get *env, current-sandbox:offset
500 left:num <- get *current-sandbox, left:offset
501 right:num <- get *current-sandbox, right:offset
502 row:num, column:num, screen, current-sandbox <- call render-editor, screen, current-sandbox
503 clear-line-until screen, right
504 row <- add row, 1
505
506 draw-horizontal screen, row, left, right
507 row <- add row, 1
508 clear-screen-from screen, row, left, left, right
509 ]
510
511 def update-cursor screen:&:screen, recipes:&:editor, current-sandbox:&:editor, sandbox-in-focus?:bool, env:&:environment -> screen:&:screen [
512 local-scope
513 load-ingredients
514 <update-cursor-special-cases>
515 {
516 break-if sandbox-in-focus?
517 cursor-row:num <- get *recipes, cursor-row:offset
518 cursor-column:num <- get *recipes, cursor-column:offset
519 }
520 {
521 break-unless sandbox-in-focus?
522 cursor-row:num <- get *current-sandbox, cursor-row:offset
523 cursor-column:num <- get *current-sandbox, cursor-column:offset
524 }
525 screen <- move-cursor screen, cursor-row, cursor-column
526 ]
527
528
529 def render-code screen:&:screen, s:text, left:num, right:num, row:num -> row:num, screen:&:screen [
530 local-scope
531 load-ingredients
532 return-unless s
533 color:num <- copy 7/white
534 column:num <- copy left
535 screen <- move-cursor screen, row, column
536 screen-height:num <- screen-height screen
537 i:num <- copy 0
538 len:num <- length *s
539 {
540 +next-character
541 done?:bool <- greater-or-equal i, len
542 break-if done?
543 done? <- greater-or-equal row, screen-height
544 break-if done?
545 c:char <- index *s, i
546 <character-c-received>
547 {
548
549 at-right?:bool <- equal column, right
550 break-unless at-right?
551
552 wrap-icon:char <- copy 8617/loop-back-to-left
553 print screen, wrap-icon, 245/grey
554 column <- copy left
555 row <- add row, 1
556 screen <- move-cursor screen, row, column
557 loop +next-character
558 }
559 i <- add i, 1
560 {
561
562 newline?:bool <- equal c, 10/newline
563 break-unless newline?
564
565 {
566 done?:bool <- greater-than column, right
567 break-if done?
568 space:char <- copy 32/space
569 print screen, space
570 column <- add column, 1
571 loop
572 }
573 row <- add row, 1
574 column <- copy left
575 screen <- move-cursor screen, row, column
576 loop +next-character
577 }
578 print screen, c, color
579 column <- add column, 1
580 loop
581 }
582 was-at-left?:bool <- equal column, left
583 clear-line-until screen, right
584 {
585 break-if was-at-left?
586 row <- add row, 1
587 }
588 move-cursor screen, row, left
589 ]
590
591
592
593 after <global-type> [
594 {
595 redraw-screen?:bool <- equal c, 12/ctrl-l
596 break-unless redraw-screen?
597 screen <- render-all screen, env:&:environment, render
598 sync-screen screen
599 loop +next-event
600 }
601 ]
602
603
604
605
606 after <global-type> [
607 {
608 switch-side?:bool <- equal c, 14/ctrl-n
609 break-unless switch-side?
610 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
611 sandbox-in-focus? <- not sandbox-in-focus?
612 *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
613 screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
614 loop +next-event
615 }
616 ]
617
618
619
620 def draw-vertical screen:&:screen, col:num, y:num, bottom:num -> screen:&:screen [
621 local-scope
622 load-ingredients
623 style:char, style-found?:bool <- next-ingredient
624 {
625 break-if style-found?
626 style <- copy 9474/vertical
627 }
628 color:num, color-found?:bool <- next-ingredient
629 {
630
631 break-if color-found?
632 color <- copy 245/grey
633 }
634 {
635 continue?:bool <- lesser-than y, bottom
636 break-unless continue?
637 screen <- move-cursor screen, y, col
638 print screen, style, color
639 y <- add y, 1
640 loop
641 }
642 ]