home ~ projects ~ socials

Add Color To The End Of Cargo Watch Tests In Rust

I couldn't figure out how to get a nice color splash at the end of cargo watch test directly. Here's how I'm doing it with a shell script that I call from cargo watch.

The Script

cargo-report

#!/bin/bash

tput clear
for i in $( seq 1 $(tput lines))
do
  echo ""
done

cargo test -q -- --nocapture

if [[ $? -eq 0 ]]
then 
  tput setaf 28
else
  tput setaf 124
fi

export BAR=$(($(tput cols) * 2))
for i in $( seq 1 $BAR); do printf '%s' '█'; done
tput sgr0

The Launcher

This is the command to run to start watching for changes and running the script when something happens. You can run it from the command line. I drop it into its own script to make it easier to launch via tab completion.

start-tests

cargo watch -q -s "./cargo-report"

TODO

    discusse pre-cognative visualization

    note about how this uses tput on a mac to get the number of columns. not sure about its availablity on other platforms

    note about part that clears the screen. tbd on if I stick with that or not, but I like removing the prior runs so I'm not seeing that status. Could also put some type of ascii art there so it's not just blank for shorter runs

    I spent two hours figuring out how to put this together. Totally worth it.

    I tried doing stuff with the ANSI escape codes. It was working but I like tput better

    Another thing that's nice about the spaces is when you scroll up it's easy to find the start of each run so you don't accidentally look at errors from a prior run.

-- end of line --

References