https://github.com/akkartik/mu1/blob/master/edit/007-sandbox-delete.mu
  1 ## deleting sandboxes
  2 
  3 scenario deleting-sandboxes [
  4   local-scope
  5   trace-until 100/app  # trace too long
  6   assume-screen 100/width, 15/height
  7   assume-resources [
  8   ]
  9   env:&:environment <- new-programming-environment resources, screen, []
 10   render-all screen, env, render
 11   # run a few commands
 12   assume-console [
 13     left-click 1, 75
 14     type [divide-with-remainder 11, 3]
 15     press F4
 16     type [add 2, 2]
 17     press F4
 18   ]
 19   event-loop screen, console, env, resources
 20   screen-should-contain [
 21     .                                                                                 run (F4)           .
 22     .                                                  ╎                                                 .
 23     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 24     .                                                  ╎0   edit       copy       to recipe    delete    .
 25     .                                                  ╎add 2, 2                                         .
 26     .                                                  ╎4                                                .
 27     .                                                  ╎─────────────────────────────────────────────────.
 28     .                                                  ╎1   edit       copy       to recipe    delete    .
 29     .                                                  ╎divide-with-remainder 11, 3                      .
 30     .                                                  ╎3                                                .
 31     .                                                  ╎2                                                .
 32     .                                                  ╎─────────────────────────────────────────────────.
 33     .                                                  ╎                                                 .
 34   ]
 35   # delete second sandbox by clicking on left edge of 'delete' button
 36   assume-console [
 37     left-click 7, 90
 38   ]
 39   run [
 40     event-loop screen, console, env, resources
 41   ]
 42   screen-should-contain [
 43     .                                                                                 run (F4)           .
 44     .                                                  ╎                                                 .
 45     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 46     .                                                  ╎0   edit       copy       to recipe    delete    .
 47     .                                                  ╎add 2, 2                                         .
 48     .                                                  ╎4                                                .
 49     .                                                  ╎─────────────────────────────────────────────────.
 50     .                                                  ╎                                                 .
 51     .                                                  ╎                                                 .
 52   ]
 53   # delete first sandbox by clicking at right edge of 'delete' button
 54   assume-console [
 55     left-click 3, 99
 56   ]
 57   run [
 58     event-loop screen, console, env, resources
 59   ]
 60   screen-should-contain [
 61     .                                                                                 run (F4)           .
 62     .                                                  ╎                                                 .
 63     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 64     .                                                  ╎                                                 .
 65     .                                                  ╎                                                 .
 66   ]
 67 ]
 68 
 69 after <global-touch> [
 70   # support 'delete' button
 71   {
 72     delete?:bool <- should-attempt-delete? click-row, click-column, env
 73     break-unless delete?
 74     delete?, env <- try-delete-sandbox click-row, env
 75     break-unless delete?
 76     screen <- render-sandbox-side screen, env, render
 77     screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
 78     loop +next-event
 79   }
 80 ]
 81 
 82 # some preconditions for attempting to delete a sandbox
 83 def should-attempt-delete? click-row:num, click-column:num, env:&:environment -> result:bool [
 84   local-scope
 85   load-inputs
 86   # are we below the sandbox editor?
 87   click-sandbox-area?:bool <- click-on-sandbox-area? click-row, click-column, env
 88   return-unless click-sandbox-area?, false
 89   # narrower, is the click in the columns spanning the 'copy' button?
 90   first-sandbox:&:editor <- get *env, current-sandbox:offset
 91   assert first-sandbox, [!!]
 92   sandbox-left-margin:num <- get *first-sandbox, left:offset
 93   sandbox-right-margin:num <- get *first-sandbox, right:offset
 94   _, _, _, _, _, _, delete-button-left:num <- sandbox-menu-columns sandbox-left-margin, sandbox-right-margin
 95   result <- within-range? click-column, delete-button-left, sandbox-right-margin
 96 ]
 97 
 98 def try-delete-sandbox click-row:num, env:&:environment -> clicked-on-delete-button?:bool, env:&:environment [
 99   local-scope
100   load-inputs
101   # identify the sandbox to delete, if the click was actually on the 'delete' button
102   sandbox:&:sandbox <- find-sandbox env, click-row
103   return-unless sandbox, false
104   clicked-on-delete-button? <- copy true
105   env <- delete-sandbox env, sandbox
106 ]
107 
108 def delete-sandbox env:&:environment, sandbox:&:sandbox -> env:&:environment [
109   local-scope
110   load-inputs
111   curr-sandbox:&:sandbox <- get *env, sandbox:offset
112   first-sandbox?:bool <- equal curr-sandbox, sandbox
113   {
114     # first sandbox? pop
115     break-unless first-sandbox?
116     next-sandbox:&:sandbox <- get *curr-sandbox, next-sandbox:offset
117     *env <- put *env, sandbox:offset, next-sandbox
118   }
119   {
120     # not first sandbox?
121     break-if first-sandbox?
122     prev-sandbox:&:sandbox <- copy curr-sandbox
123     curr-sandbox <- get *curr-sandbox, next-sandbox:offset
124     {
125       assert curr-sandbox, [sandbox not found! something is wrong.]
126       found?:bool <- equal curr-sandbox, sandbox
127       break-if found?
128       prev-sandbox <- copy curr-sandbox
129       curr-sandbox <- get *curr-sandbox, next-sandbox:offset
130       loop
131     }
132     # snip sandbox out of its list
133     next-sandbox:&:sandbox <- get *curr-sandbox, next-sandbox:offset
134     *prev-sandbox <- put *prev-sandbox, next-sandbox:offset, next-sandbox
135   }
136   # update sandbox count
137   sandbox-count:num <- get *env, number-of-sandboxes:offset
138   sandbox-count <- subtract sandbox-count, 1
139   *env <- put *env, number-of-sandboxes:offset, sandbox-count
140   # reset scroll if deleted sandbox was last
141   {
142     break-if next-sandbox
143     render-from:num <- get *env, render-from:offset
144     reset-scroll?:bool <- equal render-from, sandbox-count
145     break-unless reset-scroll?
146     *env <- put *env, render-from:offset, -1
147   }
148 ]
149 
150 scenario deleting-sandbox-after-scroll [
151   local-scope
152   trace-until 100/app  # trace too long
153   assume-screen 100/width, 10/height
154   # initialize environment
155   assume-resources [
156   ]
157   env:&:environment <- new-programming-environment resources, screen, []
158   render-all screen, env, render
159   # create 2 sandboxes and scroll to second
160   assume-console [
161     press ctrl-n
162     type [add 2, 2]
163     press F4
164     type [add 1, 1]
165     press F4
166     press page-down
167   ]
168   event-loop screen, console, env, resources
169   screen-should-contain [
170     .                                                                                 run (F4)           .
171     .                                                  ╎─────────────────────────────────────────────────.
172     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎0   edit       copy       to recipe    delete    .
173     .                                                  ╎add 1, 1                                         .
174     .                                                  ╎2                                                .
175     .                                                  ╎─────────────────────────────────────────────────.
176     .                                                  ╎1   edit       copy       to recipe    delete    .
177   ]
178   # delete the second sandbox
179   assume-console [
180     left-click 6, 99
181   ]
182   run [
183     event-loop screen, console, env, resources
184   ]
185   # second sandbox shows in editor; scroll resets to display first sandbox
186   screen-should-contain [
187     .                                                                                 run (F4)           .
188     .                                                  ╎─────────────────────────────────────────────────.
189     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎0   edit       copy       to recipe    delete    .
190     .                                                  ╎add 1, 1                                         .
191     .                                                  ╎2                                                .
192     .                                                  ╎─────────────────────────────────────────────────.
193     .                                                  ╎                                                 .
194   ]
195 ]
196 
197 scenario deleting-top-sandbox-after-scroll [
198   local-scope
199   trace-until 100/app  # trace too long
200   assume-screen 100/width, 10/height
201   # initialize environment
202   assume-resources [
203   ]
204   env:&:environment <- new-programming-environment resources, screen, []
205   render-all screen, env, render
206   # create 2 sandboxes and scroll to second
207   assume-console [
208     press ctrl-n
209     type [add 2, 2]
210     press F4
211     type [add 1, 1]
212     press F4
213     press page-down
214   ]
215   event-loop screen, console, env, resources
216   screen-should-contain [
217     .                                                                                 run (F4)           .
218     .                                                  ╎─────────────────────────────────────────────────.
219     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎0   edit       copy       to recipe    delete    .
220     .                                                  ╎add 1, 1                                         .
221     .                                                  ╎2                                                .
222     .                                                  ╎─────────────────────────────────────────────────.
223     .                                                  ╎1   edit       copy       to recipe    delete    .
224   ]
225   # delete the second sandbox
226   assume-console [
227     left-click 2, 99
228   ]
229   run [
230     event-loop screen, console, env, resources
231   ]
232   # second sandbox shows in editor; scroll resets to display first sandbox
233   screen-should-contain [
234     .                                                                                 run (F4)           .
235     .                                                  ╎─────────────────────────────────────────────────.
236     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎0   edit       copy       to recipe    delete    .
237     .                                                  ╎add 2, 2                                         .
238     .                                                  ╎4                                                .
239     .                                                  ╎─────────────────────────────────────────────────.
240     .                                                  ╎                                                 .
241   ]
242 ]
243 
244 scenario deleting-final-sandbox-after-scroll [
245   local-scope
246   trace-until 100/app  # trace too long
247   assume-screen 100/width, 10/height
248   # initialize environment
249   assume-resources [
250   ]
251   env:&:environment <- new-programming-environment resources, screen, []
252   render-all screen, env, render
253   # create 2 sandboxes and scroll to second
254   assume-console [
255     press ctrl-n
256     type [add 2, 2]
257     press F4
258     type [add 1, 1]
259     press F4
260     press page-down
261     press page-down
262   ]
263   event-loop screen, console, env, resources
264   screen-should-contain [
265     .                                                                                 run (F4)           .
266     .                                                  ╎─────────────────────────────────────────────────.
267     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎1   edit       copy       to recipe    delete    .
268     .                                                  ╎add 2, 2                                         .
269     .                                                  ╎4                                                .
270     .                                                  ╎─────────────────────────────────────────────────.
271     .                                                  ╎                                                 .
272   ]
273   # delete the second sandbox
274   assume-console [
275     left-click 2, 99
276   ]
277   run [
278     event-loop screen, console, env, resources
279   ]
280   # implicitly scroll up to first sandbox
281   screen-should-contain [
282     .                                                                                 run (F4)           .
283     .                                                  ╎                                                 .
284     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
285     .                                                  ╎0   edit       copy       to recipe    delete    .
286     .                                                  ╎add 1, 1                                         .
287     .                                                  ╎2                                                .
288     .                                                  ╎─────────────────────────────────────────────────.
289     .                                                  ╎                                                 .
290   ]
291 ]
292 
293 scenario deleting-updates-sandbox-count [
294   local-scope
295   trace-until 100/app  # trace too long
296   assume-screen 100/width, 10/height
297   # initialize environment
298   assume-resources [
299   ]
300   env:&:environment <- new-programming-environment resources, screen, []
301   render-all screen, env, render
302   # create 2 sandboxes
303   assume-console [
304     press ctrl-n
305     type [add 2, 2]
306     press F4
307     type [add 1, 1]
308     press F4
309   ]
310   event-loop screen, console, env, resources
311   screen-should-contain [
312     .                                                                                 run (F4)           .
313     .                                                  ╎                                                 .
314     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
315     .                                                  ╎0   edit       copy       to recipe    delete    .
316     .                                                  ╎add 1, 1                                         .
317     .                                                  ╎2                                                .
318     .                                                  ╎─────────────────────────────────────────────────.
319     .                                                  ╎1   edit       copy       to recipe    delete    .
320     .                                                  ╎add 2, 2                                         .
321     .                                                  ╎4                                                .
322   ]
323   # delete the second sandbox, then try to scroll down twice
324   assume-console [
325     left-click 3, 99
326     press page-down
327     press page-down
328   ]
329   run [
330     event-loop screen, console, env, resources
331   ]
332   # shouldn't go past last sandbox
333   screen-should-contain [
334     .                                                                                 run (F4)           .
335     .                                                  ╎─────────────────────────────────────────────────.
336     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎0   edit       copy       to recipe    delete    .
337     .                                                  ╎add 2, 2                                         .
338     .                                                  ╎4                                                .
339     .                                                  ╎─────────────────────────────────────────────────.
340     .                                                  ╎                                                 .
341   ]
342 ]