1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays combinators.short-circuit command-loop
5 formatting gemini gemini.private io io.directories
6 io.encodings.string io.encodings.utf8 io.files io.files.temp
7 io.launcher io.pipes kernel math math.parser namespaces present
8 sequences splitting system urls webbrowser ;
12 CONSTANT: DEFAULT-URL "gemini://gemini.circumlunar.space"
14 CONSTANT: HISTORY V{ }
19 : add-stack ( args -- )
20 URL ?first STACK index [
21 1 + dup STACK ?nth pick = [
24 STACK [ length ] [ delete-slice ] bi
27 0 STACK remove-nth! drop
34 : add-history ( args -- )
35 HISTORY dup length 10 > [
37 ] when dupd remove! push ;
39 : gemini-history ( -- )
40 HISTORY [ 1 + swap "[%s] %s\n" printf ] each-index
41 LINKS delete-all HISTORY LINKS push-all ;
43 : gemini-print ( url body meta -- )
46 gemini-charset decode string-lines [
47 { [ pre get not ] [ "=>" ?head ] } 0&& [
48 swap gemini-link present LINKS push
49 LINKS length swap "[%s] %s\n" printf
56 : gemini-get ( args -- )
58 >url dup gemini [ drop ] 2dip swap "text/" ?head [
59 "gemini.txt" temp-file
60 [ utf8 [ gemini-print ] with-file-writer ]
61 [ utf8 file-contents print ] bi
63 "ERROR: Cannot display '" "'" surround print 2drop
66 : gemini-go ( args -- )
67 present [ DEFAULT-URL ] when-empty
68 { [ "://" over subseq? ] [ "gemini://" head? ] } 1||
69 [ "gemini://" prepend ] unless
70 dup "gemini://" head? [
71 [ add-history ] [ add-stack ] [ gemini-get ] tri
74 : gemini-reload ( -- )
75 HISTORY ?last gemini-go ;
78 URL ?first STACK index [
79 1 - STACK ?nth [ gemini-get ] when*
82 : gemini-forward ( -- )
83 URL ?first STACK index [
84 1 + STACK ?nth [ gemini-get ] when*
89 >url f >>query f >>anchor
90 [ "/" ?tail drop "/" split1-last drop "/" append ] change-path
95 "gemini.txt" temp-file dup exists? [
96 "less" swap 2array try-process
101 1 + swap "[%d] %s\n" printf
105 "gemini.txt" temp-file ?delete-file 0 exit ;
108 URL ?first [ print ] when* ;
111 URL ?first [ >url "/" >>path gemini-go ] when* ;
113 : gemini-shell ( args -- )
114 "|" split "gemini.txt" temp-file dup exists? [
115 "cat" swap 2array prefix run-pipeline drop
121 { quot [ drop gemini-back ] }
122 { help "Go back to the previous Gemini URL." }
123 { abbrevs { "b" } } }
126 { quot [ drop gemini-forward ] }
127 { help "Go forward to the next Gemini URL." }
128 { abbrevs { "f" } } }
131 { quot [ drop gemini-history ] }
132 { help "Display recently viewed Gemini URLs." }
133 { abbrevs { "h" "hist" } } }
136 { quot [ drop gemini-less ] }
137 { help "View the most recent Gemini URL in a pager." }
138 { abbrevs { "l" } } }
141 { quot [ drop gemini-ls ] }
142 { help "List the currently available links." }
146 { quot [ gemini-go ] }
147 { help "Go to a Gemini URL" }
148 { abbrevs { "g" } } }
151 { quot [ drop gemini-up ] }
152 { help "Go up one directory from the recent Gemini URL." }
153 { abbrevs { "u" } } }
156 { quot [ drop gemini-url ] }
157 { help "Print the most recent Gemini URL." }
161 { quot [ drop gemini-reload ] }
162 { help "Reload the most recent Gemini URL." }
163 { abbrevs { "r" } } }
166 { quot [ drop gemini-root ] }
167 { help "Navigate to the most recent Gemini URL's root." }
171 { quot [ gemini-shell ] }
172 { help "'cat' the most recent Gemini URL through a shell." }
173 { abbrevs { "!" } } }
176 { quot [ drop gemini-quit ] }
177 { help "Quit the program." }
178 { abbrevs { "q" "exit" } } }
181 TUPLE: gemini-command-loop < command-loop ;
183 M: gemini-command-loop missing-command
184 over string>number [ 1 - LINKS ?nth ] [ f ] if* [
191 "Welcome to Project Gemini!" "GEMINI>"
192 gemini-command-loop new-command-loop
193 COMMANDS [ over add-command ] each