Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloured terminal output from Fortran

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!

like image 827
deepak Avatar asked Jun 19 '11 13:06

deepak


People also ask

What is Basic Input Output in Fortran?

Fortran - Basic Input Output. We have so far seen that we can read data from keyboard using the read * statement, and display output to the screen using the print* statement, respectively. This form of input-output is free format I/O, and it is called list-directed input-output.

How to print bright/bold red text in the terminal?

Once an appropriate ANSI sequence is sent to the terminal, all following text is rendered that way. Thus if we want all text printed to this terminal in the future to be bright/bold red, print ESC [ followed by the codes for "bright" attribute (1) and red color (31), followed by m

How do I set the terminal to render text in ANSI?

Here is a basic approach to set the terminal so that all following prints are rendered with a given color, attributes, or mode. Once an appropriate ANSI sequence is sent to the terminal, all following text is rendered that way.

What would the Python termcolor module do?

Would the Python termcolor module do? This would be a rough equivalent for some uses. The example is right from this post, which has a lot more. Here is a part of the example from docs A specific requirement was to set the color, and presumably other terminal attributes, so that all following prints are that way.


1 Answers

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.

like image 53
Don Roby Avatar answered Sep 19 '22 18:09

Don Roby