Skip to content
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

Colored console output #2308

Closed
Calinou opened this issue Jul 27, 2015 · 10 comments
Closed

Colored console output #2308

Calinou opened this issue Jul 27, 2015 · 10 comments

Comments

@Calinou
Copy link
Member

Calinou commented Jul 27, 2015

In an UNIX terminal, you can type this for example:

echo -e "\e[31;1mRed Bold Text"

However, this doesn't work in Godot, the sequence will be output as plain text:

print("\\e[31;1mRed Bold Text")

(There are two backslashes in order to actually print one backslash.)

It doesn't work with printraw() either.

Either the sequences should be supported, or, better, there should be a cross-platform way of colouring console output, as Windows supports a subset of console colours too. Possible uses are:

  • Highlighting errors and warnings
  • Easier to read logs
@OvermindDL1
Copy link

\e in your echo command is not two characters but is instead an escape sequence, thus doing \e will never work in any case like this.

@OvermindDL1
Copy link

Also, that is terminal specific, there are ways to use proper colors based on the detected terminal type.

@reduz
Copy link
Member

reduz commented Aug 2, 2015

yes this is not portable sadly

On Sun, Aug 2, 2015 at 7:53 PM, OvermindDL1 notifications@github.com
wrote:

Also, that is terminal specific, there are ways to use proper colors based
on the detected terminal type.


Reply to this email directly or view it on GitHub
#2308 (comment).

@akien-mga
Copy link
Member

I guess we could have a print_colored statement of some sort that would rely on the OS class to define how to implement colouring on the platforms that support it, or do a plain print on platforms that don't.

Basically we could use \E[#;#m on Unix (as used for error output as seen in https://github.com/godotengine/godot/pull/2737/files), and SetConsoleTextAttribute for Windows console (as in https://github.com/godotengine/godot/pull/2827/files).

Not sure if it's worth the pain though :)

@akien-mga akien-mga removed this from the Later milestone Jan 5, 2016
@Calinou Calinou mentioned this issue May 23, 2018
8 tasks
@Calinou
Copy link
Member Author

Calinou commented Nov 26, 2018

It looks like it's already possible, even though it's not particularly straightforward (but it could be abstracted by the user):

var escape = PoolByteArray([0x1b]).get_string_from_ascii()
var code = "[1;32m"
print(escape + code + "Bold green text")

Preview

This should support any ANSI escape sequence. I haven't tested this on Windows yet, but Windows 10 is able to interpret ANSI escape sequences so it should work on Windows 10 as well. However, it won't work in the Output panel (escape codes will appear as-is).

People writing libraries or plugins may also be interested in @GDScript.push_error(text) and @GDScript.push_warning(text), which can be used to make clickable errors/warnings appear in the Debugger panel in addition to standard output.

Edit: It seems ANSI escape code support should be enabled in the application code before it can work on Windows 10.

@Zireael07
Copy link
Contributor

Windows command line on any versions lower than Windows 10 does NOT support ANSI escape sequences, though.

@fire
Copy link
Member

fire commented May 26, 2019

Remember to be able to disable text colours in the startup flags.

@lifelike
Copy link

lifelike commented Oct 4, 2019

You could use something like the public domain licensed single-file https://github.com/agauniyal/rang library or find something similar. I only tested it in linux, but it is supposed to work for windows as well.

@Calinou
Copy link
Member Author

Calinou commented Oct 4, 2019

@lifelike Can that library work if you don't use the standard library to print text? Its README states:

Rang uses iostream objects - cout/clog/cerr to apply attributes to output text.

Also, in general, we tend to favor libraries that don't use the C++ standard library.

@akien-mga
Copy link
Member

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants