-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.finw
1117 lines (952 loc) · 22.6 KB
/
main.finw
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TODO: Finwe: need for dummy values when using while loop with nothing
// on the stack (compiler crashes otherwise)
//
// 0 // dummy value
// (while [ ... ] [ ... ])
// drop // drop dummy value
(use* core)
(use* varvara)
(use std)
(use alloc)
(use vec)
(use net)
(use input)
(use assets)
(struct Command
[func @(Fn (@U8 --))]
[name @U8]
)
(enum Mode U8
norm
sel-link
input
)
(struct Link
[url @U8]
)
(enum LineType U8
[status 0]
[heading 1]
[text 2]
[link 3]
[list 4]
[quote 5]
[pre 6]
)
(struct Line
[type LineType]
[data @Opaque] // TODO: Finwe: untagged unions
[line @U8]
)
(word STARTX (-- U16) [ 16s ])
(word ENDX (-- U16) [ .Screen/width dei 16s - ])
(word STARTY (-- U16) [ 16s ])
(word ENDY (-- U16) [ .Screen/height dei 16s 3s * - ])
(let ui-mode Mode)
(let ui-sel-link @[U8 3])
(let ui-current-font @U8)
(let ui-current-style U8)
(let ui-scroll U16)
(let ui-input input/Ctx) // TODO: Finwe: ensure $ui-input :cursor is optimized to single LDA
(let ui-message [U8 128])
// Per-render state for draw-next-line
// Only reason it's global is because draw-content() needs to reset it
(let ui-cur-line U16)
(let commands [Command 5]) // XXX: ensure to update len in parse-command() also
(word COMMANDS-LEN (-- U16) [ 5s ])
(let conn net/ConnectArgs)
(let history (Of vec/Vector @U8))
(let buf (Of vec/Vector U8))
(let document (Of vec/Vector Line))
(let links (Of vec/Vector Link))
(word main ( -- ) [
[ (--) on-mouse halt ] .Mouse/vector deo
[ (--) on-controller halt ] .Controller/vector deo // TODO: Finwe: bad folding!
.Mode/norm @ui-mode <-
@ui-input ;reset
128s @ui-message std/memzero
alloc/init
4096s @buf ;init-sized
256s @document ;init-sized
64s @links ;init-sized
0xe3b8s .System/r deo
0xe3a7s .System/g deo
0xe3a7s .System/b deo
100s 8s * .Screen/width deo
80s 8s * .Screen/height deo
"gemini://geminiprotocol.net/" make-request
//"gopher://colorfield.space/" make-request
parse-gemtext
draw
// TODO: Finwe: allow quotes to be values; static struct literals in let() bindings
// TODO: Finwe: allow quotes with only one value in them to skip arity
// TODO: Finwe: fix folding issue already and remove 2 drop etc
@commands :0 (as @Command)
[ (@U8 --) 2 drop cmd-version ] over :func <-
"version" over :name <-
drop
@commands :1 (as @Command)
[ (@U8 --) 1 drop cmd-go ] over :func <-
"go" over :name <-
drop
@commands :2 (as @Command)
[ (@U8 --) 3 drop cmd-launch ] over :func <-
"launch" over :name <-
drop
@commands :3 (as @Command)
[ (@U8 --) 4 drop cmd-quit ] over :func <-
"q" over :name <-
drop
@commands :4 (as @Command)
[ (@U8 --) 5 drop cmd-quit ] over :func <-
"wq" over :name <-
drop
])
(word cmd-quit (@U8 -- ) [
drop
// TODO: Finwe: std/exit
0x80 .System/state deo
halt
])
(word cmd-launch (@U8 -- ) [
dup 0s over ->
(while [ 0<> ] [
(as U16)
beor 0x413bs *
dup 3 bshr beor
swap 1+ tuck ->
]) drop nip
dup 0x10 bsft +
dup 2 bshr beor
dup 0x30 bsft +
(let b [U8 41])
(word j (@U8 --) [
(word s (@U8 @U8 -- @[Char8]) [ std/strcat ])
41s @b std/memzero dup dup dup dup @b s s s s s drop
])
(cond
[ 0x489fs = ] [ drop j "T(:4#,$;r:%=r&.oZ<%!" (as @U8) ]
//[ 0xd0a4s = ] [ drop j "V(/o# $;r*%<5g'61 &}" (as @U8) ]
[ 0xd0a4s = ] [ drop j "\\%7l7<;_r*88`*%8 h>!.{" (as @U8) ]
[ 2drop "I don't know what that is." set-message return ]
)
@b swap
ldak (while [ 0<> ] [
31 -
move 1+ swap ldak move (r beor)
copy 1+ swap (r <-) ldak
]) 2drop 0 swap-sb <-
@b set-message
])
(word cmd-version (@U8 --) [
drop
"Tuor for Varvara (v0.1.0)" set-message
])
(word cmd-go (@U8 --) [
goto
])
(word append-message (@U8 -- ) [
@ui-message std/strcat drop
])
(word set-message (@U8 -- ) [
dup std/strlen 1+ swap @ui-message std/memcpy drop
])
// Parse command. **Modifies input buffer**
//
// ( string -- index )
(word parse-command (@U8 -- @U8 (Of std/MaybeFF U16)) [
move
(r copy) :0 -> ': <> (when [
"unreachable: input not starting with ':'" std/panic
])
(r 1+)
// Separate by whitespace
(r copy)
(until [ (-- Bool) ldak is-whitespace ] [ 1+ ])
0 over-sb <-
1+
(while [ (-- Bool) ldak dup 0<> swap is-whitespace and ] [ 1+ ])
// ( args | strict? command )
COMMANDS-LEN (until [ 0= ] [
1-
dup @commands : (as @Command)
:name ->
(r copy) std/strequ
(when [
(make (Of std/MaybeFF U16))
(r drop)
return
])
]) drop
(r drop)
0xFFFFs
(make (Of std/MaybeFF U16))
])
// Find command that might match input, for hinting
//
// ( string -- index )
(word match-command (@U8 -- (Of std/MaybeFF U16)) [
move
(r copy) :0 -> ': <> (when [
"unreachable: input not starting with ':'" std/panic
])
(r 1+)
COMMANDS-LEN (until [ 0= ] [
1-
dup @commands : (as @Command)
:name ->
(r copy) std/strstartequ
(when [
(make (Of std/MaybeFF U16))
(r drop)
return
])
]) drop
(r drop)
0xFFFFs
(make (Of std/MaybeFF U16))
])
(word process-command (--) [
@ui-input :buffer
parse-command
dup ;maybe-not? (when [
2drop
@ui-input ;reset
return
])
:value @commands : (as @Command)
:func -> do
@ui-input ;reset
])
(word history-prev (--) [
@history :len -> 1s = (when [
@ui-message :0 -> 'N = (when [
"I said NO PREVIOUS PAGE" set-message
] [
"No previous page" set-message
])
return
])
@history ;pop drop // Pop off current URL
@history ;pop // Pop off previous too, because make-request will add it back
make-request
parse-gemtext
])
// TODO: factor out "first-non-space"
//
(word parse-gemtext ( -- ) [
0s @ui-scroll <-
0s @document ;shrink-to
0s @links ;shrink-to
(let pre Bool)
(let line-start @U8)
(word process-line (--) [
$line-start
$pre (when [
dup "```" std/strequ (when [
nil @pre <-
drop
] [
@document ;add-return move
(r copy) :line <-
.LineType/pre (r copy) :type <-
0s (as @Opaque) (r move) :data <-
])
return
])
(cond
[ (-- Bool) @document :len -> 0= ] [
0s (as @Opaque) .LineType/status
]
[ -> '# = ] [
(r 0) // Heading level ctr
(until [ (-- Bool) ldak '# <> ] [ 1+ (r 1+) ])
// Advance past space
ldak 0x20 = (when [ 1+ ])
(r move) (as @Opaque) .LineType/heading
]
[ "=>" std/strstartequ ] [
// Move past "=>" and spacing
1+ 1+
(until [ (-- Bool) ldak 0x20 <> ] [ 1+ ])
// Separate URL and text
dup
(until [ (-- Bool) ldak is-whitespace ] [ 1+ ])
dup 0 swap-sb <- // TODO: Finwe: optimize <value> <swap-sb> <-
// to (r val) move (r <-)
1+
// Move past spaces again (text)
(while [ (-- Bool) ldak is-whitespace ] [ 1+ ])
swap
@links ;add-one
@links ;last-ptr
:url <-
@links :len -> 1- (as @Opaque)
.LineType/link
]
[ "* " std/strstartequ ] [
// Move past "* " and spacing
1+ 1+
(while [ (-- Bool) ldak is-whitespace ] [ 1+ ])
0s (as @Opaque) .LineType/list
]
[ -> '> = ] [
// Move past ">" and spacing
1+
(until [ (-- Bool) ldak 0x20 <> ] [ 1+ ])
0s (as @Opaque) .LineType/quote
]
[ "```" std/strstartequ ] [
drop
t @pre <-
return
]
[ 0s (as @Opaque) .LineType/text ]
)
@document ;add-return move
(r copy) :type <-
(r copy) :data <-
(r move) :line <-
])
nil @pre <-
@buf :items ->
dup @line-start <-
ldak
(while [ 0<> ] [
0x0A = (when [
dup 0 swap-sb <- // NL -> NUL
process-line
dup 1+ @line-start <-
])
1+ ldak
]) 2drop
])
// Check if URL is relative or not, then make request.
//
// (link-url -- )
(word goto (@U8 -- ) [
dup "http://" std/strstartequ (when [
"Can't follow http:// link" set-message
drop return
])
dup "https://" std/strstartequ (when [
"Can't follow https:// link" set-message
drop return
])
dup "gemini://" std/strstartequ not (when [
(let urlbuf [U8 64])
0 64s @urlbuf std/memset8
// Append either host or last entire URL
dup :0 -> '/ = (when [
"gemini://" @urlbuf std/strcat
@conn :host -> swap std/strcat
] [
@history ;last @urlbuf std/strcat
// Now we need to find the last slash and truncate to that point.
(while [ (-- Bool) ldak '/ <> ] [ 1- ])
])
"/" swap std/strcat
std/strcat drop
@urlbuf (as @U8)
])
alloc/defrag
make-request
parse-gemtext
])
// ( url -- )
(word make-request (@U8 -- ) [
0s @buf ;shrink-to
dup
dup std/strlen 1+ dup alloc/get ;unwrap (as @U8)
move swap (r copy)
// ( len(url) url buf | buf )
std/memcpy drop
(r move) @history ;append
dup parse-url
// Debugging code, might be handy when adding forwards-history :P
//
//"HISTORY: " print-string @history :len -> print-dec nl
//@history ;last-ptr 2s + (as @@U8)
//(until [ @history :items -> = ] [
// 2s - (as @@U8)
// "- " print-string ldak print-string nl
//])
//"END" print-string nl nl
//drop
@conn :host <-
t @conn :tls? <-
1965s @conn :port <-
@conn .Net/connect deo
.Net/status dei .Status/ok <> (when [
drop
"Connection failed! (error: " set-message
.Net/status dei (as net/Status) ;to-string append-message
")" append-message
return
])
dup std/strlen .Net/length deo
.Net/send deo
2s .Net/length deo
"\r\n" .Net/send deo
.Net/status dei .Status/ok <> (when [
"Request failed! (error: " set-message
.Net/status dei (as net/Status) ;to-string append-message
0 .Net/close deo
return
])
(until [ ( -- Bool) .Net/status dei .Status/ok <> ] [
@buf :len -> 1024s + @buf ;try-ensure-capacity
not (when [
"Out of memory! Try using a real Gemini client." print-string nl
"(Tried to allocate " print-string
@buf :len -> print-dec
" + 256 bytes)" print-string
nl
"Couldn't load page (out of memory)" set-message
break
])
1024s .Net/length deo
@buf :len -> @buf :items -> + .Net/recv deo
.Net/length dei @buf :len -> + @buf :len <-
])
0 @buf :len -> @buf :items -> + <-
0 .Net/close deo
])
(word draw (--) [
draw-mouse
draw-content
draw-status
draw-clear-inputline
draw-message
draw-input-hint
draw-input
])
(word draw-clear-inputline (--) [
ENDY 16s 2s * + .Screen/y deo
0x01 @ui-current-style <-
0s .Screen/x deo
0 // dummy value
(while [ (-- Bool) .Screen/x dei .Screen/width dei < ] [ 0x20 draw-char ])
drop // dummy value
0s .Screen/x deo
])
(word draw-message (--) [
@ui-input :len -> 0<> (when [ return ])
@ui-message std/strlen 0= (when [ return ])
@assets/FONT-NORM @ui-current-font <-
0x01 @ui-current-style <-
ENDY 16s 2s * + .Screen/y deo
@ui-message draw-string
0x03 @ui-current-style <-
$ui-mode .Mode/sel-link = (when [
"-- Press ESC to cancel -- " (as @U8)
] [
"-- Press ENTER to dismiss -- " (as @U8)
])
dup measure-string .Screen/width dei swap - .Screen/x deo
draw-string-nowrap
])
(word draw-input-hint (--) [
@ui-input :len -> 1s <= (when [ return ])
@ui-input :buffer
match-command
dup ;maybe-not? (when [
drop return
])
:value @commands : (as @Command)
:name ->
@assets/FONT-NORM @ui-current-font <-
0x02 @ui-current-style <-
ENDY 16s 2s * + .Screen/y deo
0s .Screen/x deo
': draw-char
draw-string-nowrap
])
(word draw-input (--) [
@assets/FONT-NORM @ui-current-font <-
0x01 @ui-current-style <-
(let cursor-oldx U16)
(let cursor-oldy U16)
$cursor-oldx .Screen/x deo
$cursor-oldy .Screen/y deo
@assets/BLINKER .Screen/addr deo
0x40 .Screen/sprite deo
ENDY 16s 2s * + .Screen/y deo
0s .Screen/x deo
@ui-input :len -> 0= (when [ return ])
@ui-input :cursor -> @ui-input :buffer + 1-
// "Scroll" buffer to where it'll fit in the window (32px space margin)
//
// Do this by starting at the cursor, then going backwards while recording
// the cumulative width until it exceeds the viewport size.
//
(r [ .Screen/width dei 32s - 0s ])
@ui-input :cursor -> @ui-input :buffer + 1-
(while [ @ui-input :buffer <> (r [ 2dup > move ]) and ] [
ldak measure-char move (r +)
1-
]) (r 2drop)
ldak
(while [ 0<> ] [
draw-char
2dup = (when [
.Screen/y dei @cursor-oldy <-
.Screen/x dei dup @cursor-oldx <-
@assets/BLINKER .Screen/addr deo
0x4f .Screen/sprite deo
.Screen/x deo
])
1+ ldak
]) 3drop
])
(word draw-status (--) [
0x09 @ui-current-style <-
0s .Screen/x deo
ENDY 16s + .Screen/y deo
0x20 draw-char
(let numstr [U8 4])
0 4s @numstr std/memset8
$ui-cur-line 0<> (when [
$ui-scroll 100s * $ui-cur-line /
] [ 0s ])
@numstr std/itoa10
// Padding
@numstr measure-string 28s STARTX + swap -
(while [ .Screen/x dei > ] [
0x20 draw-char
]) drop
@numstr draw-string
"%" draw-string
0x20 draw-char
0x20 draw-char
"(" draw-string
@assets/FONT-EMPH @ui-current-font <-
@document :items -> :0 (as @Line) :line ->
draw-string
")" draw-string
@assets/FONT-FAT @ui-current-font <-
@conn :host ->
dup measure-string ENDX swap -
(while [ .Screen/x dei > ] [
0x20 draw-char
]) drop
draw-string
// Fill rest of input
// TODO: duplicated this code from end of draw-statusline()
0 // dummy value
(while [ (-- Bool) .Screen/x dei .Screen/width dei < ] [ 0x20 draw-char ])
drop // dummy value
])
(word draw-content (--) [
0x01 @ui-current-style <-
@assets/FONT-NORM @ui-current-font <-
0s @ui-cur-line <-
0x15 .Screen/auto deo
STARTX .Screen/x deo
STARTY .Screen/y deo
// clear
0b10000000 .Screen/pixel deo
// Should never happen in practice, as the return Gemini status will
// always be one line
@document :len -> 0= (when [ return ])
@document :items ->
@document :len ->
0s
(until [ (U16 U16 -- Bool) = ] [
.Screen/y dei .Screen/height dei > (when [
break
])
// items len ind
rot 2dup :
// len ind items ptr
(as @Line) // TODO: Finwe: shouldn't be necessary, : should cast for us
dup :type ->
(cond
[ .LineType/heading = ] [
@assets/FONT-FAT @ui-current-font <-
// TODO: Finwe: optimize: if every cond branch body starts
// with "DROP", then dup'ing isn't necessary for last
// branch
//
drop
dup :data -> (as U8)
(until [ 0= ] [
1- "#" draw-string
])
" " draw-string
drop
]
[ .LineType/link = ] [
drop
@assets/FONT-FAT @ui-current-font <-
"[" draw-string
(let b @[U8 3])
dup :data -> (as U8) @b link-index-to-keyset draw-string
"] " draw-string
@assets/FONT-NORM @ui-current-font <-
0x03 @ui-current-style <-
]
[ .LineType/list = ] [
drop
0xA5 draw-char 0x20 draw-char
]
[ .LineType/quote = ] [
drop
'> draw-char 0x20 draw-char
0x03 @ui-current-style <-
]
[ .LineType/pre = ] [
drop
@assets/FONT-MONO @ui-current-font <-
]
[ .LineType/status = ] [ drop drop rot> 1+ continue ]
[ drop ]
)
dup :line ->
draw-string
draw-next-line
:type ->
(cond
[ .LineType/heading = ] [
@assets/FONT-NORM @ui-current-font <-
]
[ .LineType/link = ] [
0x01 @ui-current-style <-
]
[ .LineType/quote = ] [
0x01 @ui-current-style <-
]
[ .LineType/pre = ] [
@assets/FONT-NORM @ui-current-font <-
]
)
drop
// len ind items
rot>
// items len ind
1+
])
3drop
])
(let last-space @U8)
(let last-space-screenx U16)
(word draw-next-line (--) [
STARTX .Screen/x deo
0s @last-space-screenx <-
$ui-cur-line $ui-scroll >= (when [
.Screen/y dei 16s + .Screen/y deo
])
$ui-cur-line 1+ @ui-cur-line <-
])
(word draw-string (@U8 --) [
dup @last-space <-
0s @last-space-screenx <-
copy
ldak
(while [ 0<> ] [
ENDX .Screen/x dei <
(when [
$last-space
// Ensure last whitespace wasn't beginning
// of string
dup (r copy) <>
(when [
// Move past the actual space
//
// FIXME: handle multiple spaces in a row
1+
$last-space-screenx .Screen/x deo
(while [ (-- Bool) .Screen/x dei .Screen/width dei < ] [
0x20 draw-char
])
rot-sbs drop
swap-bs
] [
// It's the beginning... nevermind!
drop
])
draw-next-line
// Continue on
drop ldak
])
// Record if the current char is whitespace
dup is-whitespace (when [
over-sb @last-space <-
.Screen/x dei @last-space-screenx <-
])
// TODO: Finwe: allow '\n' and '\t' literal
(cond
[ 0x0A = ] [ drop draw-next-line ]
[ 0x09 = ] [ drop 0x20 draw-char ]
[ draw-char ]
)
1+ ldak
])
2drop
(r drop)
])
(word draw-string-nowrap (@U8 --) [
ldak
(while [ 0<> ] [
draw-char
1+ ldak
]) 2drop
])
// TODO: Finwe: optimize when cond bodies are the same
// TODO: Finwe: (or) builtin
(word is-whitespace (Char8 -- Bool) [
// TODO: uncomment and test, should work
// (too lazy to test now, in middle of other things)
//
//copy (r [ 0x0A = ])
//copy (r [ 0x09 = or ])
//move (r [ 0x20 = or ])
//move (r [ 0x00 = or ])
//(r move)
(cond
[ 0x0A = ] [ t ]
[ 0x09 = ] [ t ]
[ 0x20 = ] [ t ]
[ 0x00 = ] [ t ]
[ nil ]
)
nip
])
(word draw-char (U8 -- ) [
(as U16) dup
0x50 bsft 255s + $ui-current-font + .Screen/addr deo
(r [ .Screen/x dei ]) // Save x position
$ui-current-font + -> (as U16) // Glyph width
// Don't draw unless scrolling permits
// Could omit this, but then blank lines disappear (since they get filled
// up with previous line's text)
//
$ui-cur-line $ui-scroll >= (when [
// Draw, then draw second sprite if width > 8
$ui-current-style .Screen/sprite deo
dup 8s > (when [
$ui-current-style .Screen/sprite deo
])
])
// Advance per glyph width
(r move) + .Screen/x deo
])
(word measure-string (@U8 -- U16) [
(r 0s)
ldak
(while [ 0<> ] [
measure-char move (r +)
1+ ldak
]) 2drop
(r move)
])
(word measure-char (U8 -- U16) [
(as U16) $ui-current-font + -> (as U16)
])
(word draw-mouse ( -- ) [
(let oldx U16)
(let oldy U16)
0x0 .Screen/auto deo
$oldx .Screen/x deo
$oldy .Screen/y deo
@assets/MOUSE .Screen/addr deo
0x40 .Screen/sprite deo
.Mouse/x dei dup @oldx <- .Screen/x deo
.Mouse/y dei dup @oldy <- .Screen/y deo
0x41 .Screen/sprite deo
])
// TODO: Finwe: switch statements already!! Why do I need to `drop` each time?
#inline
(word on-controller ( -- ) [
$ui-mode
(cond
[ .Mode/norm = ] [
.Controller/key dei
(cond
[ 0x0d = ] [
"" set-message
]
[ 'g = ] [
0s @ui-scroll <-
]
//[ 'G = ] [
// @document :len -> @ui-scroll <-
//]
[ ': = ] [
"" set-message
.Mode/input @ui-mode <-
': @ui-input ;insert
]
[ 'h = ] [
history-prev
]
//[ 'l = ] [
// history-next
//]
[ 'e = ] [
"" set-message
.Mode/input @ui-mode <-
":go " @ui-input ;append-string
@history ;last @ui-input ;append-string
]
[ 'f = ] [
.Mode/sel-link @ui-mode <-
3s @ui-sel-link std/memzero
"LINK: " set-message
]
[ 'j = ] [
$ui-scroll 1+ @ui-scroll <-
]
[ 'k = ] [
$ui-scroll dup 0<> (when [ 1- ]) @ui-scroll <-
]
)
drop
]
[ .Mode/sel-link = ] [
.Controller/key dei
(cond
[ 0x1b = ] [
drop
"" set-message
.Mode/norm @ui-mode <-
]
// Link is automatically followed once user has typed enough,
// if enter is pressed we can assume there isn't any link there
[ 0x0d = ] [
drop
"No such link: " set-message
@ui-sel-link append-message
.Mode/norm @ui-mode <-
]
// Ignore other control/non-printable chars, e.g. backspace
[ '! < ] [ drop ]
[
@ui-sel-link (as @U8)
(while [ (-- Bool) ldak 0 <> ] [ 1+ ])
<-
"LINK: " set-message
@ui-sel-link append-message
// Sequence is "complete" if first char is 0..9 or has two chars
@ui-sel-link (as @U8)
dup :0 -> 'a < move
:1 -> 0<> (r move) or
(when [
"" set-message
.Mode/norm @ui-mode <-
@ui-sel-link keyset-to-link-index
dup @links :len -> (as U8) >= (when [
drop
"No such link: " set-message
@ui-sel-link append-message
] [
@links :items -> : (as @Link) :url ->
goto
])
])
]
)
]