开发者

Cucumber's ANSI colors messing up emacs compilation buffer

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 colo开发者_JAVA技巧rs 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?


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)


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))


As of 2022, 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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜