I am running a Fortran application using Intel Fortran compiler and wanted to print messages in the output terminal of program in specific colors. I figured out one way of doing so by using the intrinsic function execute_command_line, since printing the color codes with print wasn't working. However, it might slow down the application, as a system call is being made at runtime. Do any of you know another way of doing this? The instructions below are the ones that i used and worked:
msg = achar(27)//'[31mSomething: '//achar(27)//'[0m'
call execute_command_line("echo " // msg)
Hello, dear colleagues!
Thank you all for the messages and sorry for late reply on topic. I've seen all your answers. They are all interesting and i have some of them in mind for testing and integration with the application, however before doing that one question arised. Some time ago i tested this scheme of colors in Fortran using Visual Code as editor and gfortran as compiler. Using the simple Fortran print statement i was able to change the color of the characters displayed on the screen. At that time, i used the cmd terminal in Visual Code. Testing some similar code in an application in Visual Studio 2013, but this time using Intel Fortran as compiler, i did not get the change in color. In fact, as i stated in my post, i had to make a system call using execute_command_line subroutine and pass the command of the terminal as argument. Only then, i could change the color. Does this have something to do with the compiler?
Your program may need to tell the console window that it is running in that you want the console to interpret ANSI escape sequences. I suspect John Nichols misposted my example of this in a different thread. The capability to interpret ANSI escape sequences was added [back] to the Windows 10 console when they introduced Windows Subsystem for Linux. It may be enabled by default in more recent versions.
The brutal summary is that you need to need to call the SetConsoleMode Windows API, adding ENABLE_VIRTUAL_TERMINAL_PROCESSING to the modes. See the set_ansi procedure in the code in that other thread.
That API is probably being called for you at some stage as part of your EXECUTE_COMMAND_LINE approach.
@IanH , my apologies, I did not pick up that you were the author of the code I posted. Sorry, I was in a tearing rush and I did not think about posting it here, need to slow down.
All of my early programs used ANSI characters to control the placement of stuff on the DOS screen, we had home written sewer, drainage and water supply programs. The users had limited computer experience in 1988 and so I made the programs user friendly.
Windows came along and destroyed the ability to use the ANSI codes. I stopped developing these programs about that time, due to a change of job.
I was always interested in the ansi codes and they came back recently as you said, I have a new program that I need someone to use, it is simple and I did not want to take a month to put it into Windows form, and I do not like quick win.
My main reason was to clear the screen after the menu, lol still do not have that working, there are only so many days in the year.
I stumbled across your excellent code and tried it, it was good, so I am using it in my simple new program. I had tried several commercial packages for the main problem, and had a long chat with the MS people working on the problem, when I explained my problem they said not there yet. They are close but do not do what I want to do with the data. Plus Fortran is a lot faster than their code.
So thank you for the code, at the moment I use it to change the colour of the text to a nice deep red. I
Your code is then easy to use. I will put this into a simple subroutine so I get away from the pure form, shown below.
CH=CHAR(27) CALL set_ansi WRITE(*,'(1X,4A,A23\)')CH,'[5;15H',CH,'[31m','
Good job and thanks. Just got to find some time to determine control characters to clear the screen.
Interestingly, I am reviewing the findings against an FEM model - standard commercial package from Australia, I did a model of the Pont du Gard, as I have data from the bridge, even though the archeologists were upset that I just walked on and recorded, one does not do that in their world. The FEM model takes 24 hours to run to determine the first 250 modes to 4 Hz, it died when I tried past 4 Hz with a request for 500, filled the allowed space and stopped. That is the advantage of real data and pure Fortran you are using the appropriate tools, of course my grad students would try MATLAB or something similar. Professors teach to the lowest common simple method, it is destroying our education in a way. I was helping a student today, she looked at me and said why do they not teach this stuff it is so easy the way you show it. I had no answer for her.
Tp paraphrase the Brits, Fortran is dead, long live Python. I hate Python.