1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays assocs combinators
5 combinators.short-circuit command-loop formatting gemini
6 gemini.private io io.directories io.encodings.string
7 io.encodings.utf8 io.files io.files.temp io.launcher io.pipes
8 kernel math math.parser namespaces present sequences splitting
9 system urls webbrowser ;
13 CONSTANT: DEFAULT-URL "gemini://gemini.circumlunar.space"
15 CONSTANT: HISTORY V{ }
21 : find-url ( url items -- i item )
22 [ dup array? [ first ] when = ] with find ;
24 : nth-url ( i items -- url )
25 ?nth dup array? [ first ] when ;
27 : stack-url ( delta -- url )
28 URL ?first STACK find-url drop
29 [ + STACK nth-url ] [ drop f ] if* ;
31 : add-stack ( args -- )
32 dup dup array? [ first ] when
33 dup STACK find-url drop [
36 URL ?first STACK find-url drop [
37 over PAGE find-url drop [
38 1 + dup STACK nth-url rot = [
41 STACK [ length ] [ delete-slice ] bi
44 0 STACK remove-nth! drop
58 : add-history ( args -- )
59 HISTORY dup length 10 > [
61 ] when dupd remove! push ;
63 : print-links ( links verbose? -- )
64 LINKS delete-all over LINKS push-all
66 1 + swap [ dup array? [ first ] when URL ?first = [ drop "*" ] when ] keep
67 _ [ dup array? [ dup second empty? not ] [ f ] if ] [ f ] if [
68 first2 swap "[%s] %s (%s)\n" printf
70 dup array? [ first2 ] [ f ] if
71 dup empty? -rot ? "[%s] %s\n" printf
75 : gemini-history ( -- )
76 HISTORY t print-links ;
78 : gemini-print ( url body meta -- )
81 gemini-charset decode split-lines [
82 { [ pre get not ] [ "=>" ?head ] } 0&& [
83 swap gemini-link present over 2array PAGE push
84 PAGE length swap "[%s] %s\n" printf
89 LINKS delete-all PAGE LINKS push-all
92 : gemini-get ( args -- )
94 >url dup gemini [ drop ] 2dip swap "text/" ?head [
95 "gemini.txt" temp-file
96 [ utf8 [ gemini-print ] with-file-writer ]
97 [ utf8 file-contents print ] bi
99 "ERROR: Cannot display '" "'" surround print 2drop
102 : gemini-go ( args -- )
103 present [ DEFAULT-URL ] when-empty
104 { [ dup "://" find-subseq? ] [ "gemini://" head? ] } 1||
105 [ "gemini://" prepend ] unless
106 dup "gemini://" head? [
107 [ add-history ] [ add-stack ] [ gemini-get ] tri
110 : gemini-reload ( -- )
111 URL ?first gemini-go ;
114 -1 stack-url [ gemini-get ] when* ;
116 : gemini-forward ( -- )
117 1 stack-url [ gemini-get ] when* ;
121 >url f >>query f >>anchor
122 [ dup "/" tail? "./../" "./" ? url-append-path ] change-path
127 "gemini.txt" temp-file dup file-exists? [
128 "less" swap 2array try-process
131 : gemini-ls ( args -- )
132 [ PAGE ] [ "-l" = ] bi* print-links ;
135 "gemini.txt" temp-file ?delete-file 0 exit ;
138 URL ?first [ print ] when* ;
141 URL ?first [ >url "/" >>path gemini-go ] when* ;
143 : gemini-shell ( args -- )
144 "|" split "gemini.txt" temp-file dup file-exists? [
145 "cat" swap 2array prefix run-pipeline drop
151 { quot [ drop gemini-back ] }
152 { help "Go back to the previous Gemini URL." }
153 { abbrevs { "b" } } }
156 { quot [ drop gemini-forward ] }
157 { help "Go forward to the next Gemini URL." }
158 { abbrevs { "f" } } }
161 { quot [ drop gemini-history ] }
162 { help "Display recently viewed Gemini URLs." }
163 { abbrevs { "h" "hist" } } }
166 { quot [ drop gemini-less ] }
167 { help "View the most recent Gemini URL in a pager." }
168 { abbrevs { "l" } } }
171 { quot [ gemini-ls ] }
172 { help "List the currently available links." }
176 { quot [ gemini-go ] }
177 { help "Go to a Gemini URL" }
178 { abbrevs { "g" } } }
181 { quot [ drop "gemini://gus.guru/search" gemini-go ] }
182 { help "Submit a query to the GUS search engine." }
186 { quot [ drop gemini-up ] }
187 { help "Go up one directory from the recent Gemini URL." }
188 { abbrevs { "u" } } }
191 { quot [ drop gemini-url ] }
192 { help "Print the most recent Gemini URL." }
196 { quot [ drop gemini-reload ] }
197 { help "Reload the most recent Gemini URL." }
198 { abbrevs { "r" } } }
201 { quot [ drop gemini-root ] }
202 { help "Navigate to the most recent Gemini URL's root." }
206 { quot [ gemini-shell ] }
207 { help "'cat' the most recent Gemini URL through a shell." }
208 { abbrevs { "!" } } }
211 { quot [ drop gemini-quit ] }
212 { help "Quit the program." }
213 { abbrevs { "q" "exit" } } }
216 TUPLE: gemini-command-loop < command-loop ;
218 M: gemini-command-loop missing-command
219 over string>number [ 1 - LINKS ?nth ] [ f ] if* [
222 [ dup array? [ first ] when gemini-get 3drop ] tri
228 "Welcome to Project Gemini!" "GEMINI>"
229 gemini-command-loop new-command-loop
230 COMMANDS [ over add-command ] each