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 ! XXX: page titles and urls in history
23 : add-stack ( args -- )
24 dup STACK index [ drop ] [
25 URL ?first STACK index [
26 over PAGE keys index [
27 1 + dup STACK ?nth pick = [ 2drop ] [
28 STACK [ length ] [ delete-slice ] bi
31 0 STACK remove-nth! drop
43 : add-history ( args -- )
44 HISTORY dup length 10 > [
46 ] when dupd remove! push ;
48 : gemini-history ( -- )
49 HISTORY [ 1 + swap "[%d] %s\n" printf ] each-index
50 LINKS delete-all HISTORY LINKS push-all ;
52 : gemini-print ( url body meta -- )
55 gemini-charset decode string-lines [
56 { [ pre get not ] [ "=>" ?head ] } 0&& [
57 swap gemini-link present over 2array PAGE push
58 PAGE length swap "[%s] %s\n" printf
63 LINKS delete-all PAGE keys LINKS push-all
66 : gemini-get ( args -- )
68 >url dup gemini [ drop ] 2dip swap "text/" ?head [
69 "gemini.txt" temp-file
70 [ utf8 [ gemini-print ] with-file-writer ]
71 [ utf8 file-contents print ] bi
73 "ERROR: Cannot display '" "'" surround print 2drop
76 : gemini-go ( args -- )
77 present [ DEFAULT-URL ] when-empty
78 { [ "://" over subseq? ] [ "gemini://" head? ] } 1||
79 [ "gemini://" prepend ] unless
80 dup "gemini://" head? [
81 [ add-history ] [ add-stack ] [ gemini-get ] tri
84 : gemini-reload ( -- )
85 HISTORY ?last gemini-go ;
88 URL ?first STACK index [
89 1 - STACK ?nth [ gemini-get ] when*
92 : gemini-forward ( -- )
93 URL ?first STACK index [
94 1 + STACK ?nth [ gemini-get ] when*
99 >url f >>query f >>anchor
100 [ "/" ?tail drop "/" split1-last drop "/" append ] change-path
105 "gemini.txt" temp-file dup exists? [
106 "less" swap 2array try-process
109 : gemini-ls ( args -- )
112 _ [ " (" ")" surround ] [ drop f ] if
115 LINKS delete-all PAGE keys LINKS push-all ;
118 "gemini.txt" temp-file ?delete-file 0 exit ;
121 URL ?first [ print ] when* ;
124 URL ?first [ >url "/" >>path gemini-go ] when* ;
126 : gemini-shell ( args -- )
127 "|" split "gemini.txt" temp-file dup exists? [
128 "cat" swap 2array prefix run-pipeline drop
131 : gemini-stack ( -- )
133 1 + swap dup URL ?first = " (*)" f ?
136 LINKS delete-all STACK LINKS push-all ;
141 { quot [ drop gemini-back ] }
142 { help "Go back to the previous Gemini URL." }
143 { abbrevs { "b" } } }
146 { quot [ drop gemini-forward ] }
147 { help "Go forward to the next Gemini URL." }
148 { abbrevs { "f" } } }
151 { quot [ drop gemini-history ] }
152 { help "Display recently viewed Gemini URLs." }
153 { abbrevs { "h" "hist" } } }
156 { quot [ drop gemini-less ] }
157 { help "View the most recent Gemini URL in a pager." }
158 { abbrevs { "l" } } }
161 { quot [ gemini-ls ] }
162 { help "List the currently available links." }
166 { quot [ gemini-go ] }
167 { help "Go to a Gemini URL" }
168 { abbrevs { "g" } } }
171 { quot [ drop "gemini://gus.guru/search" gemini-go ] }
172 { help "Submit a query to the GUS search engine." }
176 { quot [ drop gemini-up ] }
177 { help "Go up one directory from the recent Gemini URL." }
178 { abbrevs { "u" } } }
181 { quot [ drop gemini-url ] }
182 { help "Print the most recent Gemini URL." }
186 { quot [ drop gemini-reload ] }
187 { help "Reload the most recent Gemini URL." }
188 { abbrevs { "r" } } }
191 { quot [ drop gemini-root ] }
192 { help "Navigate to the most recent Gemini URL's root." }
196 { quot [ gemini-shell ] }
197 { help "'cat' the most recent Gemini URL through a shell." }
198 { abbrevs { "!" } } }
201 { quot [ drop gemini-stack ] }
202 { help "Display the current navigation stack." }
206 { quot [ drop gemini-quit ] }
207 { help "Quit the program." }
208 { abbrevs { "q" "exit" } } }
211 TUPLE: gemini-command-loop < command-loop ;
213 M: gemini-command-loop missing-command
214 over string>number [ 1 - LINKS ?nth ] [ f ] if* [
221 "Welcome to Project Gemini!" "GEMINI>"
222 gemini-command-loop new-command-loop
223 COMMANDS [ over add-command ] each