1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# Editor widget: takes a string and screen coordinates, and returns a new string.
recipe main [
default-space:address:array:location <- new location:type, 30:literal
switch-to-display
width:number <- display-width
height:number <- display-height
divider:number, _ <- divide-with-remainder width:number, 2:literal
draw-vertical 0:literal/screen, divider:number, 0:literal/top, height:number
# shorten bottom border and darken to make it seem thinner
border-left:number <- multiply divider:number, 0.2
border-right:number <- multiply divider:number, 0.8
draw-horizontal 0:literal/screen, 5:literal/row, border-left:number, border-right:number, 241:literal/grey
in:address:array:character <- new [abcdef]
edit in:address:array:character, 0:literal/screen, 0:literal/top, 0:literal/left, 5:literal/bottom, divider:number/right, 0:literal/keyboard
wait-for-key-from-keyboard
return-to-console
]
scenario edit-prints-string-to-screen [
assume-screen 10:literal/width, 5:literal/height
assume-keyboard []
run [
s:address:array:character <- new [abc]
s2:address:array:character, screen:address, keyboard:address <- edit s:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/bottom, 5:literal/right, keyboard:address
]
screen-should-contain [
.abc .
. .
]
]
recipe edit [
default-space:address:array:location <- new location:type, 30:literal
s:address:array:character <- next-ingredient
screen:address <- next-ingredient
# no clipping of bounds
top:number <- next-ingredient
left:number <- next-ingredient
bottom:number <- next-ingredient
bottom:number <- subtract bottom:number, 1:literal
right:number <- next-ingredient
right:number <- subtract right:number, 1:literal
keyboard:address <- next-ingredient
render s:address:array:character, screen:address, top:number, left:number, bottom:number, right:number
]
recipe render [
default-space:address:array:location <- new location:type, 30:literal
s:address:array:character <- next-ingredient
screen:address <- next-ingredient
top:number <- next-ingredient
left:number <- next-ingredient
bottom:number <- next-ingredient
right:number <- next-ingredient
# traversing inside s
len:number <- length s:address:array:character/deref
i:number <- copy 0:literal
# traversing inside screen
row:number <- copy top:number
column:number <- copy left:number
move-cursor screen:address, row:number, column:number
{
+next-character
done?:boolean <- greater-or-equal i:number, len:number
break-if done?:boolean
off-screen?:boolean <- greater-than row:number, bottom:number
break-if off-screen?:boolean
c:character <- index s:address:array:character/deref, i:number
{
# newline? move to left rather than 0
newline?:boolean <- equal c:character, 10:literal/newline
break-unless newline?:boolean
row:number <- add row:number, 1:literal
column:number <- copy left:number
move-cursor screen:address, row:number, column:number
i:number <- add i:number, 1:literal
loop +next-character:label
}
{
# at right? more than one letter left in the line? wrap
at-right?:boolean <- equal column:number, right:number
break-unless at-right?:boolean
next-index:number <- add i:number, 1:literal
next-at-end?:boolean <- greater-or-equal next-index:number, len:number
break-if next-at-end?:boolean
next:character <- index s:address:array:character/deref, next-index:number
next-character-is-newline?:boolean <- equal next:character, 10:literal/newline
break-if next-character-is-newline?:boolean
# wrap
print-character screen:address, 8617:literal/loop-back-to-left, 245:literal/grey
column:number <- copy left:number
row:number <- add row:number, 1:literal
move-cursor screen:address, row:number, column:number
# don't increment i
loop +next-character:label
}
print-character screen:address, c:character
i:number <- add i:number, 1:literal
column:number <- add column:number, 1:literal
loop
}
]
scenario edit-prints-multiple-lines [
assume-screen 5:literal/width, 3:literal/height
assume-keyboard []
run [
s:address:array:character <- new [abc
def]
s2:address:array:character, screen:address, keyboard:address <- edit s:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/bottom, 5:literal/right, keyboard:address
]
screen-should-contain [
.abc .
.def .
. .
]
]
scenario edit-handles-offsets [
assume-screen 5:literal/width, 3:literal/height
assume-keyboard []
run [
s:address:array:character <- new [abc]
s2:address:array:character, screen:address, keyboard:address <- edit s:address:array:character, screen:address, 0:literal/top, 1:literal/left, 10:literal/bottom, 5:literal/right, keyboard:address
]
screen-should-contain [
. abc .
. .
. .
]
]
scenario edit-prints-multiple-lines-at-offset [
assume-screen 5:literal/width, 3:literal/height
assume-keyboard []
run [
s:address:array:character <- new [abc
def]
s2:address:array:character, screen:address, keyboard:address <- edit s:address:array:character, screen:address, 0:literal/top, 1:literal/left, 10:literal/bottom, 5:literal/right, keyboard:address
]
screen-should-contain [
. abc .
. def .
. .
]
]
scenario edit-wraps-long-lines [
assume-screen 5:literal/width, 3:literal/height
assume-keyboard []
run [
s:address:array:character <- new [abc def]
s2:address:array:character, screen:address, keyboard:address <- edit s:address:array:character, screen:address, 0:literal/top, 0:literal/left, 10:literal/bottom, 5:literal/right, keyboard:address
]
screen-should-contain [
.abc ↩.
.def .
. .
]
screen-should-contain-in-color, 245:literal/grey [
. ↩.
. .
. .
]
]
recipe draw-box [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
top:number <- next-ingredient
left:number <- next-ingredient
bottom:number <- next-ingredient
right:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
# top border
draw-horizontal screen:address, top:number, left:number, right:number, color:number
draw-horizontal screen:address, bottom:number, left:number, right:number, color:number
draw-vertical screen:address, left:number, top:number, bottom:number, color:number
draw-vertical screen:address, right:number, top:number, bottom:number, color:number
draw-top-left screen:address, top:number, left:number, color:number
draw-top-right screen:address, top:number, right:number, color:number
draw-bottom-left screen:address, bottom:number, left:number, color:number
draw-bottom-right screen:address, bottom:number, right:number, color:number
# position cursor inside box
move-cursor screen:address, top:number, left:number
cursor-down screen:address
cursor-right screen:address
]
recipe draw-horizontal [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
row:number <- next-ingredient
x:number <- next-ingredient
right:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
move-cursor screen:address, row:number, x:number
{
continue?:boolean <- lesser-than x:number, right:number
break-unless continue?:boolean
print-character screen:address, 9472:literal/horizontal, color:number
x:number <- add x:number, 1:literal
loop
}
]
recipe draw-vertical [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
col:number <- next-ingredient
x:number <- next-ingredient
bottom:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
{
continue?:boolean <- lesser-than x:number, bottom:number
break-unless continue?:boolean
move-cursor screen:address, x:number, col:number
print-character screen:address, 9474:literal/vertical, color:number
x:number <- add x:number, 1:literal
loop
}
]
recipe draw-top-left [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
top:number <- next-ingredient
left:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
move-cursor screen:address, top:number, left:number
print-character screen:address, 9484:literal/down-right, color:number
]
recipe draw-top-right [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
top:number <- next-ingredient
right:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
move-cursor screen:address, top:number, right:number
print-character screen:address, 9488:literal/down-left, color:number
]
recipe draw-bottom-left [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
bottom:number <- next-ingredient
left:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
move-cursor screen:address, bottom:number, left:number
print-character screen:address, 9492:literal/up-right, color:number
]
recipe draw-bottom-right [
default-space:address:array:location <- new location:type, 30:literal
screen:address <- next-ingredient
bottom:number <- next-ingredient
right:number <- next-ingredient
color:number, color-found?:boolean <- next-ingredient
{
# default color to white
break-if color-found?:boolean
color:number <- copy 245:literal/grey
}
move-cursor screen:address, bottom:number, right:number
print-character screen:address, 9496:literal/up-left, color:number
]
|