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

How to use colored prompt in Fortran application

BrunoAmaral
Novice
1,836 Views

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
Arjen_Markus
Honored Contributor I
1,791 Views

Perhaps the "foul" package is something you could use - https://sourceforge.net/projects/foul/

0 Kudos
Norman_K_
New Contributor I
1,716 Views
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
andrew_4619
Honored Contributor II
1,783 Views

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

0 Kudos
GVautier
New Contributor II
1,777 Views

I think it should work. Take a look here https://ss64.com/nt/syntax-ansi.html

JohnNichols
Valued Contributor III
1,737 Views

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
ur
New Contributor II
1,703 Views

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

https://github.com/urbanjost/M_escape

 

 

0 Kudos
Robert_van_Amerongen
New Contributor III
1,632 Views

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

 

Robert

 

0 Kudos
BrunoAmaral
Novice
1,458 Views

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
IanH
Honored Contributor II
1,449 Views

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
JohnNichols
Valued Contributor III
1,423 Views

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

 

 



    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. 

JMN

 

0 Kudos
Reply