简体   繁体   English

Cucumber 的 ANSI 颜色弄乱了 emacs 编译缓冲区

[英]Cucumber's ANSI colors messing up emacs compilation buffer

When working in Emacs, I use the compile command (F12 by default) to run programs.在 Emacs 中工作时,我使用编译命令(默认为 F12)来运行程序。 When I run Cucumber in Emacs, Cucumber spits out ANSI colors that the Emacs compilation mode doesn't interpret.当我在 Emacs 中运行 Cucumber 时,Cucumber 会吐出 Emacs 编译模式无法解释的 ANSI 颜色。 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 Emacs 23.1
  • Cucumber 0.8.3黄瓜 0.8.3
  • Cucumber-rails 0.3.2黄瓜导轨 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让 Emacs 解释其编译缓冲区中的 ANSI 颜色代码,或
  • Get Cucumber to stop spitting out ANSI color codes让 Cucumber 停止吐出 ANSI 颜色代码

Any ideas?有任何想法吗?

I use this to turn on ansi color interpretation in my compilation buffer:我用它在我的编译缓冲区中打开 ansi 颜色解释:

(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 Mx grep like commands and more efficient:我改进了代码,因此它不会像命令一样污染Mx grep并且更高效:

(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 2021, the most modern way appears to be the xterm-color Emacs package.到 2021 年,最现代的方式似乎是xterm-color Emacs 包。

  1. Execute Mx package-install with xterm-color .使用xterm-color执行Mx package-install

  2. Add the following lines to your ~/.emacs or ~/.emacs.d/init.el : ~/.emacs.d/init.el添加到您的~/.emacs~/.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 .) (请参阅xterm-color 文档。)

Note that this will provide an error message if xterm-color was not installed properly.请注意,如果xterm-color未正确安装,这将提供错误消息。 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.强烈建议这样做,因为在不完整的 Emacs 安装中,它会清楚地向您解释什么是错误的,而不是让您想知道为什么颜色不起作用。

However, if you really prefer to not being informed if xterm-color is missing, use instead:但是,如果您真的不想在缺少xterm-colorxterm-color通知,请改用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM