-
Couldn't load subscription status.
- Fork 3k
Add io_ansi module #9940
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
base: master
Are you sure you want to change the base?
Add io_ansi module #9940
Conversation
CT Test Results 5 files 288 suites 2h 58m 4s ⏱️ Results for commit a6e4754. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
b17ffb1 to
a6e4754
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work
| -doc """ | ||
| Change background color to index color. `Index` 0-15 are equivilant to | ||
| the named colors in `t:background_color/0` in the order that they are listed. | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you have already thought about this but why not have:
foreground/1 and background/1 accept color atoms directly?
Then you could remove all Color() and Color_background() functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, good point! we can definitely do that!
| ?FUNCTION(negative_off). | ||
|
|
||
| -doc """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similiar to previous comment, do we want a style(Style::atom()) function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case I don't think so as the style does not add any additional information while background/foreground does.
| ?SPEC(cursor_report_position). | ||
| ?FUNCTION(cursor_report_position). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a cursor_get_position() -> {Col:integer(), Row:integer()} feasible?
So I don't have to parse the ANSI codes, when debugging?
Or do I do that with one of the terminfo functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be possible. The problem I have found is that this function is rather slow, so I'm not sure it has any practical usecases... also there are no terminfo function for parsing stuff... so I'd have to do that myself.
| Activate the alternate screen. | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a short description of what alternate screen do?
Most of the other stuff is self explanatory but this I would have to go to other docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do!
| [case Data of | ||
| <<Value:(byte_size(Value))/binary, Rest/binary>> -> | ||
| throw({Key, Value, Rest}); | ||
| _ -> | ||
| ok | ||
| end || Key := Values <- get_vts_mappings(), | ||
| Value <- Values], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is un-parseable for me :-)
Re-write with a fun() in the map-comprehension or something?
Co-authored-by: Dan Gudmundsson <[email protected]>
|
Great to finally see ANSI support in Erlang! 🙌 APIIn general, I think it would be nice if there was an API that is a bit more programmable. For example, consider the following hypothetical configuration: #{
background => blue,
position => {3, down}
}Code that uses this would have to be a bit convoluted: render_background(#{background := blue}) ->
io_ansi:blue_background();
render_background(#{background := red}) ->
io_ansi:red_background();
render_background(#{background := green}) ->
io_ansi:green_background();
% etc.
place_cursor(#{position := {N, down}}) ->
io_ansi:cursor_down(N);
place_cursor(#{position := {N, up}}) ->
io_ansi:cursor_down(N);
place_cursor(#{position := {N, forward}}) ->
io_ansi:cursor_forward(N);
% etc.or the unreadable A better API would perhaps be to parameterize as much of the different values as possible? E.g.:
One could even go further with the API in certain places to make it more command based:
Another feature I'd like to see is support for the widely accepted Name
Additional SupportResetThere are individual reset codes for many of the formatting options that are sometimes useful. For example, FormattingAlso, I don't see italic support in the module, that would be nice too 😉. There are a few additional formatting codes that are widely supported:
Screen StateOne thing useful when building terminal UIs is the possibility to save/restore the screen state: #!/bin/sh
tput smcup #save previous state
echo hello
sleep 3
tput rmcup #restore previous stateThese are In addition, there are also the alternate screen buffer that can be activated ( |
This PR adds a new module called
io_ansithat allows the user to emit Virtual Terminal Sequences (aka ansi sequences) to the terminal in order to add colors/styling to text, or create fullyfledged terminal applications.io_ansiuses the local terminfo database in order to be as cross-platform compatibly as possible.It also works across nodes so that if functions on a remote node calls
io_ansi:fwrite/1it will use the destination terminals terminfo database to determine which sequences to emit. In practice this means that you can call things in a remote shell session that usesio_ansiand it will properly detects that terminal sequences the target terminal can handle and will print using them correctly.At the same time all at-hoc ANSI escape sequence usage in Erlang/OTP has been migrated to use
io_ansiand the terminal application user's guide has been updated.