Sublime Forum

Build results: file_regex partial fail with ANSI characters

#1

I have a custom makefile that outputs ANSI color characters on some lines that I execute with a custom build system cloned from the Make build system. The basic file_regex option in the build system is:
"file_regex": "^(..[^:\n]*):([0-9]+):?([0-9]+)?:? (.*)$",

Here is a sample message that I would be matching, which is a basic GCC warning with ANSI color characters at the start and end:
e[33m./common/src/XXX.c:528:6: warning: 'foobar' defined but not used [-Wunused-function]e[0m

Browsing the build results produce the correct behaviour for the filename, line and column (I am able to browse to the precise location of the message). However, the message itself is not displayed in the text editor. Removing the starting ANSI character results in the message being displayed correctly. I have tried modifying file_regex as follows to remove the starting characters, but this only truncates my file name:
"file_regex": "^.....(..[^:\n]*):([0-9]+):?([0-9]+)?:? (.*)$",

In addition, another problem arises when I try to strip the ending ANSI character (e[0m). The following regex will produce the correct behaviour, but will truncate messages that don’t end with that color character:
"file_regex": "^(..[^:\n]*):([0-9]+):?([0-9]+)?:? (.*)....$",

However, using more elaborated regular expressions such as the following:
"file_regex": "^(..[^:\n]*):([0-9]+):?([0-9]+)?:? \\e\\[.*m(.*)\\e\\[.*m$",

Or even just:
"file_regex": "^(..[^:\n]*):([0-9]+):?([0-9]+)?:? (.*)...\\w$

Produces a correctly stripped error message in the editor, but breaks all browsing of the matched build results.

Any help would be appreciated :slight_smile:

0 Likes

#2

You could try to use the ANSIEscape package.

0 Likes

#3

I already am using this package so that colors are visible in the build result, using the “target” and “syntax” options as described on the plugin page. I should have perhaps mentioned this, but I thought it was unrelated since the plugin seemed to only act aesthetically.

I might be wrong, but playing with it I have a feeling that file_regex is run on the output parsed by ANSIEscape to determine file/line/column, but is run on the unadulterated output when looking for the message causing an inconsistency where depending on how the regexp is written, Sublime can only get one or the other.

0 Likes

#4

I think the most aesthetic approach would be to supply functionality (a command line argument) to remove the ansi colors from the output of your forked make executable.

0 Likes

#5

By “build system”, I meant a Sublime Text “custom_make.sublime-build” that I derived from “Make.sublime-build”. I am using a standard make executable.

Also there’s a misunderstanding, the whole point of this endeavour is to have colors in my make output (and, if I could, the error bubbles in the editor at the same time). I add the ANSI colors myself in the make process through sed commands. I could remove then, but then I’d just be back where I started :slight_smile:

0 Likes

#6

Aha! I understand your problem now :slight_smile:

The canonical way to add colors to your output is by doing something like this:

{
  "build_systems":
  [
    {
      "name": "my_build",
      "shell_cmd": "make blah",
      "file_regex": "...",
      "syntax": "Packages/User/MyMakeOutputSyntax.sublime-syntax" // <---
    }
  ]
}

Forget about sed coloring your output, and write a sublime syntax file to color your output.

0 Likes

#7

While this doesn’t solve the current technical problem, that’s indeed a much better solution for my original need. It seems so obvious now that I’ve delved a little deeper in how Sublime works, I wish that had popped up in my original Google search.

Thanks for the help!

1 Like