When you’re developing a terminal application, it’s usually helpful to indicate the progress of a long running task. Over the years, I’ve done this in a number of ways. Most of them involve printing some kind of status message, such as “Processing file1.csv…” or “Processing 10 of 1,000,000…” etc.
One technique that I’ve always liked, but never took time to learn, was displaying a dynamic progress indicator. Chances are you’re seen this yourself. It’s used in popular applications such as Git and Homebrew. The indicators come in various shapes and sizes. I’ve seen progress bars, percentages, spinners, and others.
The Technique In Action
I’ve created a simple example below so that you can see this technique in action. When you run it, you will see a percentage that dynamically updates in-place as it gradually increases from 0 to 100 over a period of 10 seconds. To try it, open your terminal, start a new irb session, and enter the code below.
How It Works
The above example probably looks very similar to code you’ve written in the past. What’s the secret? The key is the
\r character in the first argument of the printf command. This character is known as a carriage return. According to Wikipedia, a carriage return “moves the position of the cursor to the first position on the same line.” This means that each time printf is called inside the loop, the previous line is overwritten - in-place - by a new line. If the carriage return were not included, the new output would be continuously appended to the previous output and the result would look like this:
Percentage: 0%Percentage 1%...
Below, I’ve included some additional examples that I’ve seen before in other applications. I’ve also combined them into a gist on GitHub. Feel free to use them or extend them as you see fit.