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