1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors ascii assocs combinators
5 combinators.short-circuit formatting gemini gemini.private io
6 io.encodings.string kernel math math.parser namespaces present
7 sequences splitting urls ;
22 CONSTANT: HISTORY V{ }
26 : add-history ( args -- )
27 HISTORY dup length 10 > [
29 ] when dupd remove! push ;
31 : gemini-history ( args -- )
32 drop HISTORY [ 1 + swap "[%s] %s\n" printf ] each-index
33 LINKS delete-all HISTORY LINKS push-all ;
35 : gemini-get ( args -- )
37 >url dup gemini [ drop ] 2dip swap "text/" ?head [
40 gemini-charset decode string-lines [
41 { [ pre get not ] [ "=>" ?head ] } 0&& [
42 swap gemini-link present LINKS push
43 LINKS length swap "[%s] %s\n" printf
50 "ERROR: Cannot display '" "'" surround print 2drop
53 : gemini-go ( args -- )
54 [ "gemini://gemini.circumlunar.space" ] when-empty
55 dup "gemini://" head? [ "gemini://" prepend ] unless
56 dup add-history gemini-get ;
58 : gemini-reload ( args -- )
59 drop HISTORY ?last gemini-go ;
61 : gemini-back ( args -- )
62 drop URL ?first HISTORY index [
63 1 - HISTORY ?nth [ gemini-get ] when*
66 : gemini-forward ( args -- )
67 drop URL ?first HISTORY index [
68 1 + HISTORY ?nth [ gemini-get ] when*
71 : gemini-up ( args -- )
73 >url f >>query f >>anchor
74 [ "/" ?tail drop "/" split1-last drop "/" append ] change-path
78 : gemini-cmd ( cmd -- )
79 " " split1 swap >lower ABBREVS ?at drop {
80 { "history" [ gemini-history ] }
81 { "go" [ gemini-go ] }
82 { "reload" [ gemini-reload ] }
83 { "back" [ gemini-back ] }
84 { "forward" [ gemini-forward ] }
85 { "up" [ gemini-up ] }
88 dup string>number [ 1 - LINKS ?nth ] [ f ] if* [
91 "ERROR: Unknown command '" "'" surround print drop
97 "Welcome to Project Gemini!" print flush [
98 "GEMINI> " write flush readln
99 [ gemini-cmd t ] [ f ] if*