M_framework__attr(3f) - [M_framework__attr::INTRO] control text attributes on terminals (LICENSE:MIT)
Synopsis
Description
Access
Limitations
Examples
Author
License
See Also
use M_framework__attr, only : attr, attr_mode, attr_update
M_framework__attr(3f) is a Fortran module that uses common ANSI escape sequences to control terminal text attributes.
use M_framework__attr write(*,*)attr(<red>Red Text!</red> <green>Green Text!</green>) endIt is designed to use three simple procedures to
+ Specify attributes using simple HTML-like syntax + allow the sequences to be suppressed when desired + permit the user program to completely customize the keywords. The user can add, delete and replace the sequences associated with a keyword without changing the code.One advantage of the approach of using formatting directives which are replaced with in-band escape sequences is that it is easy to turn off when running batch.
Another important capability is that programs can be run in "raw" mode and create a simple text file with the formatting directives in it that can then be read back in by a simple filter program that strips it back to plain text( see app/plain.f90), or displays it to a screen in color(see app/light.f90) or perhaps converts it to another format.
So this approach makes it trivial to read specially-formatted data from a file like a message catalog (perhaps with various versions in different languages) and colorize it or display it as plain text
By making each line self-contained (by default) lines can be filtered by external utilities and still display correctly.
Via git(1):
git clone https://github.com/urbanjost/M_framework__attr.git cd M_framework__attr/src # change Makefile if not using one of the listed compilers make clean; make gfortran # for gfortran make clean; make ifort # for ifort make clean; make nvfortran # for nvfortranThis will compile the M_framework__attr module and example programs.
Alternatively, via fpm (see https://github.com/fortran-lang/fpm):
git clone https://github.com/urbanjost/M_framework__attr.gitor just list it as a dependency in your fpm.toml project file.
[dependencies] M_framework__attr = { git = "https://github.com/urbanjost/M_framework__attr.git" }
o colors are not nestable. o keywords are case-sensitive, o ANSI escape sequences are not universally supported by all terminal emulators; and normally should be suppressed when not going to a tty device. Therefore, you should use M_system::system_istty(3f) or the common Fortran extension ISATTY() to set the default to "plain" instead of "color" when the output file is not a conforming terminal. On basic MSWindows console windows, it is best to use Windows 10+ and/or the Linux mode; you may have to enable ANSI escape sequence mode on MSWindows. It does work as-is with CygWin and MinGW and Putty windows and mintty(1) as tested.
Sample program
program demo_M_framework__attr use M_framework__attr, only : attr, attr_mode, attr_update implicit none character(len=256) :: line character(len=*),parameter :: f=( & &" <bo><w><G> GREAT: </G></w>& &The new value <Y><b>",f8.4,1x,"</b></Y> is in range"& &) real :: valuewrite(*,(a))& &attr( <r><W><bo> ERROR: </W>red text on a white background</y>)
value=3.4567 write(line,fmt=f) value write(*,(a))attr(trim(line))
! write same string as plain text write(*,*) call attr_mode(manner=plain) write(*,(a))attr(trim(line))
call attr_mode(manner=color) ! use pre-defined or user defined strings write(*,*) write(*,(a))attr(<ERROR> Woe is nigh.) write(*,(a))attr(<WARNING> The night is young.) write(*,(a))attr(<INFO> It is Monday)
! create a custom mnemonic call attr_update(MYERROR,attr(& <R><e> E<w>-<e>R<w>-<e>R<w>-<e>O<w>-<e>R: </e></R></bo>& )) write(*,*) write(*,(a))attr(<MYERROR> my custom message style)
end program demo_M_framework__attr
John S. Urban, 2021
attr(3f), attr_mode(3f), attr_update(3f)Related information:
terminfo(3c), termlib(3c), tput(1), reset(1), clear(1), console_codes(4), ECMA-48, https://en.wikipedia.org/wiki/ANSI_escape_code
Nemo Release 3.1 | M_framework__attr (3) | February 23, 2025 |