https://github.com/akkartik/mu/blob/master/edit/010-sandbox-trace.mu
  1 ## clicking on the code typed into a sandbox toggles its trace
  2 
  3 scenario sandbox-click-on-code-toggles-app-trace [
  4   local-scope
  5   trace-until 100/app  # trace too long
  6   assume-screen 100/width, 10/height
  7   # basic recipe
  8   assume-resources [
  9     [lesson/recipes.mu] <- [
 10       |recipe foo [|
 11       |  stash [abc]|
 12       |]|
 13     ]
 14   ]
 15   env:&:environment <- new-programming-environment resources, screen, [foo]
 16   render-all screen, env, render
 17   # run it
 18   assume-console [
 19     press F4
 20   ]
 21   event-loop screen, console, env, resources
 22   screen-should-contain [
 23     .                                                                                 run (F4)           .
 24     .recipe foo [                                      ╎                                                 .
 25     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 26     .]                                                 ╎0   edit       copy       to recipe    delete    .
 27     .                                                  ╎foo                                              .
 28     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 29     .                                                  ╎                                                 .
 30   ]
 31   # click on the code in the sandbox
 32   assume-console [
 33     left-click 4, 51
 34   ]
 35   run [
 36     event-loop screen, console, env, resources
 37     cursor:char <- copy 9251/␣
 38     print screen, cursor
 39   ]
 40   # trace now printed and cursor shouldn't have budged
 41   screen-should-contain [
 42     .                                                                                 run (F4)           .
 43     .␣ecipe foo [                                      ╎                                                 .
 44     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 45     .]                                                 ╎0   edit       copy       to recipe    delete    .
 46     .                                                  ╎foo                                              .
 47     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎abc                                              .
 48   ]
 49   screen-should-contain-in-color 245/grey, [
 50     .                                                                                                    .
 51     .                                                  ╎                                                 .
 52     .                                                  ╎─────────────────────────────────────────────────.
 53     .                                                  ╎                                                 .
 54     .                                                  ╎                                                 .
 55     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎abc                                              .
 56   ]
 57   # click again on the same region
 58   assume-console [
 59     left-click 4, 55
 60   ]
 61   run [
 62     event-loop screen, console, env, resources
 63     print screen, cursor
 64   ]
 65   # trace hidden again
 66   screen-should-contain [
 67     .                                                                                 run (F4)           .
 68     .␣ecipe foo [                                      ╎                                                 .
 69     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 70     .]                                                 ╎0   edit       copy       to recipe    delete    .
 71     .                                                  ╎foo                                              .
 72     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 73     .                                                  ╎                                                 .
 74   ]
 75 ]
 76 
 77 scenario sandbox-shows-app-trace-and-result [
 78   local-scope
 79   trace-until 100/app  # trace too long
 80   assume-screen 100/width, 10/height
 81   # basic recipe
 82   assume-resources [
 83     [lesson/recipes.mu] <- [
 84       |recipe foo [|
 85       |  stash [abc]|
 86       |  reply 4|
 87       |]|
 88     ]
 89   ]
 90   env:&:environment <- new-programming-environment resources, screen, [foo]
 91   render-all screen, env, render
 92   # run it
 93   assume-console [
 94     press F4
 95   ]
 96   event-loop screen, console, env, resources
 97   screen-should-contain [
 98     .                                                                                 run (F4)           .
 99     .recipe foo [                                      ╎                                                 .
100     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
101     .  reply 4                                         ╎0   edit       copy       to recipe    delete    .
102     .]                                                 ╎foo                                              .
103     .                                                  ╎4                                                .
104     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
105     .                                                  ╎                                                 .
106   ]
107   # click on the code in the sandbox
108   assume-console [
109     left-click 4, 51
110   ]
111   run [
112     event-loop screen, console, env, resources
113   ]
114   # trace now printed above result
115   screen-should-contain [
116     .                                                                                 run (F4)           .
117     .recipe foo [                                      ╎                                                 .
118     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
119     .  reply 4                                         ╎0   edit       copy       to recipe    delete    .
120     .]                                                 ╎foo                                              .
121     .                                                  ╎abc                                              .
122     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎8 instructions run                               .
123     .                                                  ╎4                                                .
124     .                                                  ╎─────────────────────────────────────────────────.
125     .                                                  ╎                                                 .
126   ]
127 ]
128 
129 scenario clicking-on-app-trace-does-nothing [
130   local-scope
131   trace-until 100/app  # trace too long
132   assume-screen 100/width, 10/height
133   assume-resources [
134   ]
135   env:&:environment <- new-programming-environment resources, screen, [stash 123456789]
136   render-all screen, env, render
137   # create and expand the trace
138   assume-console [
139     press F4
140     left-click 4, 51
141   ]
142   event-loop screen, console, env, resources
143   screen-should-contain [
144     .                                                                                 run (F4)           .
145     .                                                  ╎                                                 .
146     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
147     .                                                  ╎0   edit       copy       to recipe    delete    .
148     .                                                  ╎stash 123456789                                  .
149     .                                                  ╎123456789                                        .
150   ]
151   # click on the stash under the edit-button region (or any of the other buttons, really)
152   assume-console [
153     left-click 5, 57
154   ]
155   run [
156     event-loop screen, console, env, resources
157   ]
158   # no change; doesn't die
159   screen-should-contain [
160     .                                                                                 run (F4)           .
161     .                                                  ╎                                                 .
162     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
163     .                                                  ╎0   edit       copy       to recipe    delete    .
164     .                                                  ╎stash 123456789                                  .
165     .                                                  ╎123456789                                        .
166   ]
167 ]
168 
169 container sandbox [
170   trace:text
171   display-trace?:bool
172 ]
173 
174 # replaced in a later layer
175 def! update-sandbox sandbox:&:sandbox, env:&:environment, idx:num -> sandbox:&:sandbox, env:&:environment [
176   local-scope
177   load-inputs
178   data:text <- get *sandbox, data:offset
179   response:text, _, fake-screen:&:screen, trace:text <- run-sandboxed data
180   *sandbox <- put *sandbox, response:offset, response
181   *sandbox <- put *sandbox, screen:offset, fake-screen
182   *sandbox <- put *sandbox, trace:offset, trace
183 ]
184 
185 # clicks on sandbox code toggle its display-trace? flag
186 after <global-touch> [
187   # check if it's inside the code of any sandbox
188   {
189     sandbox-left-margin:num <- get *current-sandbox, left:offset
190     click-column:num <- get t, column:offset
191     on-sandbox-side?:bool <- greater-or-equal click-column, sandbox-left-margin
192     break-unless on-sandbox-side?
193     first-sandbox:&:sandbox <- get *env, sandbox:offset
194     break-unless first-sandbox
195     first-sandbox-begins:num <- get *first-sandbox, starting-row-on-screen:offset
196     click-row:num <- get t, row:offset
197     below-sandbox-editor?:bool <- greater-or-equal click-row, first-sandbox-begins
198     break-unless below-sandbox-editor?
199     # identify the sandbox whose code is being clicked on
200     sandbox:&:sandbox <- find-click-in-sandbox-code env, click-row
201     break-unless sandbox
202     # toggle its display-trace? property
203     x:bool <- get *sandbox, display-trace?:offset
204     x <- not x
205     *sandbox <- put *sandbox, display-trace?:offset, x
206     screen <- render-sandbox-side screen, env, render
207     screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
208     loop +next-event
209   }
210 ]
211 
212 def find-click-in-sandbox-code env:&:environment, click-row:num -> sandbox:&:sandbox [
213   local-scope
214   load-inputs
215   # assert click-row >= sandbox.starting-row-on-screen
216   sandbox <- get *env, sandbox:offset
217   start:num <- get *sandbox, starting-row-on-screen:offset
218   clicked-on-sandboxes?:bool <- greater-or-equal click-row, start
219   assert clicked-on-sandboxes?, [extract-sandbox called on click to sandbox editor]
220   # while click-row < sandbox.next-sandbox.starting-row-on-screen
221   {
222     next-sandbox:&:sandbox <- get *sandbox, next-sandbox:offset
223     break-unless next-sandbox
224     next-start:num <- get *next-sandbox, starting-row-on-screen:offset
225     found?:bool <- lesser-than click-row, next-start
226     break-if found?
227     sandbox <- copy next-sandbox
228     loop
229   }
230   # return sandbox if click is in its code region
231   code-ending-row:num <- get *sandbox, code-ending-row-on-screen:offset
232   click-above-response?:bool <- lesser-than click-row, code-ending-row
233   start:num <- get *sandbox, starting-row-on-screen:offset
234   click-below-menu?:bool <- greater-than click-row, start
235   click-on-sandbox-code?:bool <- and click-above-response?, click-below-menu?
236   {
237     break-if click-on-sandbox-code?
238     return null/no-click-in-sandbox-output
239   }
240   return sandbox
241 ]
242 
243 # when rendering a sandbox, dump its trace before response/warning if display-trace? property is set
244 after <render-sandbox-results> [
245   {
246     display-trace?:bool <- get *sandbox, display-trace?:offset
247     break-unless display-trace?
248     sandbox-trace:text <- get *sandbox, trace:offset
249     break-unless sandbox-trace  # nothing to print; move on
250     row, screen <- render-text screen, sandbox-trace, left, right, 245/grey, row
251   }
252   <render-sandbox-trace-done>
253 ]