2017-12-11 03:30:45 +01:00
This is a tiny Gopher client written in RETRO. It's only for the iOS implementation, but could be adapted for others with a little reworking of the file I/O code.
It's intentionally kept minimal, handling only text files and directories. The interface is line oriented.
# General Configuration
`GOPHER-DATA` is the name of the file to store the last downloaded content into.
`WrapPoint` holds the number of characters to display before wrapping output.
There is also a `Results` variable which will point to the data set being displayed.
~~~
'GopherData.txt 'GOPHER-DATA s:const
#34 'WrapPoint var<n>
'Results var
2018-08-06 00:56:17 +02:00
'TARGET d:create #1024 #64 * allot
2017-12-11 03:30:45 +01:00
~~~
# Download Data
Specify a server, a port number, and the initial seletor to fetch.
~~~
:grab (sns-)
2018-08-06 00:56:17 +02:00
&TARGET 'abcd 'dabc reorder gopher:get
drop ;
2017-12-11 03:30:45 +01:00
~~~
# Indexes
An output line will have a format like this:
1234: TXT About this server
1235: DIR Archive
1236: This is an information line or
unknown type
I define a variable `Displayed` to track how
many characters have been displayed so far.
~~~
{{
'Displayed var
~~~
A `wrap` word handles wrapping and indenting lines based on the `Displayed` characters and the global `WrapPoint`.
~~~
:wrap
@Displayed @WrapPoint gt?
2018-05-07 18:36:37 +02:00
[ #0 !Displayed nl '__________ s:put ]
2017-12-11 03:30:45 +01:00
[ &Displayed v:inc ] choose ;
~~~
Next is a word to display the directory type. This uses a simple case structure to handle things.
~~~
:type (c-)
2018-05-07 18:36:37 +02:00
$0 [ 'TXT_ s:put ] case
$1 [ 'DIR_ s:put ] case
drop '____ s:put ;
2017-12-11 03:30:45 +01:00
~~~
I define a `line` word to display the line number. This lets me pad the number with a leading number of spaces and have a colon and space following it.
~~~
:line (n-)
n:to-string dup
s:length #4 swap - [ sp ] times
2018-05-07 18:36:37 +02:00
s:put ':_ s:put ;
2017-12-11 03:30:45 +01:00
~~~
2018-05-07 18:36:37 +02:00
I now define a custom version of `s:put` that will call the `wrap` word defined earlier. I've not used `s:for-each` here as this will be called frequently, so it's a little faster to have a manually constructed loop here.
2017-12-11 03:30:45 +01:00
~~~
2018-05-07 18:36:37 +02:00
:s:put<w/wrap> (s-)
[ repeat fetch-next 0; c:put wrap again ]
2017-12-11 03:30:45 +01:00
call drop ;
~~~
2018-05-07 18:36:37 +02:00
The next word is `display`. This will tokenize a line and display the `type` and then the rest of the description using `s:put<w/wrap>`.
2017-12-11 03:30:45 +01:00
~~~
:display (s-)
#0 !Displayed
ASCII:HT s:tokenize #0 set:nth fetch
2018-05-07 18:36:37 +02:00
fetch-next type s:put<w/wrap> ;
2017-12-11 03:30:45 +01:00
~~~
~~~
---reveal---
~~~
And finally, tie everything together. This will display an index.
~~~
:display:index (-)
2018-08-06 00:56:17 +02:00
&TARGET ASCII:LF s:tokenize !Results
2017-12-11 03:30:45 +01:00
@Results fetch #2 - @Results store
2018-08-06 00:56:17 +02:00
#0 @Results
2017-12-11 03:30:45 +01:00
[ over line display nl n:inc ]
set:for-each drop ;
}}
~~~
# Text Viewer
Text files are really easy. We just read the downloaded file into a buffer and display it.
~~~
:display:text (-)
2018-08-06 00:56:17 +02:00
&TARGET s:put nl ;
2017-12-11 03:30:45 +01:00
~~~
# Final Bits
`display-by-type` takes a type identifer and then calls the proper viewer.
~~~
:display-by-type (c-)
$0 [ display:text ] case
$1 [ display:index ] case
2018-05-07 18:36:37 +02:00
dup c:put nl drop ;
2017-12-11 03:30:45 +01:00
~~~
`home` just fetches a starting index and displays it. I have this hardcoded to my personal Gopher server.
~~~
:home (-)
2018-08-06 00:56:17 +02:00
'forthworks.com #70 '/ grab
2017-12-11 03:30:45 +01:00
$1 display-by-type ;
~~~
The last bit is `g`, which is used to navigate a directory. Pass it the line number to load.
~~~
:g (n-)
@Results swap set:nth fetch
ASCII:HT s:tokenize
dup #0 set:nth fetch fetch [
[ #2 set:nth fetch ]
[ #3 set:nth fetch s:chop s:to-number ]
[ #1 set:nth fetch ] tri grab
] dip display-by-type ;
~~~
The final thing to do is just call `home` to get started.
~~~
home
~~~