31cc129ccd7180ea6996e1e00bc130df59f02a2e — Luxferre 1 year, 2 months ago ec0972f
Added tools section and the first tool, gopherinfo.sh
2 files changed, 77 insertions(+), 0 deletions(-)

A tools/README-tools.md
A tools/gopherinfo.sh
A tools/README-tools.md => tools/README-tools.md +39 -0
@@ 0,0 1,39 @@
# Bopher Tools: a set of simple Bash scripts to not just browse Gopher but also create for it

Bopher Tools is a small collection of simple shell scripts that are:

- written in pure Bash with no external dependencies;
- totally independent from each other (but can be combined in the true spirit of Unix philosophy);
- only accept input from stdin and only output their results into stdout;
- aim to ease your life when it comes to creating and publishing your own content in Gopherspace.

It is recommended to distribute these scripts along with the main Bopher-NG client, although everyone is free to redistribute them in any other form. Just like the main Bopher-NG script, all these tools are released into public domain. Their documentation is added to this page in the chronological order they were created.

Without further ado, let's begin!

## `gopherinfo.sh`

This tool converts every line of the standard input into a valid CRLF-terminated Gophermap line of the `i` type that obeys RFC1436. It accepts three **optional** parameters: the number of leading spaces (0 by default), the number of trailing spaces (0 by default) and the placeholder (';' by default) to put into the selector and host fields unused in the `i` type (and the port field is always set to 0). It also pads every input line with the amount of spaces required to make the line length the same as the longest one in the input. All this is done to make your output Gophermap look nice when viewed in plaintext as well.

Note that the tool **does not** output the trailing dot to end the Gophermap, because its output can be used as a part of other Gophermaps. To end the map manually according to the RFC1436, just invoke `printf '.\r\n'` afterwards redirecting to the same output.

Example of using `gopherinfo.sh` in combination with FIGlet, using 4 leading spaces, 0 trailing spaces and `|` as the placeholder:
$ figlet 'Bash it' | bash gopherinfo.sh 4 0 '|'
i     ____            _       _ _       |       |       0
i    | __ )  __ _ ___| |__   (_) |_     |       |       0
i    |  _ \ / _` / __| '_ \  | | __|    |       |       0
i    | |_) | (_| \__ \ | | | | | |_     |       |       0
i    |____/ \__,_|___/_| |_| |_|\__|    |       |       0
i                                       |       |       0

A non-trivial task would be using this output in some Web-based services like Happy Net Box. You have to paste it in a format that the browser's text input element wouldn't mess with. To do this, you need the following steps:

1. Pipe the `gopherinfo.sh` output into the autoquoter like this: `| ( read -rsd '' x; printf '%q\n' "$x" )`
2. Copy everything except the first dollar sign of the output to your clipboard.
3. In your Web browser, open a developer console (F12), type `console.log()` and put the cursor between the brackets.
4. Paste your escaped string and hit Enter. If everything went correctly, the output should not be mangled.
5. Copy your `console.log` output again (make sure to fully copy the last line too!) and paste it in your Web service's posting textarea.

This hack also appies to any Gophermaps and not just the ones generated with this tool.

A tools/gopherinfo.sh => tools/gopherinfo.sh +38 -0
@@ 0,0 1,38 @@
# A simple helper tool to create valid Gophermap info lines from the standard input
# Features:
# - implemented in pure Bash and doesn't depend on wc (that can have a non-standard implementation)
# - aligns the spaces according to the longest line so that the output would look nice in plaintext too
# - doesn't do any heuristics but allows you to specify the amount of leading and trailing spaces (0 by default)
# - allows visual selector/host placeholder customization (';' by default)
# - converts LF-only line endings to CRLF (as required by the RFC) without unix2dos dependency
# - only outputs to stdout (but you can redirect it into a file if you need to)
# Usage: cat [file] | gopherinfo.sh [leading_spaces] [trailing_spaces] [placeholder_char]
# Created by Luxferre in 2023, released into public domain



[[ -z "$LSPACES" ]] && LSPACES=0
[[ -z "$TSPACES" ]] && TSPACES=0
[[ -z "$DELIM" ]] && DELIM=';'

readarray -t LINES -d $'\n' # read the input line array (split by LF)
LINECOUNT=${#LINES[@]} # get the array length
MAXLEN=0 # initialize the maximum length variable
for ((idx=0;idx<LINECOUNT;idx++)); do # first pass to filter out inconsistent newlines and to detect maximum length
  line="${LINES[$idx]%%$'\r'}" # remove a trailing CR if it is there
  llen=${#line} # get the line length
  (( llen > MAXLEN )) && MAXLEN=$llen # update the current maximum length
  LINES[$idx]="$line" # update the element by index
fmtstr="%-$(( LSPACES + 1 ))s%-$(( MAXLEN + TSPACES ))s${TAB}%s${TAB}%s${TAB}0${CRLF}" # params: i, line, DELIM, DELIM
for line in "${LINES[@]}"; do # second pass to actually output the formatted lines
  printf "$fmtstr" 'i' "$line" "$DELIM" "$DELIM"