Feature Request #5411

Colored shell output for warnings / errors

Added by andythenorth about 7 years ago. Updated over 5 years ago.

Status:NewStart date:2013-04-09
Priority:NormalDue date:
Assignee:-% Done:


Target version:-


Patch attached colors a lang warning using ANSI colors

colored_warning.diff Magnifier (366 Bytes) andythenorth, 2013-04-09 20:48

Associated revisions

Revision 2095:e72d25e21123
Added by planetmaker almost 7 years ago

Feature #5411: Coloured warnings and errors in shell output (andythenorth)

Revision 2207:01988dfeee65
Added by planetmaker over 5 years ago

Change #5411: At least check for isatty when using the funky colour codes in warning output


#1 Updated by planetmaker over 6 years ago

I guess some more proper colouring would be nice, which works independent of terminal type: http://stackoverflow.com/questions/6195877/python-colour-printing-with-decorator-in-a-function

#2 Updated by Alberth over 6 years ago

Don't bother using hacky ansi escape sequences when you have a curses module right in the standard library http://docs.python.org/2/library/curses.html

#3 Updated by MinchinWeb over 6 years ago

I don't think curses is available for Windows. However, if you use a module like Colorama ( https://pypi.python.org/pypi/colorama ), just add these couple lines of code at the beginning of the Python script and coloured output should work on Windows, Mac, and Linux.

from colorama import init

#4 Updated by planetmaker over 5 years ago

This issue is left open as both, the suggestion by Alberth and MinchinWeb seem possibly to be the better solution than the currently implemented one.

The currently implemented solution breaks on some terminals which don't support those colour codes. E.g. see the jenkins logs on anything with a warning like https://jenkins.openttdcoop.org/job/yeti/39/console

Also see http://dev.openttdcoop.org/projects/nml/repository/revisions/01988dfeee65 (r2207)

<Alberth> https://docs.python.org/3/library/io.html#io.IOBase.isatty
<Webster> Title: 16.2. io — Core tools for working with streams Python 3.4.1 documentation (at docs.python.org)
<planetmaker> does, yes
<Alberth> The character encoding is platform-dependent. Under Windows, if the stream is interactive (that is, if its isatty() method returns True), the console codepage is used, otherwise the ANSI code page. Under other platforms, the locale encoding is used (see locale.getpreferredencoding()). <-- but this worries me (sys.stderr docs)
<Alberth> ie it's broken under Windows
<planetmaker> hm, yeah. As broken as currently
<Alberth> ok, the patch itself is fine, isatty seems also to be supported under windows
<Alberth> colouring itself is broken, but no more than it alsready was :p
<planetmaker> but agreed, the ncurses approach is probably better, if it works correctly also on windows (dunno)
<Alberth> but that was to be expected if you use hard-coded ansi escape sequences
<Alberth> you need something to query the system how to change colour
<planetmaker> yeah
<Alberth> curses is more oriented towards full-screen applications
<Alberth> ie before graphical applications became the standard
<planetmaker> I briefly looked at it before I made this quick-hack-fix :P Seemed more complicated for this issue
<DevZone> Project 2ccts build #311-push: SUCCESS in 1 min 37 sec: https://jenkins.openttdcoop.org/job/2ccts/311/
<Alberth> the proper solution may be to 1) not add anything when not a tty, and 2) add colour when a package like https://pypi.python.org/pypi/termcolor or colorama is detected
<Webster> Title: termcolor 1.1.0 : Python Package Index (at pypi.python.org)
<Alberth> there are probably more such packages
<frosch123> i checked 3 of them: termcolor, colorama, blessings
<planetmaker> hm, true. Such test should not be too difficult either
<frosch123> none were installed on my system by default :)
<planetmaker> yeah, that's the issue with colorama and termcolor which I checked, too :D
<Alberth> pypi is mostly all random python packages made by random people :)
<frosch123> random packages by random people installed randomly on random systems by random sysadmins :)
<planetmaker> :D
<Alberth> http://devs.openttd.org/~alberth/yumex_packages.png I can just install them
<planetmaker> yeah, but I still would not want them a hard requirement
<planetmaker> making them optional for good colour support would be nice and ok, I think
<planetmaker> but then it could well be that w/o the chosen one(s) there's no colour output. That's ok, I guess
<Alberth> it's better than refuse to compile due to lack of recolouring the errors :p
<planetmaker> :D

Also available in: Atom PDF