Formatting results for the terminal
Formatting results is a tricky topic, and there are many different ways to approach it. One thing we want to be particularly sensitive to is the Unix philosophy of assuming that the output of our application will be immediately consumed by another (such as grep or sort or similar), and as such, we should be sure that our output is predictable and machine-readable. On the other hand, we also want our output to be human-readable if it is just printed to the terminal or written to a file. We may even want to change the behavior slightly depending on whether the output is actually a terminal or a file or another program (but we won’t do this here). In all of this, we want to keep the actual code relatively simple.
The most flexible approach, which would be necessary if we wanted to support a variety of output formats, would be to provide an abstract interface for printing results. This would allow us to provide several implementations that...