Colour up your terminal

From the start of the new year I’ve been investing some time in making my development box a bit nicer. I’ve been looking at different window mangers, tinkering with my Emacs setup and just trying to give my work computer a bit more personality / general niceness.

Well, easily the smallest thing I’ve done is add a couple of helper functions to my ~/bin/ directory that give echo a coloured output using Ansi Escape Codes. Nothing ground breaking by any stretch but its nice to be able to do this:

green 'green'
green

Or display a coloured message based on the exit status in a bash script.

false && green 'true' ||  red 'false'
false

Which will be rendered in the corresponding colour. It might not show well in Hugo via Org Babel… Have a picture instead. :-)

Figure 1: COLOURS!

Figure 1: COLOURS!

Anyway. I wanted this when trying to debug an intimately failing test. It was only failing about 15% of the time so I was running it in a bash for run in {1..X}; do [run test here; done loop. The test was a high level one with lots of output so instead of having to pay attention to that output I just checked the exit status and printed pass or fail.

for run in {1..10}; do run the test 2>&1 > /dev/null && green pass || red fail ; done

Which gave me time to keep looking into why adding a database index might be breaking the tests in the first place. :-)

Here are the helper scripts if you’d like them.

red

#!/bin/bash
echo -e "\033[31m${@}\033[m"

green

#!/bin/bash
echo -e "\033[32m${@}\033[m"

yellow

#!/bin/bash
echo -e "\033[33m${@}\033[m"

blue

#!/bin/bash
echo -e "\033[34m${@}\033[m"

Like I said, I put them in my ~/bin directory which is on my $PATH and then used chmod to make them all executable.

Thanks for reading! :D


[Tom Heyes]

311 Words

2023-28-01 00:00 +0000