The Gemini protocol is a very simple and light Internet protocol.
Unlike HTML files that contain layers of tags, style sheets and Javascript, a Gemini document is a simple readable document.
In this blog I wanted to document:
- Gemini servers and clients using only 1 line of Bash
- How to use large ASCII text in Gemini documents
- How to create simple bar charts
- How to create Gemini CGI apps in Python
Getting Started
A Gemini document only supports a few statements and graphic images such as JPEG or PNG are not supported.
An example Gemini document with the common formatting options:
# Heading level 1 (H1)
## Heading level 2 (H2)
### Heading level 3 (H3)
=> testpage.gmi A link to another page.
> This line will show as a block-quote.
A list of items
* This is the first list item.
* This is another list item.
```
Code or ASCII Block
_ ___ ___ ___ ___ _____ _
/_\ / __|/ __|_ _|_ _| |_ _|__ __| |_
/ _ \\__ \ (__ | | | | | |/ -_|_-< _|
/_/ \_\___/\___|___|___| |_|\___/__/\__|
```
Within a Gemini browser this file would look like:

Content Type
The content type is used by browsers and applications to determine how to manage the requested file.
Typically the content type is managed by server, for example a web server will send a HTTP/1.0 200 OK prior to sending the HTML file.
For the Gemini protocol the content type is: 20 text/gemini . Depending on the Gemini server the user may need to be add the content type manually. (More about this in Bash and CGI servers).
Simple Bash Gemini Servers and Clients
For basic testing a one line Bash statement can be used for custom Gemini servers and clients.
The Gemini protocol typically uses SSL (Secure Sockets Layer) and TLS (Transport Layer Security) encryption so the Bash ncat utility is needed (Note: the nc command does not support SSL).
Below is an example of single Gemini request:

The Gemini server is defined by using the -l , listen option. When a client requests data, the cat statement is used with a pipe (|) to output the file testpage.gmi.
The Gemini client echo’s a “from PC” message with its request, this helps identify which client is requesting data.
A simple Bash ncat statement doesn’t manage the content type so a “20 text/gemini” line is added to the top of the test page.
Dynamic Bash Data
In the earlier example the Bash server is only supporting 1 request then exiting.
A while loop can be added to pass a Bash script to the ncat statement. Below is an example of Bash script (showdata.sh) that show CPU data using the vmstat utility:
#!/bin/bash
#
# showdata.sh - Output data for Gemini Bash Server
#
echo "20 text/gemini"
echo " "
echo "#VMSTAT"
echo " "
date +"%T"
echo " "
# set Gemini formating for ASCII
echo "\`\`\`"
#vmstat
top -n 1
echo "\`\`\`"
To make the script executable use the command: chmod +x showdata.sh
The Bash command to run this script as a Gemini server is:
while true; do ./showdata.sh | ncat -l -p 1965 --ssl; done
The earlier Bash Gemini client command can be used, or a Gemini browser/client app can be used. The handling of SSL/TLS encryption will vary with the client app. I used the very basic Zain app (https://gitgud.io/sathariel/zain) :

(Note: for the Zain client I needed to load tcl/tls, sudo apt-get install -y tcl-tls)
Using a 1 line Bash Gemini server is great for basic testing but I wouldn’t recommend if you want to connect to variety of different Gemini client applications.
Large ASCII Text
Gemini documents don’t support different font sizes, a workaround is to use the figlet tool to generate multi-line text strings. Figlet is installed on Ubuntu/Debian/Raspbian by:
sudo apt install figlet
Figlet has a number of font styles that use 2-5 line height characters:

When using ASCII headings the Gemini code formatting option should be used, and this has three backticks (“`) before and after the headings.
The earlier example can be modified to have ASCII headings:
#!/bin/bash
#
# showdata2.sh - Output Large Headings to a Gemini Bash Server
#
echo "20 text/gemini"
echo " "
echo "\`\`\`"
# Generate large text
figlet -f standard "Raspberry Pi"
figlet -f small -m 2 $(date +"%T")
# show CPU stats
vmstat
echo "\`\`\`"

Bar Charts
Horizontal ASCII bar charts can be created by using the printf statement with different ASCII fill characters. For example:
# show a label with bar and value text
#
label="temp"; val=20;
bar="printf '█%.0s' {1..$val}" ;
printf '\n%-5s ' $label; eval $bar
printf '░%.0s' {1..5} ;
printf ' 50 C\n'
temp ████████████████████░░░░░ 50 C
This bar logic can be using in a Raspberry Pi Stats page that looks at idle time and free space on the SD card:
#!/bin/bash
#
# pi_stats.sh - Output Pi Stats as a Gemini Page
#
echo "20 text/gemini"
echo " "
# Put the rest of the Gemini document into code block mode
echo "\`\`\`"
# Generate large text
figlet -f standard "Pi Stats"
# Show the time
echo "Time: $(date +'%T')"
echo ""
# Get idle time, scale 0-50
idle=$(top -n 1 | grep id | awk '{print $8}')
barsize=$(echo $idle | awk '{printf "%0.f" , $1/2}')
thebar="printf '█%.0s' {1..$barsize}"
graysize=$(expr 50 - $barsize)
thegray="printf '░%.0s' {1..$graysize}"
printf 'Idle Time '; eval $thebar; eval $thegray ; echo " $idle %"
echo ""
# Get free space on SD card, scale 0-50
freesp=$(df | grep root | awk '{printf "%0.f", $5/2}')
barsize=$(echo $freesp | awk '{printf "%0.f" , $1/2}')
thebar="printf '█%.0s' {1..$barsize}"
graysize=$(expr 50 - $barsize)
thegray="printf '░%.0s' {1..$graysize}"
printf 'Free Space '; eval $thebar; eval $thegray ; echo " $freesp %"
echo "\`\`\`"
To run this page use
while true; do ./pi_stats.sh | ncat -l -p 1965 --ssl; done

Python CGI Pages
There are a number of good Gemini servers, for my testing I used the Python based Jetforce server, it is installed by:
pip install jetforce
To run the Jetforce server it is important to define a home directory, the allowable hosts that can connect (0.0.0.0 is all IP4 nodes) and the server’s hostname:
# Start the jetforce Gemini server for all IP4 hosts
jetforce --dir /home/pi/temp --host "0.0.0.0" --hostname 192.168.0.105 &
# Start jetforce without hardcoding hostname
# jetforce --dir /home/pi/temp --host "0.0.0.0" --hostname $(hostname -I) &
By default CGI (Common Gateway Interface) files are defined in the directory cgi-bin which is under the home directory.
Unlike a one-line Bash server, Jetforce server will pass environment variables like QUERY_STRING and host and remote connection information.
CGI programs can be written in a variety of programming languages. For this example I wanted to pass Raspberry Pi BME280 temperature, pressure and humidity sensor information to a Gemini CGI page.

The CGI program was written in Python and I installed a bme280 and figlet library:
pip install RPi.bme280
pip install pyfiglet
The Python script (sersor2gmi.py) outputs a Gemini content type, a Figlet title and then the sensor data values:
#!//usr/bin/python3
#
# sersor2gmi.py - send BME280 sensor data to a Gemini page
#
import smbus2
import bme280
import pyfiglet
# find device address via: i2cdetect -y 1
port = 1
address = 0x77
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)
# the sample method returns a compensated_reading object
data = bme280.sample(bus, address, calibration_params)
# Output a Gemini page
print("20 text/gemini") #Note: Some Gemini CGI servers may do this
print("```") # use code mode
#print(pyfiglet.figlet_format("Pi BME280 Data", font = "small"))
print(pyfiglet.figlet_format("Pi BME 280 Data", font = "small"))
print("Temperature:" + "{:5.1f}".format(data.temperature) + " C" )
print("Pressure: " + "{:5.1f}".format(data.pressure) + " kPa" )
print("Humidity: " + "{:5.1f}".format(data.humidity) + " %" )
print("```")
This file was added to the cgi-bin directory and it is made executeable (chmod +x sersor2gmi.py).
Below is the output seen in the Lagrange Gemini browser:

Final Comments
There are a variety of Gemini browsers for Windows, Android, Mac and Linux so if you’re looking for a quick and dirty internet solution Gemini might be a good solution.
I like how Gemini documents are totally readable, I can’t say the same for most web pages.
The one thing that I missed with Gemini pages is the ability of show nice charts, text based bars work for simple stuff but doing text based line charts is a little too ugly for me.