Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A script to accept stdin (expecting typical output from running a .t file and redirecting the output) and colorize it for better readability
#!/bin/bash
# For reference, a list of available color choices
# Black 0;30 Dark Gray 1;30
# Red 0;31 Light Red 1;31
# Green 0;32 Light Green 1;32
# Brown/Orange 0;33 Yellow 1;33
# Blue 0;34 Light Blue 1;34
# Purple 0;35 Light Purple 1;35
# Cyan 0;36 Light Cyan 1;36
# Light Gray 0;37 White 1;37
# color definitions
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
WHITE='\033[1;37m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Reset the colors if killed
trap 'printf "${NC}"' SIGINT SIGTERM
# Set up regexes so indentation is irrelevant
space_regex='^[[:space:]]*#';
not_ok_regex='^[[:space:]]*not ok';
ok_regex='^[[:space:]]*ok';
while IFS= read -r line
do
# Comments
if [[ $line =~ $space_regex ]]
then
printf "${YELLOW}$line\n"
# Failed tests
elif [[ $line =~ $not_ok_regex ]]
then
printf "${RED}$line\n"
# Passed tests
elif [[ $line =~ $ok_regex ]]
then
printf "${GREEN}$line\n"
# Default color
else
printf "${WHITE}$line\n"
fi
done < "${1:-/dev/stdin}"
# ensure colors are set back to normal
printf "${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment