Building an SEO Friendly Command Line App with Go: lolcat
Do you enjoy working with Command Line Interface (CLI) applications? If so, then you’re going to love this tutorial on building a CLI app with Go called lolcat.
While searching for some terminal applications for inspiration, I stumbled upon lolcat. The original version can be found at https://github.com/busyloop/lolcat, but there are already several Go implementations available. Here are some examples:
Now, you might be thinking, why would anyone want to build something so useless? Well, sometimes it’s just fun to build something for the sake of it. So let’s get started!
First, let’s start by printing some values on the screen. Then, we’ll move on to coloring them, and finally, we’ll look into accepting user input to work as a pipe.
To generate some random output, I’ll be using the Faker library, which you can get by running the following command:
1 | go get -u github.com/enodata/faker |
The following Go code uses the Faker library to generate a number of phrases and prints them on the screen:
1 | package main |
Unfortunately, the output is displayed in boring black and white. Let’s add some color to make it more interesting. We can do this by using escape character sequences in fmt.Printf
to print the strings in gold color (#FFD700), which has an RGB color code of (255, 215, 0):
1 | package main |
Now the output is displayed in a shiny gold color.
But why stop at just one color? Let’s introduce a rainbow effect to make it even more exciting. We can achieve this by modifying the code as follows:
1 | package main |
Now the output is displayed in a beautiful rainbow of colors.
But what if we want our program to work as a pipe for other programs? Instead of providing its own output, we can modify the program to read content from os.Stdin
and apply the rainbow effect to it. Here’s an example of how to do that:
1 | package main |
Now the program reads one rune at a time from os.Stdin
and applies the rainbow effect to it.
If you want a more efficient approach, you can apply the rainbow effect “just in time” as each rune is scanned, like this:
1 | package main |
It works the same way as before, but it applies the rainbow effect as each rune is read.
Now, let’s have some fun with fortune and cowsay. Here’s an example of how we can use lolcat as a pipe with these programs to generate colorful output:
1 | fortune | gololcat | cowsay |
And here’s an example of using it with cowsay and the “meow” option:
1 | cowsay -f meow | gololcat |
Finally, to make this a system-wide command, you can build and install it using the following commands:
1 | go build |
The command will be accessible as gololcat
. Now you can enjoy your rainbow-colored terminal output everywhere.
Tags: Go, CLI, lolcat, terminal applications