multipart-mixed

Code Snippet: Unit Test Green/Red Bar for ANSI Terminals

If you like writing unit tests but you don't have a fancy "green bar/red bar" unit test window, here's a simple solution you can use in any ANSI terminal window. It looks like this:

Green/red bar screen shot

It's a piece of cake with some ANSI escape codes. Here's the code:

#include <stdio.h>

void printTestResults(int numPass, int numFail)
{
    if (numFail > 0)
        printf("\x1b[37;41m"); /* white on red */
    else
        printf("\x1b[30;42m"); /* black on green */

    printf("\x1b[2K");         /* clear to end of line */

    printf("Test results: %d pass, %d fail (%d total)\n", 
        numPass, numFail, numPass + numFail);

    printf("\x1b[0m");        /* reset colors */
    printf("\x1b[2K");        /* clear to end of line */
}

Comments

ANSI terminals are one of the best ways to minimize the excess use of computing power. Not everyone needs full-fledged computers. For those that use computers for simple tasks, an inexpensive terminal connected to a server would not only save costs but would also help save the environment by saving a lot of energy in the long run. At the green news, we get people together who are interested in ecological issues, in order to make a lasting impact on our environment.

ANSI terminals are one of the best ways to minimize the excess use of computing power. Not everyone needs full-fledged computers. For those that use computers for simple tasks, an inexpensive terminal connected to a server would not only save costs but would also help save the environment by saving a lot of energy in the long run. At the green news, we get people together who are interested in ecological issues, in order to make a lasting impact on our environment.

Post a comment