The Practical Way to Visualize Stylish Fonts in the Command Line

Prerequisites
figletfzf
figlet is a command-line utility to display large letters out of ordinary characters. You can learn more about it here. A typical output looks like this:

So what's the problem?
There are many types of "fonts" available to choose from. You can select them using the -f flag. For example, to use the "avatar" style, run the following command:
figlet -f avatar hello world!
But you don't want to run the figlet command with every single style and see the output before deciding which one to use. I faced this issue where I wanted to print texts with different styles but I didn't want to go through a long list of styles before picking just a few out of them.
The Solution
In order to quickly preview all available styles, I created a script that utilizes fzf (learn more about this amazing tool here) to navigate through the different styles and preview the entered text with that style applied.
Here is the script file:
#!/usr/bin/env bash
figlet -I2 \
| xargs ls \
| grep ".flf$" \
| sed "s/\.flf//" \
| fzf --preview="figlet -f {} $1" \
| xargs -I {} figlet -f {} $1
Let's dissect this command step by step:
figlet -I2The
-I2flag retrieves the directory where the font files (with the.flfextension) forfigletare located.xargs lsThe output from
figlet -I2(the directory path) is piped intoxargs, which then executeslsusing that directory path as an argument. This command lists all files in the font directory.grep ".flf"Since there are other types of files present in the fonts directory, we filter out those that have the
.flfextension.sed "s/.flf//"sedis another powerful command-line utility. Here, using regular expressions, we replace the.flfin the file names with empty strings. This cleans up the list to show just the font names, making it easier for the user to understand which fonts are available.fzf --preview="figlet -f {} $1"The list of font names is passed to the
fzfcommand, allowing you to search through a list with real-time query updates. The--previewoption lets us render a preview of how the input text (passed as$1argument to the whole command) would look in each font. The{}is a placeholder for each font name dynamically replaced byfzf.xargs -I {} figlet -f {} $1Finally, the selected font (represented by
{}) is used to invoke thefigletcommand to print the input text (again, the$1argument) with the selected style.
Usage
In order to use this script, simply make it executable and then invoke it by passing your input string. Since I named my script as fig, the command for me looks like:
cd /path/to/the/script/file
chmod +x ./fig
./fig "desired text"
Conclusion
The figlet command has a lot of great ASCII art fonts at its disposal but previewing them all on your input text is time-consuming. By using this script, users can efficiently browse through all the available fonts, preview what their specific text looks like in each font, and select the font that best fits their needs, all within a few keystrokes in the terminal.

