1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays ascii assocs combinators
5 combinators.short-circuit formatting gemini gemini.private
6 grouping io io.directories io.encodings.string io.encodings.utf8
7 io.files io.files.temp io.launcher kernel math math.parser
8 namespaces present sequences splitting system urls webbrowser ;
26 { "back" "Go back to the previous Gemini URL." }
27 { "forward" "Go forward to the next Gemini URL." }
28 { "go" "Go to a Gemini URL" }
29 { "help" "Get help for commands." }
30 { "history" "Display recent history of Gemini URLs." }
31 { "less" "View the most recent Gemini URL in a pager." }
32 { "quit" "Quit the program." }
33 { "reload" "Reload the most recent Gemini URL." }
34 { "up" "Go up one directory from the recent Gemini URL." }
37 CONSTANT: HISTORY V{ }
41 : add-history ( args -- )
42 HISTORY dup length 10 > [
44 ] when dupd remove! push ;
46 : gemini-history ( -- )
47 HISTORY [ 1 + swap "[%s] %s\n" printf ] each-index
48 LINKS delete-all HISTORY LINKS push-all ;
50 : gemini-print ( url body meta -- )
53 gemini-charset decode string-lines [
54 { [ pre get not ] [ "=>" ?head ] } 0&& [
55 swap gemini-link present LINKS push
56 LINKS length swap "[%s] %s\n" printf
63 : gemini-get ( args -- )
65 >url dup gemini [ drop ] 2dip swap "text/" ?head [
66 "gemini.txt" temp-file
67 [ utf8 [ gemini-print ] with-file-writer ]
68 [ utf8 file-contents print ] bi
70 "ERROR: Cannot display '" "'" surround print 2drop
73 : gemini-go ( args -- )
74 [ "gemini://gemini.circumlunar.space" ] when-empty
75 { [ "://" over subseq? ] [ "gemini://" head? ] } 1||
76 [ "gemini://" prepend ] unless
77 dup "gemini://" head? [
78 dup add-history gemini-get
81 : gemini-reload ( -- )
82 HISTORY ?last gemini-go ;
85 URL ?first HISTORY index [
86 1 - HISTORY ?nth [ gemini-get ] when*
89 : gemini-forward ( -- )
90 URL ?first HISTORY index [
91 1 + HISTORY ?nth [ gemini-get ] when*
96 >url f >>query f >>anchor
97 [ "/" ?tail drop "/" split1-last drop "/" append ] change-path
102 "less" "gemini.txt" temp-file 2array try-process ;
105 "gemini.txt" temp-file ?delete-file 0 exit ;
107 : gemini-help ( args -- )
110 [ 6 <groups> ] [ longest length 4 + ] bi
111 '[ [ _ CHAR: \s pad-tail write ] each nl ] each
113 ABBREVS ?at drop COMMANDS ?at [
116 "ERROR: Command '" "' not found" surround print
120 : gemini-cmd ( cmd -- )
121 " " split1 swap >lower ABBREVS ?at drop {
122 { "help" [ gemini-help ] }
123 { "history" [ drop gemini-history ] }
124 { "go" [ gemini-go ] }
125 { "reload" [ drop gemini-reload ] }
126 { "back" [ drop gemini-back ] }
127 { "forward" [ drop gemini-forward ] }
128 { "up" [ drop gemini-up ] }
129 { "less" [ drop gemini-less ] }
130 { "quit" [ drop gemini-quit ] }
133 dup string>number [ 1 - LINKS ?nth ] [ f ] if* [
136 "ERROR: Unknown command '" "'" surround print drop
142 "Welcome to Project Gemini!" print flush [
143 "GEMINI> " write flush readln
144 [ gemini-cmd t ] [ f ] if*