0

I'm working on making some of my code cross platform, which means I'm battling windows issues. I'm currently having issues getting colors to display in the terminal.

I mostly use pygments to work with colors. When I enter:

python -c "import pygments.console; print(pygments.console.colorize('red', 'hi'))"

in the win32 console (or git bash for that matter) I get this:

    [31;01mhi[39;49;00m 

However, If I open up IPython and enter the same commands it works:

$ ipython
Python 3.6.3 |Anaconda, Inc.| (default, Nov  8 2017, 15:10:56) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import pygments.console

In [2]:  print(pygments.console.colorize('red', 'hi'))
hi

In [3]:

Note the hi does print out as red in this example.

How is this working? Is IPython doing something clever and replacing ansi sequences with windows codes from colorama when text gets written to stdout? Or is IPython doing something to the terminal that allows ansi to work?

Either way, is there a way I can make the pygments colorize function behave well in a windows environment?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Erotemic
  • 4,806
  • 4
  • 39
  • 80

1 Answers1

0

It seems there is a stdout intercept method being used. Its not IPython that does it it is colorama itself. I guess I misunderstood what that module was doing.

To get ansii colors to display correctly in a win32 cmd terminal executing

import colorama
colorama.init()

will cause ansi to work for the rest of the session.

Erotemic
  • 4,806
  • 4
  • 39
  • 80