I recently had to check some data that had been reprocessed. By "check" I mean verify that the new totals match the old totals. The query itself was simple, but with lots of output (shown below) it was making my eyes go square:
Specifically I was looking for values that differed (indicated by the diff_* columns). Yes, I could have just added an extra condition to the query that would only show the difference but that's much less fun.
Before we continue, this is the not the default way SQLite3 outputs data. You can get a much more readable output (similar to psql) by setting some options in the sqlite session with dot commands:
Now that we have that out of the way, here is a much more pretty version:
If your next question is "Huh, I didn't know SQLite3 supported colors?" then you're right. It does not. However, bash does and by using ASCII escape codes we can show values in color.
This is the starting point (prints Hello World!):
Technically this works, but it's not very friendly to use. Also, you have to remember that 2 means green. So let's take it one step further.
A color table allows us to reference a color by name (rather than its integer) and also provides us with a cleaner template for printf:
Now we can do this:
We have got rid of most of the nastiness. This is the simplest interface I could come up with.
How does it work in a real example, you say? Here you go:
Format the output with negative numbers in red and positive numbers in green:
This time I am not using the column output. Explained below.
When SQLite3 renders the output it uses the character length, but not the visible character length. This causes the headings to not match up (as seen in the original image). It seems that SQLite3 truncates these values unusually in some cases so that the color runs onto the next line, or even chops off prefix characters on the next line.
Anyway, have a play with it an tell me how you go. :)