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 string-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 { [ "://" over subseq? ] [ "gemini://" head? ] } 1||
105 [ "gemini://" prepend ] unless
106 dup "gemini://" head? [
107 [ add-history ] [ add-stack ] [ gemini-get ] tri
110 : gemini-reload ( -- )
111 HISTORY ?last 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 [ "../" url-append-path ] change-path
126 "gemini.txt" temp-file dup exists? [
127 "less" swap 2array try-process
130 : gemini-ls ( args -- )
131 [ PAGE ] [ "-l" = ] bi* print-links ;
134 "gemini.txt" temp-file ?delete-file 0 exit ;
137 URL ?first [ print ] when* ;
140 URL ?first [ >url "/" >>path gemini-go ] when* ;
142 : gemini-shell ( args -- )
143 "|" split "gemini.txt" temp-file dup exists? [
144 "cat" swap 2array prefix run-pipeline drop
150 { quot [ drop gemini-back ] }
151 { help "Go back to the previous Gemini URL." }
152 { abbrevs { "b" } } }
155 { quot [ drop gemini-forward ] }
156 { help "Go forward to the next Gemini URL." }
157 { abbrevs { "f" } } }
160 { quot [ drop gemini-history ] }
161 { help "Display recently viewed Gemini URLs." }
162 { abbrevs { "h" "hist" } } }
165 { quot [ drop gemini-less ] }
166 { help "View the most recent Gemini URL in a pager." }
167 { abbrevs { "l" } } }
170 { quot [ gemini-ls ] }
171 { help "List the currently available links." }
175 { quot [ gemini-go ] }
176 { help "Go to a Gemini URL" }
177 { abbrevs { "g" } } }
180 { quot [ drop "gemini://gus.guru/search" gemini-go ] }
181 { help "Submit a query to the GUS search engine." }
185 { quot [ drop gemini-up ] }
186 { help "Go up one directory from the recent Gemini URL." }
187 { abbrevs { "u" } } }
190 { quot [ drop gemini-url ] }
191 { help "Print the most recent Gemini URL." }
195 { quot [ drop gemini-reload ] }
196 { help "Reload the most recent Gemini URL." }
197 { abbrevs { "r" } } }
200 { quot [ drop gemini-root ] }
201 { help "Navigate to the most recent Gemini URL's root." }
205 { quot [ gemini-shell ] }
206 { help "'cat' the most recent Gemini URL through a shell." }
207 { abbrevs { "!" } } }
210 { quot [ drop gemini-quit ] }
211 { help "Quit the program." }
212 { abbrevs { "q" "exit" } } }
215 TUPLE: gemini-command-loop < command-loop ;
217 M: gemini-command-loop missing-command
218 over string>number [ 1 - LINKS ?nth ] [ f ] if* [
221 [ dup array? [ first ] when gemini-get 3drop ] tri
227 "Welcome to Project Gemini!" "GEMINI>"
228 gemini-command-loop new-command-loop
229 COMMANDS [ over add-command ] each