Adding some color to your text messages

While format/3 is widely known, its related cousin that adds color is much less seen in the wild: ansi_format/3

While it may seem intimidating to use at first if you start using it for something as simple as just changing the color of the text from black to red or green for reporting test results, it quickly grows on you.

Here is an example of output from a test case that used ansi_format/3

image
Note: Our current version of Discourse does not allow plugins, so was unable to add the text color plugin and used a PNG to show the color. :slightly_frowning_face:

Actual text less color.

Test number: 5, Sets: [[a,b,c]], @1 base_extend([],[a,b,c]) , Expected_result: [[a],[b],[c]], Result: [[a],[b],[c]], Passed!!! 
.
Test number: 6, Sets: [[a,b],[c]], @2 base_extend([a,b],[c]) , Expected_result: [[a,c],[b,c]], Result: [[a,b,c]]

Example Prolog

test(2,[condition(true),forall(example_07_test_case_generator(Test_number,Sets,Expected_result))]) :-
    format('~nTest number: ~w, Sets: ~w',[Test_number,Sets]),
    (
        sets_extend(Sets,Result)
    ->
        (
            Result == Expected_result
        ->
            format(', Expected_result: ',[]),
            ansi_format([bold,fg(green)],'~w',[Expected_result]),
            format(', Result: ~w',[Result]),
            format(', Passed!!! ~n',[])
        ;
            format(', Expected_result: ',[]),
            ansi_format([bold,fg(red)],'~w',[Expected_result]),
            format(', Result: ~w~n',[Result])
        )
    ;
        ansi_format([bold,fg(red)], 'Failed!!! ~n', [])
    ).

While it is not readily apparent in reading the example messages, there were actually around 60 of these messages and in the messages are the critical predicate @2 that would sometimes pass and sometimes give an invalid result. By seeing all of the results on one page with the input to the predicate, the expected result, the invalid results and the color coding, it was very easy to spot the needed pattern to segregate the valid code from the code that needed more work. Without the color coding required reading and remembering many of the details and was becoming tedious.

2 Likes