Coloured terminal output from Fortran

deepak picture deepak · Jun 19, 2011 · Viewed 7.5k times · Source

My program outputs state of computations to the terminal and includes quite a bit of information. I would like to, if possible, color code parts of the text.

I have seen how it can be done in Bash and C++ by referring to threads on this site. However, I have not been able to use any of that to achieve the same result in Fortran (modern). For example, I tried this sample code, which I thought should work:

PROGRAM test
    PRINT*, 'A great color is \033[95m pink \033[0m.'
END PROGRAM test

I would have expected the output to be "A great color is pink" where pink is colored pink. Instead I get "A great color is \033[95m pink \033[0m." I don't understand what I am missing.

If I replace the print line in the code with: CALL EXECUTE_COMMAND_LINE("echo 'A great color is \033[95m pink \033[0m.'") then I get the output as desired. However I wouldn't want to keep calling on echo from my code. Is there any way I can get colored output?

Thanks!

Answer

Don Roby picture Don Roby · Jun 19, 2011

The escape character representation as '\033' doesn't appear to be working for you. I don't have fortran handy to check, but you might try explicitly using the character instead of the c-style escaping by calling the char conversion function, i.e., make the actual character by calling char(27) and build it into your output string in the correct places.