Community
    • Login

    ANSI escape sequences color rendering

    Scheduled Pinned Locked Moved General Discussion
    13 Posts 4 Posters 12.8k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Xavier BeaumeX
      Xavier Beaume
      last edited by

      Hello,

      That feature request has actually been pretty well formulated in that thread (2018):
      https://community.notepad-plus-plus.org/topic/16612/interprete-ansi-color

      …but it has been locked since and never received any reply.

      Is it not relevant ? (why ?)

      It’s quite handy to capture a color-formatted stream out of a log handler into a file, and of course to be able to visualize it with rendering as it would be in a regular console (with type, more, less, etc.)
      …Notepad even offers “tail -f” as a real-time log tracer : wouldn’t it be natural to offer ANSI escape sequences colors (rather than the awful actual codes as in “[1;33m This is a warning ”)

      Thanks
      Xavier

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Xavier Beaume
        last edited by

        @xavier-beaume ,

        It was locked simply because it was old and inactive. As to why no one answered then, I don’t know.

        Notepad++ is a text editor, and those codes and the colors they produce in ANSI.sys environments are not text, they are binary sequences. Notepad++ does the best it can and renders those codes as if they were text characters.

        Someone could write a plugin that would take the raw source code and render it in a separate panel, like the Markdown++ or HTML Preview plugins do. But there are a lot of ANSI escape sequences besides colors, and not all make sense in a text editor.

        PeterJonesP 1 Reply Last reply Reply Quote 0
        • PeterJonesP
          PeterJones @PeterJones
          last edited by PeterJones

          @xavier-beaume,

          I had another idea: the EnhanceAnyLexer plugin allows you to use regular expressions (regex) to add foreground coloring to any lexer (whether builtin or a User Defined Language). (The plugin isn’t (yet) in the Plugins Admin list, so you have to manually install it from the github I linked.)

          If you have the text

          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          Initial text SomeTextGoeshereOther Text Goes Here
          

          You can set up an empty UDL called ANSIEscape, and then add the following to the EnhanceAnyLexerConfig.ini:

          [ANSIEscape]
          ; For Control Sequence Introducer, or CSI, commands, the ESC [ is followed by any number (including none) of "parameter bytes" in the range 0x30–0x3F (ASCII 0–9:;<=>?), then by any number of "intermediate bytes" in the range 0x20–0x2F (ASCII space and !"#$%&'()*+,-./), then finally by a single "final byte" in the range 0x40–0x7E (ASCII @A–Z[\]^_`a–z{|}~).
          0x808080 = \x1B\x5B[0-?]*[!-/]*[@-~]
          0x000000 = (\x1B\x5B(\d+;)?30m)\K(.*?)(?=\x1B\x5B0m)
          0x0000C0 = (\x1B\x5B(\d+;)?31m)\K(.*?)(?=\x1B\x5B0m)
          0x00C000 = (\x1B\x5B(\d+;)?32m)\K(.*?)(?=\x1B\x5B0m)
          0x00C0C0 = (\x1B\x5B(\d+;)?33m)\K(.*?)(?=\x1B\x5B0m)
          0xC00000 = (\x1B\x5B(\d+;)?34m)\K(.*?)(?=\x1B\x5B0m)
          0xC000C0 = (\x1B\x5B(\d+;)?35m)\K(.*?)(?=\x1B\x5B0m)
          0xC0C000 = (\x1B\x5B(\d+;)?36m)\K(.*?)(?=\x1B\x5B0m)
          0xC0C0C0 = (\x1B\x5B(\d+;)?37m)\K(.*?)(?=\x1B\x5B0m)
          

          Then it will format as per the following screenshot:
          0ae17b64-7c6c-46fc-8e2b-9a0143f49f66-image.png

          The EnhanceAnyLexer plugin won’t be able to affect background, or bold/italic/underline, but it can at least do the foreground color. (And obviously, this won’t handle position/etc… but I wouldn’t want it to while editing the source code for the ANSI-escaped document) If you want the 256-color variant, you would have to program another 256 regexes to handle it… I thought showing just the default 8 colors would be sufficient for demonstration purposes.

          In theory, with a similar block in the INI file, you would be able to set up the same rules for the MS-DOS Style builtin lexer, but it’s got a confusing name (MS-DOS Style in Language menu, Dos Style in Style Configurator, and MSDOS Style/ASCII Art in the status bar), and I couldn’t figure out the name to use in the [...] header

          –

          @Ekopalypse,

          1. Were you planning on adding EnhanceAnyLexer to NppPluginList, so that we can direct people to Plugins Admin rather than making them manually install it?

          2. For the multi-word named languages, like Fortran (free form), I think I found that it matches the prefix in the status bar (so Fortran free form). If that’s right, it should be made clear in the default .ini file that the group name must match the status-bar-text, not the name in the Language menu or Style Configurator.
            When I tried that rule for the MS-DOS group name, with [MSDOS Style/ASCII Art], that would not style it (nor would using names any of the three listed in the paragraph above)… but I don’t know if that’s just because the MS-DOS lexer does extra overrides on styling to change the font and encoding as well, and thus it overrides your plugin, or whether it’s because the status-bar-text-name has the / in it, which maybe confuses something in your INI parser. But unless it’s something that the MSDOS-lexer is doing to prevent your plugin from styling, it should probably be made to actually work with any of the builtin lexers

          EkopalypseE J_SwaJimeJ 2 Replies Last reply Reply Quote 4
          • EkopalypseE
            Ekopalypse @PeterJones
            last edited by Ekopalypse

            @peterjones said in ANSI escape sequences color rendering:

            Were you planning on adding EnhanceAnyLexer to NppPluginList, so that we can direct people to Plugins Admin rather than making them manually install it?

            Yes, but I have not yet checked what needs to be done to make this work with v lang and gcc.
            To do the tests, I am hesitant to install visual studio to get a debug version, but I couldn’t get gcc to build either. Hmm …

            For the multi-word named languages, like Fortran (free form), I

            You are right, basically I am asking Npp with nppm_getlanguagename for the programming language currently in use. I will make this clearer, thanks for the suggestion.

            By the way, there is an error_list lexer script for this particular problem and an 8.4 compatible version along with a FR that asks for an option to disable non-styling of larger text files. @Xavier-Beaume

            PeterJonesP 1 Reply Last reply Reply Quote 2
            • PeterJonesP
              PeterJones @Ekopalypse
              last edited by PeterJones

              @ekopalypse said in ANSI escape sequences color rendering:

              You are right, basically I am asking Npp with nppm_getlanguagename for the programming language currently in use

              Which, for a language called MS-DOS Style or a variant thereof everywhere else will “obviously” be “NFO” as the result from that call. Sheesh: Four different names for the same Language, depending one where you look!

              And yes, using [NFO] as the group name in the INI file will result in the MS-DOS Style language accepting EnhanceAnyLexer formatting.

              Thanks.

              —

              For future reference: The source-code for each of the four strings can be found in the repo:

              Text Source Notes
              MS-DOS Style Notepad_plus.rc#L929 & #L1054 Translated via id="46015", for example in esperanto.xml, though not all translations (including english.xml) provide that ID
              Dos Style theme.xml In many themes as <LexerType name="nfo" desc="Dos Style" ext="">, though langs.model.xml does not have the desc attribute
              MSDOS Style/ASCII Art ScintillaEditView.cpp#L82 _longName column
              NFO ScintillaEditView.cpp#L82 _shortName column
              EkopalypseE 1 Reply Last reply Reply Quote 2
              • EkopalypseE
                Ekopalypse @PeterJones
                last edited by

                @peterjones

                I wanted to add something like this

                ; Each configured lexer must have a section with its name,
                ; which can be seen in the first field of the status bar,
                ; followed by one or more lines with the syntax
                ; color = regular expression.
                

                Does that make sense?

                1 Reply Last reply Reply Quote 0
                • EkopalypseE
                  Ekopalypse
                  last edited by

                  updated version

                  ; Each configured lexer must have a section with its name,
                  ; which can be seen in the first field of the status bar,
                  ; (and in the case of a UDL, use only the part after "User defined language file - ")
                  ; followed by one or more lines with the syntax
                  ; color = regular expression.
                  
                  PeterJonesP 1 Reply Last reply Reply Quote 1
                  • PeterJonesP
                    PeterJones @Ekopalypse
                    last edited by

                    @ekopalypse ,

                    Looks good.

                    1 Reply Last reply Reply Quote 1
                    • PeterJonesP PeterJones referenced this topic on
                    • PeterJonesP PeterJones referenced this topic on
                    • J_SwaJimeJ
                      J_SwaJime @PeterJones
                      last edited by

                      @PeterJones

                      I just found this today and tweaked it to account for the “bright” colors and also to allow attributes to not hinder the matches.

                      I got this via perplexity.ai and it had comments on the end of the lines that made it not work.

                      Point here is multiple attributes can be combined and in any order.
                      So <esc>[2;48;33;1m was not matching for me even though it should.

                      I also found that the built-in language “ErrorList” removes the escape sequences from sight. Before that I had modified the UDF for <esc>(.*?)m to be comments and styled with the smallest font at 5. That helped reduce the noise and keep the color mods. I did not find any better way. I tried finding a way to reach out to converse but this platform says i don’t have enough experience :-P And I didn’t see any other way to contact.

                      You can see I had written some related stuff way back -> https://github.com/swajime/ansi

                      [ANSIEscape]
                      ; For Control Sequence Introducer, or CSI, commands, the ESC [ is followed by any number (including none) of "parameter bytes" in the range 0x30–0x3F (ASCII 0–9:;<=>?), then by any number of "intermediate bytes" in the range 0x20–0x2F (ASCII space and !"#$%&'()*+,-./), then finally by a single "final byte" in the range 0x40–0x7E (ASCII @A–Z[\]^_`a–z{|}~).
                      
                      ; Black text
                      0x000000 = (?:\x1B\x5B(?:\d+;)*?30(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Red text
                      0xAA0000 = (?:\x1B\x5B(?:\d+;)*?31(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Green text
                      0x00AA00 = (?:\x1B\x5B(?:\d+;)*?32(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Brown/Yellow text
                      0xAA5500 = (?:\x1B\x5B(?:\d+;)*?33(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Blue text
                      0x0000AA = (?:\x1B\x5B(?:\d+;)*?34(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Magenta text
                      0xAA00AA = (?:\x1B\x5B(?:\d+;)*?35(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Cyan text
                      0x00AAAA = (?:\x1B\x5B(?:\d+;)*?36(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Light Gray text
                      0xAAAAAA = (?:\x1B\x5B(?:\d+;)*?37(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Dark Gray text
                      0x555555 = (?:\x1B\x5B(?:\d+;)*?90(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Light Red text
                      0xFF5555 = (?:\x1B\x5B(?:\d+;)*?91(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Light Green text
                      0x55FF55 = (?:\x1B\x5B(?:\d+;)*?92(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Yellow text
                      0xFFFF55 = (?:\x1B\x5B(?:\d+;)*?93(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Light Blue text
                      0x5555ff = (?:\x1B\x5B(?:\d+;)*?94(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Light Magenta text
                      0xff55ff = (?:\x1B\x5B(?:\d+;)*?95(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; Light Cyan text
                      0x55ffff = (?:\x1B\x5B(?:\d+;)*?96(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      
                      ; White text
                      0xffffff = (?:\x1B\x5B(?:\d+;)*?97(?:;\d+)*m)\K(.*?)(?=\x1B\x5B0m)
                      0xe0e0e0 = \x1B\x5B[0-?]*[!-/]*[@-~]
                      
                      
                      PeterJonesP 1 Reply Last reply Reply Quote 0
                      • PeterJonesP
                        PeterJones @J_SwaJime
                        last edited by

                        @John-Simpson-0 said in ANSI escape sequences color rendering:

                        I also found that the built-in language “ErrorList” removes the escape sequences from sight.

                        ErrorList is new (wasn’t available in 2022 when these posts were written; it was just enabled this year, in fact). The ErrorList lexer obeys the View > Show Symbol > Show Control Characters & Unicode EOL toggle:
                        6cfe7014-a91f-43ed-8041-0452d3fa05b3-image.png

                        If that option is toggled on, ErrorList will show the sequence:
                        ad988be7-6986-4758-805d-8d485d2337cb-image.png
                        If that is toggled off, ErrorList will hide the sequence:
                        e689f09a-4cec-43d0-8e42-ceeab371697a-image.png

                        If you use the ErrorList for your lexer for files with the escape sequences, you no longer need the EnhanceAnyLexer to do that. But it’s up to you to decide which gives the best user experience.

                        J_SwaJimeJ 1 Reply Last reply Reply Quote 0
                        • J_SwaJimeJ
                          J_SwaJime @PeterJones
                          last edited by

                          @PeterJones

                          Thank you for receiving my post!

                          I just went through the exercise again with one of my clients. The ErrorList by itself does not do coloring of the text.

                          So with above I forgot some detail:

                          1. After adding UDL “ANSIEscape”, associate it with the extenstions you want ( log ) for instance for myfile.log
                            That makes the new UDL effective without being selected.
                          2. Change the Language to ErrorList and hide all from the show character type menu.

                          That is it … you get color without noise! :-)

                          Here is an image:
                          ANSIEscape.png

                          PeterJonesP 1 Reply Last reply Reply Quote 0
                          • PeterJonesP
                            PeterJones @J_SwaJime
                            last edited by

                            @J_SwaJime said in ANSI escape sequences color rendering:

                            The ErrorList by itself does not do coloring of the text.

                            As I thoroughly tested it when I was enabling that lexer in Notepad++, I am quite certain that ErrorList by itself does show the ESC[30m; - ESC[37m; colors correctly, or their ESC[1;30m; bold equivalents:
                            e15e34f7-2e47-4632-82d6-2735875b8659-image.png

                            or with escape codes hidden:
                            7eb34426-3029-4155-a836-ac3be68b3717-image.png

                            Notepad++ v8.8.1   (64-bit)
                            Build time : May  3 2025 - 18:41:09
                            Scintilla/Lexilla included : 5.5.6/5.4.4
                            Boost Regex included : 1_85
                            Path : C:\usr\local\apps\npp\npp.881.configUpdater\notepad++.exe
                            Command Line : "C:\usr\local\share\TempData\Npp\example.err" -multiInst -nosession -lerrorlist -n18 -c1
                            Admin mode : OFF
                            Local Conf mode : ON
                            Cloud Config : OFF
                            Periodic Backup : OFF
                            Placeholders : OFF
                            Scintilla Rendering Mode : SC_TECHNOLOGY_DEFAULT (0)
                            Multi-instance Mode : monoInst
                            File Status Auto-Detection : cdEnabledNew (for current file/tab only)
                            Dark Mode : OFF
                            OS Name : Windows 11 Home (64-bit)
                            OS Version : 24H2
                            OS Build : 26100.4349
                            Current ANSI codepage : 1252
                            Plugins : 
                                CollectionInterface (1.1)
                                ColumnsPlusPlus (1.2)
                                ComparePlus (1.2)
                                ConfigUpdater (2.0.1.2)
                                mimeTools (3.1)
                                NppConverter (4.6)
                                NppExport (0.4)
                            

                            notice that I don’t even have EnhanceAnyLexer installed on this copy of Notepad++

                            That said, ErrorList lexer doesn’t handle background escapes (neither does EnhanceAnyLexer), and it doesn’t handle the ESC[90m; variants of the bold (so-called “4 bit mode”), nor the 8-bit RGB color or 24-bit TrueColor mode. But since your screenshot seems to match my test document from years ago, you aren’t using those modes.

                            My guess is that you had an older version of Notepad++, wherein your stylers.xml got setup, then you updated to a newer Notepad++ with ErrorList enabled. When Notepad++ updates, it doesn’t overwrite your stylers.xml or customized themes (to avoid changing any customized colors)… but that means that you don’t get all the new styles defined correctly.

                            I am guessing that if you go into Style Configurator, you either cannot choose ErrorList from the Language: dropdown, or that you are missing some or all of the styles in the Style: chooser below it:
                            b12d87ba-d5ff-4f83-8ecf-7f4701e17d2b-image.png
                            There are a couple screenfulls of the varios styles with “Error” in the name, then 16 that are ANSI COLOR XYZ – you need to have all of those listed. If you don’t see ErrorList and or all its styles:

                            1. Close all instances of Notepad++
                            2. Open one instance, and open the files %AppData%\Notepad++\stylers.xml and C:\Program Files\Notepad++\stylers.model.xml, and put them side-by-side (right click on the stylers.xml tab, Move Document > Move to Other View)
                            3. In stylers.model.xml search for name="errorlist"
                            4. In stylers.xml, search for the same.
                              • If it doesn’t exist, scroll to just before the name="escript", and copy the entire <LexerType name="errorlist" ... </LexerType> section from the model into the stylers.xml
                              • If it does exist, do a side-by-side comparison, and copy over any <WordsStyle ... /> lines from the model’s errorlist section and paste them into the stylers.xml
                                • This screenshot is showing an example where the model has ANSI COLOR BRIGHT CYAN but the stylers.xml does not, so you would copy it over from the model to stylers.xml:
                                  9a6d6a8f-702a-4a36-b5ee-35baa8346901-image.png
                            5. After updating stylers.xml, save the file
                            6. Exit Notepad++
                            7. When you start Notepad++ again, ErrorList should show up in the style configurator, and you should get the colors in errorlist correctly.

                            If you find those 7 steps too difficult, use Plugins Admin to install my ConfigUpdater plugin, then after restarting, run Plugins Admin > ConfigUpdater > Update Config Files

                            6aa89c3f-7b78-41c6-80f3-ce2ecad2c1d5-image.png

                            Anytime you upgrade to a newer version of Notepad++, run the ConfigUpdater, and it will make sure you aren’t missing any languages or styles (without overwriting customized colors).

                            J_SwaJimeJ 1 Reply Last reply Reply Quote 0
                            • J_SwaJimeJ
                              J_SwaJime @PeterJones
                              last edited by

                              @PeterJones

                              Sorry I wasn’t trying to stir up trouble. Either you are correct or I may simply have mangled things by going in the wrong order.

                              Here is a pic showing miswritten regex I found via AI, which was what I was trying to correct. I did not find the ErrorList language feature until after writing my initial post.

                              Screenshot_20250618_203154_RVNC Viewer.jpg

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              The Community of users of the Notepad++ text editor.
                              Powered by NodeBB | Contributors