-
Notifications
You must be signed in to change notification settings - Fork 187
ANSI Colors support #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think something along the The ANSI escape sequences for colors work out of the box on Linux / macOS, but must be turned on to work on Windows. So one needs some basic terminal support. I created this library in C++ to do colors on all platforms as well as keyboard input: https://github.com/certik/terminal This allows to create things like interactive prompts, get arrows working for cursor movements, and to draw anywhere on the screen. Similar to the |
I like the FACE interface. I have a similar library but it is not as completely or nicely developed. For something fancier than colors I generally use ncurses from Fortran http://www.urbanjost.altervista.org/LIBRARY/libscreen/ncurses/pdsrc/ncurses_from_Fortran.html but I think that is a bit beyond the stdlib but would be appropriate for fpm if it supported C wrappers as well as Fortran. Specifically for xterm(1) I have a library that lets you set fonts, window size and position, window title, etc. The esc(1) program in the GPF collection actually combines the two
|
@urbanjost yes, I agree that going beyond colors should go into a separate fpm package. I would recommend in general to not depend on platform specific libraries, such as ncurses (which doesn't work on Windows in the native I agree that the reason that can be done is that nowadays everybody supports some subset of the ANSI escape sequences, and so one can just use those. |
I put M_escape I found it interesting to see what language have color built-in and how many have a standard module or library for it in Rosetta Code. |
That seems similar to the approach here:
https://python-prompt-toolkit.readthedocs.io/en/master/pages/printing_text.html#html
It works. But it's slower, because you have to parse the string. So it depends on the application.
…On Sat, Sep 12, 2020, at 8:15 PM, urbanjost wrote:
I put M_escape <https://github.com/urbanjost/M_escape>
image
<https://user-images.githubusercontent.com/29845229/93008620-c2065e80-f544-11ea-84d1-0fbba159c769.png> onto github as it uses a different API model than the others referenced so far, although the parsing method used is just a prototype it is functional for basic string coloring.
I found it interesting to see what language have color built-in and how
many have a standard module or library for it in Rosetta Code.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#229 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAAFAWBXFUPNUIPL4BX5VCLSFQTMBANCNFSM4Q74M77Q>.
|
Speed is not always an issue in that you can process fixed strings once, as in |
The advantage of the approach of replacing in-band escape sequences I need to add some example filters (a loop that reads the "raw" files and displays them as plain text or as color on screen would liternally be a read/write loop that calls the esc(3f) routine, but an HTML and PDF example ala like the M_ncurses does for ncurses dumps (to HTML) or the asa2pdf(1) program does for "ASA carriage control" might make a stronger case); but have not seen a lot of interest in this so far(?) so it is on the "one of these days" lists for now. People can write HTML pretty easily from Fortran, after all. This actually has some advantages because it is simple and assumes fix spaced output and keeps each line independent by default. So that is how I justify the parsing overhead. It compliments traditional batch use but still lets you throw a little sparkle in your output. |
It's a question what to use for markup to show colors. HTML is certainly one way to do that, used in To be honest though, I still kind of like the simplicity of just outputting ANSI sequences, as you don't have to worry about any kind of HTML, you just output the sequences directly, here is an example from C++: std::string text = "Some text with "
+ color(fg::red) + color(bg::green) + "red on green"
+ color(bg::reset) + color(fg::reset) + " and some "
+ color(style::bold) + "bold text" + color(style::reset) + ".";
std::cout << text << std::endl; You can use One way to design color output is to first provide the |
I use the ansi2html(1) bash shell as well. I changed M_escape to have not only the pseudo-XML mode, but a function-based and direct ANSI escape sequence mode and added an fpm(1) config for it and simple filters called plain(1) and light(1) to read the pseudo-XML files and print the output to stdout with and without color as examples for discussion. I was going to do an OOP interface too, but I think the other resources listed here cover that. Comments welcome. I put a list of resources that compliments those mentioned here as well. If anyone tries it I would be interested in a vote as to which interface people prefer. The README files shows an example of each of the three modes. |
@urbanjost thanks! I like your example as the lowest level API: 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 I think that works great. With this, and your html API, I think that covers most use cases. |
I think this kind of low level API is great, but we should keep in mind the complaint about the redirection issue too. So i think it should have a higher level API with a function that handles all the functionality: program
use stdlib_colors only: color & ! other suggestions: colorize, stylize...
fg_blue, sgr_bold
implicit none
write(*,*) color('Hello... ', fg='red', bg='yellow'), &
color("it's me you're ",fg='blue', bg='yellow'), &
color('LOOKING FOR?', fg='red', bg='yellow', style='bold')
write(*,*) color('I can see it in your <b>eyes</b>')
write(*,*) color('I can see it in your <y>smile</y>')
write(*,*) 'Tell me how to win your ', color('<3', 'red')
end program This would apply the escapes and the reset and handle the file redirection thing. |
Can you explain what you mean by this? Is it to change the output from ANSI sequences to HTML using the same code? The API you proposed is very good also, and it can be built on top of the low level API we discussed above. |
I mean the redirection of such outputs to a file, since they would write the ansi escapes sequences too, polluting it (see "Few remarks from the community" section of this issue). |
I see. I feel this must go into the API that is above the low level API. I see his architecture all over many of the other issues in stdlib. In this case, the API layers as I can see them could be (from lowest to highest):
|
It would be nice to have some sort of pre-alpha version of this module in stdlib. I'd certainly use it. I believe many programmers don't use colors, because colors are not cheap. He has to find a third party library, figure how to install and use. This process is not always easy. Writing |
I was hoping a few people would try it via |
Hi @urbanjost . I think you are right. I've tried M_escape with fpm and it works beautifuly on Windows. Yes, I use only basic color formatting (don't need cursor positioning). |
I added an fpm(1) package M_attr that concentrates on polishing up the HTML-like mode explored in M_escape, including an example of using the ISATTY extension from gfortran and ifort as an example of what was discussed above, as an alternative to the solution given, as it eliminates the need for a C interface in trade for using a non-standard but common feature. Made prettier examples; added support for a character array and a way to set a fixed right margin for non-default blocks with a background color set and a few other expansions on the original M_escape submode, including examples of pre-defined strings like "write(,)attr(' error message') for very simple basic but common use-cases. |
@zoziha, @arjenmarkus you can contribute to the issue on color support here (I've hidden your comments in #487 to keep the issue focused). |
For API inspiration, it might also be worth looking at what other languages do:
|
@ivan-pi thanks for the links. There is a longer list at the end of the README here: https://github.com/jupyter-xeus/cpp-terminal/ |
In #580, @urbanjost wrote:
I don't think this has been considered so far. so I thought I'd just expand on this idea. The first step would be to re-implement the stdlib string-type adding the necessary color and style fields: !> String type holding an arbitrary sequence of characters.
type :: colored_string_type
sequence
private
integer :: style
character(len=3) :: color
character(len=:), allocatable :: raw
end type string_type A new type constructor would accept foreground, background, and style arguments. Finally, the method stdlib/src/stdlib_string_type.fypp Line 1129 in 3af3259
could be modified to check if the unit is a teletype writer, and if yes print the escape sequences accordingly. If the interface of string type and the colored string type remained the same, you would in principle need to modify a single line:
A few issues with this approach:
|
Some interesting questions. I could picture not just color but screen position being an attribute, and so being able to build a panel to display on the screen as another aspect. An object-oriented ncurses library, so to speak. But that does complicate the simpler string operators, so maybe it needs it's own type. Not sure if it would still be useful to just treat it like the current stdlib_string_type for all existing syntax, or if that would be too non-intuitive. Appending a red string and a green string and ending up with an all red string seems non-intuitive, although as you mention operating with a regular string seems like it would be straight-forward. It would be nice to be able to not just color strings but to be able to easily create TLI interfaces with a Fortran-only (mostly) interface. CDC NOS and NOS/VE and VAX/VMS had those years ago and they were very useful at the time, but that was before X11 Windows and such. But having a string object with text attributes and position is appealing to me, as a screen in many ways is a great example of how OOP programming can be used to great advantage; I can picture an entire introduction to Fortran OOP programming using such a type. To truly be an extension and not change current behavior I think new operators like .oeq. would be needed and == would just compare the strings; not sure what // should do; returning a linked list solves some problems and introduces othersl |
Cannot find anything where anyone took the approach, but I keep thinking it is not only potentially useful but also easily accessible for demonstrating aspects of modern Fortran, as everyone has a terminal and you can see the results "graphically" on a screen, |
Currently there is a thread
<https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-use-colored-prompt-in-Fortran-application/m-p/1340123/emcs_t/S2h8ZW1haWx8Ym9hcmRfc3Vic2NyaXB0aW9ufEtXTEtZUU1FR00xSUtRfDEzNDAxMjN8U1VCU0NSSVBUSU9OU3xoSw#M158542>
on the Intel Fortran forum about coloured output. I suggested they have a
look at the foul library <https://sourceforge.net/projects/foul/>, but
there are other possibilities as well.
An object-oriented interface to ncurses does sound nice, though I have not
done anything with ncurses for a few decades ;).
Op di 30 nov. 2021 om 15:59 schreef urbanjost ***@***.***>:
… Cannot find anything were anyone took the approach, but I keep thinking it
is not only potentially useful but also easily accessible for demonstrating
aspects of modern Fortran, as everyone has a terminal and you can see the
results "graphically" on a screen,
and even maybe do some coarrays to allow for event processing.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#229 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN6YR4UPPOMOWTDBKMWVN3UOTRGFANCNFSM4Q74M77Q>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Colors Module
In order to support colors and styling (bold, underline and etc) I propose a small utility to handle colored output, namely
stdlib_colors
. The use cases can vary, but the main application would be the richer logger and errors which helps a lot to visually identify what kind of message is right away.Figure 1 - coloredlogs python library showing a use case
Few remarks from the community in #193
Proof of Concept and fixing the issue with redirection
isatty
function (from<unistd.h>
), ifisatty
returns true then we write the ANSI colors, otherwise no colors in the output.stdlib_system
module as a new function binding the C one (and alternatives to other platforms).Some other related projects that could inspire the API
The text was updated successfully, but these errors were encountered: