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