1. Georg Brandl
  2. pygments-main
  3. Issues
Issue #770 new

Support ANSI (ECMA-48) color-coded text input

Michał Górny
created an issue

I feel like many pastebin users would appreciate such a feature in pygments: reading (already colored) data stream with ECMA-48 color codes embedded (like console program output) and outputting it in any format pygments support.

I guess such a feature would require adding a new token types to represent console colors.

If there's interest in this, I can assemble a patch for it.

Comments (14)

  1. Tim Hatch

    I'd be interested in at least a 16-color version of this. Some of the other codes (like bold, reverse, etc) might overcomplicate it however. Can you get a basic version ready to review in the 1.7 time frame (guessing 6 months out)?

  2. Michał Górny reporter

    I'm sorry but I'm overburdened with work all the time, and this one doesn't come as a priority to me. I will be happy to work on this but I don't know when I'll have a free hand.

  3. Dejan Muhamedagic

    I started working on this, but I'm not entirely sure how to proceed. My first idea was to replace ANSI color codes with RGB color codes, but I'm not sure how to do that, i.e. how to insert those RGB color codes into the output. Can a lexer do that kind of preprocessing?

    Or is it necessary to have this kind of token rules:

    (r'%s(3[0-7])m(.*?)%s%s' % (esc, esc, reset_re), ansi2rgb)

    where ansi2rgb would lookup and yield appropriate color tokens. If so, where would we define these tokens? All tokens are defined in various styles, but that seems like an overkill.

  4. Michał Górny reporter

    Well, my initial idea was to introduce new token types representing the 16 standard colors, then mapping them to real colors via style. This would also allow overriding colors properly for light background.

  5. Dejan Muhamedagic

    On Tue, Feb 03, 2015 at 04:48:27AM -0000, Clément Pit--Claudel wrote:

    This is something I put together which could give me usable output from files generated by the script(1) program:


    However, it supports only 8 colors (30m-37m) and can parse only color codes which are not combined with other graphic control codes.

    It would be rather difficult to make a general graphics code parser with regular expressions, but I'm still not sure how to implement more complex parsers. Comments and suggestions would be very appreciated.

  6. Log in to comment