Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28439 Discussions

How to use colored prompt in Fortran application


Hello there, 

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)

Labels (1)
0 Kudos
10 Replies
Honored Contributor I

Perhaps the "foul" package is something you could use -

0 Kudos
New Contributor I
I downloaded and had a play with foul, but it did not work on my Windows 10 system until I started with an "call execute_command_line('cls')" otherwise it worked fine
0 Kudos
Honored Contributor II

I wrote my own simple wrapper around the sdk routine "SetConsoleTextAttribute" and also use the WriteConsole function.

0 Kudos
New Contributor II

I think it should work. Take a look here

Valued Contributor III

Back in the late '80s, I wrote some software that use ANSI controls in DOS to control placement on the screen.  I recently tried it on the new ANSI cmd prompt and it appears to work.  

I have only just looked at it briefly , but this might help.  


0 Kudos
New Contributor II

A discussion about color from fortran including code accessible as an fpm(1) package and links to several other resources



0 Kudos
New Contributor III

As Andrew_4619 does,  I use the functions of the WindowAPI. Enclosed I have added a smal example.




0 Kudos

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?

0 Kudos
Honored Contributor II

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.


0 Kudos
Valued Contributor III

@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. 


----- background-----


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. 



    CALL set_ansi




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. 



0 Kudos