Cucumber's ANSI colors messing up emacs compilation buffer
Asked Answered
K

3

46

When working in Emacs, I use the compile command (F12 by default) to run programs. When I run Cucumber in Emacs, Cucumber spits out ANSI colors that the Emacs compilation mode doesn't interpret. The result is ugly and hard to read. Here's a snippet of the *compilation* buffer showing the ugly:

^[[31m(::) failed steps (::)^[[0m

The command I'm using:

( cd ~/lab/rails/todolist && rake cucumber:all )

Versions:

  • Emacs 23.1
  • Cucumber 0.8.3
  • Cucumber-rails 0.3.2

The world would be sunshine and birds singing if I could:

  • Get Emacs to interpret ANSI color codes in its compilation buffer, or
  • Get Cucumber to stop spitting out ANSI color codes

Any ideas?

Kahaleel answered 18/6, 2010 at 19:51 Comment(0)
S
78

I use this to turn on ansi color interpretation in my compilation buffer:

(require 'ansi-color)
(defun colorize-compilation-buffer ()
  (let ((inhibit-read-only t))
    (ansi-color-apply-on-region (point-min) (point-max))))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
Sewan answered 18/6, 2010 at 20:25 Comment(5)
Exactly what I was looking for! Unfortunately, in my environment (cygwin-xemacs) it kroaked on (buffer-read-only #<buffer "compilation">) with "Error in process filter", which seems very strange. Any ideas?Trencherman
This worked great for me, too. I also added (linum-mode 0) (and, at first attempt (line-number-mode 0), which didn't work, but I left in anyway, and might work for someone else), after (toggle-read-only), which makes me even that much happier (a bunch of lines that were just barely wrapping now don't).Identify
I decided to add some colour to my makefiles output and hit this issue in emacs. Great solution!Sirloin
For modern Emacsen, one should let-bind inhibit-read-only to t, instead of calling toggle-read-only.Simplex
Any way to make this work even as new text is being added? I tested that on the result of ack, but the hook seems to be run before the content fills the buffer, so the content is no interpreted.Calve
O
22

I improve code so it doesn't pollute M-x grep like commands and more efficient:

(ignore-errors
  (require 'ansi-color)
  (defun my-colorize-compilation-buffer ()
    (when (eq major-mode 'compilation-mode)
      (ansi-color-apply-on-region compilation-filter-start (point-max))))
  (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
Olga answered 26/12, 2013 at 17:22 Comment(2)
Note I test this code only with Emacs 24.x, it may not work in version 23.x or less...Olga
This works fine, why (add-hook 'compilation-filter-hook 'ansi-color-for-comint-mode-on) does not?Fadeless
E
9

As of 2023, the most modern way appears to be the xterm-color Emacs package.

  1. Execute M-x package-install with xterm-color.

  2. Add the following lines to your ~/.emacs or ~/.emacs.d/init.el:

(require 'xterm-color)
(setq compilation-environment '("TERM=xterm-256color"))
(defun my/advice-compilation-filter (f proc string)
  (funcall f proc (xterm-color-filter string)))
(advice-add 'compilation-filter :around #'my/advice-compilation-filter)

(See xterm-color documentation.)

Note that this will provide an error message if xterm-color was not installed properly. This is strongly advised, because on an incomplete Emacs installation it will clearly explain to you what's wrong, instead of leaving you wondering why the colors don't work.

However, if you really prefer to not being informed if xterm-color is missing, use instead:

(when (require 'ansi-color nil t)
  (setq compilation-environment '("TERM=xterm-256color"))
  (defun my/advice-compilation-filter (f proc string)
    (funcall f proc (xterm-color-filter string)))
  (advice-add 'compilation-filter :around #'my/advice-compilation-filter))
Earthnut answered 2/9, 2020 at 17:14 Comment(6)
This worked well for coloring my pytest compilation commands, but broke my ag-mode - it lost interactivity and slowed down.Ineradicable
@Ineradicable I'd propose to file a bug report: github.com/atomontage/xterm-color/issuesEarthnut
There's one already for rg.el and ag.el packages: github.com/dajva/rg.el/issues/65. Both packages rely on matching terminal color escapes and do not provide option to ignore highlighting. I'd switch off my/advice-compilation-filter for specifically ag-mode, but I'll have to figure it out.Ineradicable
This didn't work for sbt color output in the compilation buffer for me. The escape codes still printed.Dearborn
works great for mypy errorsPirtle
actually there is a weird issue with this -- I have the compilation buffer in one frame and a source buffer in the other frame, and I have a keybinding setup to trigger compilation regardless of which frame/buffer I'm using. If the compilation buffer has focus and I press the key, I get colors. If the source buffer has focus, then no colors.Pirtle

© 2022 - 2024 — McMap. All rights reserved.