Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add syntax highlighting to a make error message?

Summary

I want to manually add syntax highlighting of specific words in error messages. I tried with a pipeline to awk, but this doesn't seem to work with the entire message that is thrown by make. Can anybody help me to get the following pipeline working for make's entire error message?

make | awk '{for(i=1;i<=NF;i++){ if($i~/error/ || $i~/Built/) $i=sprintf("\033[0;36m %s \033[0;00m",$i)}; print}'

Or maybe—better still—a different approach to my

Situation (in detail)

To build my latex project, I use Cmake together with UseLATEX (github page). That way, I can simply render my document by executing make from inside a build folder. Only problem: There is no syntax highlighting for the wall of text of an error message. I need to add a minimal version of this, to work productively. (Maybe words could be highlighted globally in my terminal?)

By passing the output in a pipeline to awk (manpage)—inspired by this askubuntu post—I was able to highlight make's success message, pdflatex myfile.tex's success and error message, but not make's error message, it did not highlight the interesting parts of the long message.

By highlighting Built in the code snippet above, one can see that it does highlight when the first command make succeeds.

The problem is that I do not fully understand when the pipeline passes what. It seems that some output can get highlighted and some cannot.

Thanks for any help!

Steps needed to reproduce

To get into exactly the situation at hand, you need a project folder containing

build/
cmake/
myfile.tex
CMakeLists.txt

with cmake/ containing UseLATEX.cmake and CMakeLists.txt reading

include(cmake/UseLATEX.cmake)

ADD_LATEX_DOCUMENT(myfile.tex)

and myfile.tex being a valid or non-valid latex document, e.g.

\documentclass{article}
  \begin{document}
    Some \undefined command
  \end{document}

From inside the build/ folder, call cmake .., and then make, or rather the full command above.

like image 898
gitchhiker Avatar asked Dec 21 '25 07:12

gitchhiker


1 Answers

The message is written on stderr, not on stdout. You have to handle stderr. In bash, you can:

make 2> >(do_something_with_stderr >&2) | do_something_with_stdout

Because | changes buffering, it's not always wanted in interactive programs. For interactive I prefer:

make 2> >(do_something_with_stderr >&2) >(do_something_with_stdout)

and sometimes that's one function:

some_func() { do_something; }
make 2> >(some_func >&2) >(some_func)
like image 171
KamilCuk Avatar answered Dec 22 '25 23:12

KamilCuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!