M_escape(3f) - [M_escape] substitute escape sequences for XML-like syntax in strings
Synopsis
Description
Major Features
Limitations
Future
Example
Alternate Direct Use
Alternate Functional Interface
Alternate Object Oriented
use M_escape, only : esc, esc_mode, update use M_escape, only : attr use M_escape, only : color, color_mode
M_escape is a Fortran module for using XML-like syntax to add attributes to terminal output such as color.ANSI escape sequences are not universally supported by all terminal emulators; and normally should be suppressed when not going to a tty device. This routine provides the basic structure to support such behaviors, or to perhaps in the future generate a CSS style sheet and HTML instead of text to the terminal, ...
Alternatively, direct use of the escape sequences is supported, as well as a functional interface, and an object-oriented approach.
The original concept was to allow formatting by using an existing XML library to allow the user to write HTML and to format it on a terminal like w3m, lynx, and link do. And in some ways this is an opposite approach in that it is directly formatting the text by using a similar syntax to directly generate text attributes; but it is a much simpler approach programmatically.
Typically, 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 terminal.
o Add ANSI terminal escape sequences with an XML-like syntax with ESC(3f). o suppress the escape sequence output with ESC_MODE(3f). o add, delete, and replace what strings are produced using UPDATE(3f).
o colors are not nestable, keywords are case-sensitive, o not all terminals obey the sequences. On Windows, it is best if you use Windows 10+ and/or the Linux mode; although it has worked with all CygWin and MinGW and Putty windows and mintty(1).
Full support for alternate output formats like HTML and popular markdown syntax. For exampleApparently have to make a stack of colors to allow nesting colors
ANSI HTML Markdown <h1></h1> # <h2></h2> ## <b></b> ** and ** <i></i> __ and __
How common are extensions like xterm-256 has to set RGB values for colors and so on?
Should a call to system_istty(3f) be built in to turn off escape sequences when a terminal is not present?
For color or pre-defined words a case statement could be used to call specific functions to support platforms like old Microsoft consoles that require a function call to assign text attributes instead of in-band ANSI escape control sequences. See the "Rosetta Code" web site for examples of generating color in Microsoft consoles.
Attributes are currently ended at the end of each call to esc(3f). Perhaps allow multi-line formatting?
Ultimately, an object-oriented package with capabilities like ncurses to define a "pad" and move and resize and format it would be ideal and very useful. Also see fixedform(3f) in the GPF (General Fortran Package).
It is a shame xterm(1) does not support pixel-oriented abilities to define a "graphics" area or support canvas(3c)-like in-band graphics, somewhat like Tektronix terminals, although it does have a Tektronix 4010 mode.
Perhaps overload + to replace //
Sample program
program demo_M_escape use M_escape, only : esc, esc_mode implicit none character(len=1024) :: line real :: value write(*,(a))& &esc(<r><W>ERROR:</W>This should appear as red text</y>) write(*,(a))& &esc(<y><B>WARNING:</B></y> This should appear as default text)value=3.4567 if( (value>0.0) .and. (value<100.0))then write(line,fmt=& &("<w><G>GREAT</G></w>:& &The new value <Y><b>",f8.4,"</b></Y> is in range"))value else write(line,fmt=& &("<R><e>ERROR</e></R>:& &The new value <Y><b>",g0,"</b></Y> is out of range"))value endif
write(*,(a))esc(trim(line)) ! write as plain text call esc_mode(manner=plain) write(*,(a))esc(trim(line))
end program demo_M_escape
Alternatively, you may use the escape sequences directly
program direct use M_escape, only : & ! FOREGROUND COLORS & fg_red, fg_cyan, fg_magenta, fg_blue, & & fg_green, fg_yellow, fg_white, fg_ebony, & & fg_default, & ! BACKGROUND COLORS & bg_red, bg_cyan, bg_magenta, bg_blue, & & bg_green, bg_yellow, bg_white, bg_ebony, & & bg_default, & ! ATTRIBUTES & bold, italic, inverse, underline, & & unbold, unitalic, uninverse, ununderline, & & reset, & ! DISPLAY & clear implicit none write(*,(*(g0)))fg_red,bg_green,bold,Hello!,reset end program direct
If you prefer a functional interface, use the attr(3f) function with the same keywords as with the esc(3f) function. Note that esc_mode(3f) will work with this function.
program functional use M_escape, only : attr, esc_mode implicit none call printme(color) call printme(plain) call printme(raw) contains subroutine printme(mymode) character(len=*),intent(in) :: mymode call esc_mode(mymode) write(*,(a))mymode write(*,(*(g0)),advance=no) & & attr(red:BLUE:bold,Hello!), & & and everything is back to defaults or , & & attr(RED:blue:bold),Hello Again!, & & attr(/BLUE), Well, the text color is still on.,attr(reset) write(*,(*(g0)),advance=yes) Back to normal writes. end subroutine printme end program functional
M_escape (3) | March 11, 2021 |