Reputation: 2075
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!
Upvotes: 17
Views: 10239
Reputation: 181
If you are compiling with ifort you need to compile using "-assume bscc", only then you can use
PRINT*, 'A great color is \033[95m pink \033[0m.'
codes are:
[90m=dark grey [30m=black
[91m=peach [31m=red
[92m=light green [32m=green
[93m=light yellow [33m=yellow
[94m=light blue [34m=blue
[95m=pink [35m=purple
[96m=light aqua [36m=aqua
[97m=pearl white
Upvotes: 7
Reputation: 1867
This is kind of an old question, but I figured I'd throw in my answer in case anyone comes along later (as I did) looking for an answer to this.
I had a similar problem as you did, trying to get escape sequences to work. I ended up going to the man page for gfortran. searching for 'escape' lead me to the compiler option '-fbackslash'. From the man page:
Change the interpretation of backslashes in string literals from a single backslash character to "C-style" escape characters. The following combinations are expanded "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\", and "\0" to the ASCII characters alert, backspace, form feed, newline, carriage return, horizontal tab, vertical tab, backslash, and NUL , respectively. Additionally, "\x"nn, "\u"nnnn and "\U"nnnnnnnn (where each n is a hexadecimal digit) are translated into the Unicode characters corresponding to the specified code points. All other combinations of a character preceded by \ are unexpanded.
So, to get escape sequences to work in Fortran, all we need to do is compile with this option. The one thing that's different is that we have to use hexadecimal numbers along with x instead of octal numbers. In this specific case, instead of \033
we use \x1B
. For example, PRINT *, "\x1B[31mThis text is red.\x1B[0m
will print some text in red.
I think this method is definitely preferable to concatenating a bunch of individually defined characters each time we want to use color.
Upvotes: 6
Reputation: 46356
I have just stumbled onto the foul module/library which seems to do exactly what you want. I haven't used it yet but will be giving it a go soon as having formatted terminal output from my Fortran programs would be very useful.
Upvotes: 5
Reputation: 41135
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.
Upvotes: 20