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   # run it
 17   assume-console [
 18     press F4
 19   ]
 20   event-loop screen, console, env, resources
 21   screen-should-contain [
 22     .                                                                                 run (F4)           .
 23     .recipe foo [                                      ╎                                                 .
 24     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 25     .]                                                 ╎0   edit          copy            delete         .
 26     .                                                  ╎foo                                              .
 27     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 28     .                                                  ╎                                                 .
 29   ]
 30   # click on the code in the sandbox
 31   assume-console [
 32     left-click 4, 51
 33   ]
 34   run [
 35     event-loop screen, console, env, resources
 36     cursor:char <- copy 9251/␣
 37     print screen, cursor
 38   ]
 39   # trace now printed and cursor shouldn't have budged
 40   screen-should-contain [
 41     .                                                                                 run (F4)           .
 42     .␣ecipe foo [                                      ╎                                                 .
 43     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 44     .]                                                 ╎0   edit          copy            delete         .
 45     .                                                  ╎foo                                              .
 46     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎abc                                              .
 47   ]
 48   screen-should-contain-in-color 245/grey, [
 49     .                                                                                                    .
 50     .                                                  ╎                                                 .
 51     .                                                  ╎─────────────────────────────────────────────────.
 52     .                                                  ╎                                                 .
 53     .                                                  ╎                                                 .
 54     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎abc                                              .
 55   ]
 56   # click again on the same region
 57   assume-console [
 58     left-click 4, 55
 59   ]
 60   run [
 61     event-loop screen, console, env, resources
 62     print screen, cursor
 63   ]
 64   # trace hidden again
 65   screen-should-contain [
 66     .                                                                                 run (F4)           .
 67     .␣ecipe foo [                                      ╎                                                 .
 68     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 69     .]                                                 ╎0   edit          copy            delete         .
 70     .                                                  ╎foo                                              .
 71     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 72     .                                                  ╎                                                 .
 73   ]
 74 ]
 75 
 76 scenario sandbox-shows-app-trace-and-result [
 77   local-scope
 78   trace-until 100/app  # trace too long
 79   assume-screen 100/width, 10/height
 80   # basic recipe
 81   assume-resources [
 82     [lesson/recipes.mu] <- [
 83       |recipe foo [|
 84       |  stash [abc]|
 85       |  reply 4|
 86       |]|
 87     ]
 88   ]
 89   env:&:environment <- new-programming-environment resources, screen, [foo]
 90   # run it
 91   assume-console [
 92     press F4
 93   ]
 94   event-loop screen, console, env, resources
 95   screen-should-contain [
 96     .                                                                                 run (F4)           .
 97     .recipe foo [                                      ╎                                                 .
 98     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
 99     .  reply 4                                         ╎0   edit          copy            delete         .
100     .]                                                 ╎foo                                              .
101     .                                                  ╎4                                                .
102     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
103     .                                                  ╎                                                 .
104   ]
105   # click on the code in the sandbox
106   assume-console [
107     left-click 4, 51
108   ]
109   run [
110     event-loop screen, console, env, resources
111   ]
112   # trace now printed above result
113   screen-should-contain [
114     .                                                                                 run (F4)           .
115     .recipe foo [                                      ╎                                                 .
116     .  stash [abc]                                     ╎─────────────────────────────────────────────────.
117     .  reply 4                                         ╎0   edit          copy            delete         .
118     .]                                                 ╎foo                                              .
119     .                                                  ╎abc                                              .
120     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎8 instructions run                               .
121     .                                                  ╎4                                                .
122     .                                                  ╎─────────────────────────────────────────────────.
123     .                                                  ╎                                                 .
124   ]
125 ]
126 
127 scenario clicking-on-app-trace-does-nothing [
128   local-scope
129   trace-until 100/app  # trace too long
130   assume-screen 100/width, 10/height
131   assume-resources [
132   ]
133   env:&:environment <- new-programming-environment resources, screen, [stash 123456789]
134   # create and expand the trace
135   assume-console [
136     press F4
137     left-click 4, 51
138   ]
139   event-loop screen, console, env, resources
140   screen-should-contain [
141     .                                                                                 run (F4)           .
142     .                                                  ╎                                                 .
143     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
144     .                                                  ╎0   edit          copy            delete         .
145     .                                                  ╎stash 123456789                                  .
146     .                                                  ╎123456789                                        .
147   ]
148   # click on the stash under the edit-button region (or any of the other buttons, really)
149   assume-console [
150     left-click 5, 57
151   ]
152   run [
153     event-loop screen, console, env, resources
154   ]
155   # no change; doesn't die
156   screen-should-contain [
157     .                                                                                 run (F4)           .
158     .                                                  ╎                                                 .
159     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
160     .                                                  ╎0   edit          copy            delete         .
161     .                                                  ╎stash 123456789                                  .
162     .                                                  ╎123456789                                        .
163   ]
164 ]
165 
166 container sandbox [
167   trace:text
168   display-trace?:bool
169 ]
170 
171 # replaced in a later layer
172 def! update-sandbox sandbox:&:sandbox, env:&:environment, idx:num -> sandbox:&:sandbox, env:&:environment [
173   local-scope
174   load-ingredients
175   data:text <- get *sandbox, data:offset
176   response:text, _, fake-screen:&:screen, trace:text <- run-sandboxed data
177   *sandbox <- put *sandbox, response:offset, response
178   *sandbox <- put *sandbox, screen:offset, fake-screen
179   *sandbox <- put *sandbox, trace:offset, trace
180 ]
181 
182 # clicks on sandbox code toggle its display-trace? flag
183 after <global-touch> [
184   # check if it's inside the code of any sandbox
185   {
186     sandbox-left-margin:num <- get *current-sandbox, left:offset
187     click-column:num <- get t, column:offset
188     on-sandbox-side?:bool <- greater-or-equal click-column, sandbox-left-margin
189     break-unless on-sandbox-side?
190     first-sandbox:&:sandbox <- get *env, sandbox:offset
191     break-unless first-sandbox
192     first-sandbox-begins:num <- get *first-sandbox, starting-row-on-screen:offset
193     click-row:num <- get t, row:offset
194     below-sandbox-editor?:bool <- greater-or-equal click-row, first-sandbox-begins
195     break-unless below-sandbox-editor?
196     # identify the sandbox whose code is being clicked on
197     sandbox:&:sandbox <- find-click-in-sandbox-code env, click-row
198     break-unless sandbox
199     # toggle its display-trace? property
200     x:bool <- get *sandbox, display-trace?:offset
201     x <- not x
202     *sandbox <- put *sandbox, display-trace?:offset, x
203     hide-screen screen
204     screen <- render-sandbox-side screen, env, render
205     screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
206     # no change in cursor
207     show-screen screen
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-ingredients
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 0/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 ]