The Practical Way to Visualize Stylish Fonts in the Command Line

·

3 min read

The Practical Way to Visualize Stylish Fonts in the Command Line

Prerequisites

  • figlet

  • fzf


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:

  1. figlet -I2

    The -I2 flag retrieves the directory where the font files (with the .flf extension) for figlet are located.

  2. xargs ls

    The output from figlet -I2 (the directory path) is piped into xargs, which then executes ls using that directory path as an argument. This command lists all files in the font directory.

  3. grep ".flf"

    Since there are other types of files present in the fonts directory, we filter out those that have the .flf extension.

  4. sed "s/.flf//"

    sed is another powerful command-line utility. Here, using regular expressions, we replace the .flf in 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.

  5. fzf --preview="figlet -f {} $1"

    The list of font names is passed to the fzf command, allowing you to search through a list with real-time query updates. The --preview option lets us render a preview of how the input text (passed as $1 argument to the whole command) would look in each font. The {} is a placeholder for each font name dynamically replaced by fzf.

  6. xargs -I {} figlet -f {} $1

    Finally, the selected font (represented by {}) is used to invoke the figlet command to print the input text (again, the $1 argument) 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.