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

Way*_*rad 38 emacs cucumber

在Emacs中工作时,我使用编译命令(默认为F12)来运行程序.当我在Emacs中运行Cucumber时,Cucumber会吐出Emacs编译模式无法解释的ANSI颜色.结果很丑陋,难以阅读.这是*compilation*缓冲区的片段,显示丑陋:

^[[31m(::) failed steps (::)^[[0m
Run Code Online (Sandbox Code Playgroud)

我正在使用的命令:

( cd ~/lab/rails/todolist && rake cucumber:all )
Run Code Online (Sandbox Code Playgroud)

版本:

  • Emacs 23.1
  • 黄瓜0.8.3
  • 黄瓜 - 铁路0.3.2

如果可以的话,世界将是阳光和鸟儿歌唱:

  • 让Emacs在其编译缓冲区中解释ANSI颜色代码,或
  • 让Cucumber停止吐出ANSI颜色代码

有任何想法吗?

ata*_*lor 69

我用它在我的编译缓冲区中打开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)
Run Code Online (Sandbox Code Playgroud)

  • 对于现代的Emacsen,应该将`inhibit-read-only`绑定到`t`,而不是调用`toggle-read-only`. (10认同)

gav*_*koa 21

我改进了代码,因此它不会M-x 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))
Run Code Online (Sandbox Code Playgroud)


vog*_*vog 6

到 2021 年,最现代的方式似乎是xterm-colorEmacs 包。

  1. 执行M-x package-installxterm-color

  2. 将以下行添加到您的~/.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)
Run Code Online (Sandbox Code Playgroud)

(请参阅xterm-color 文档。)

请注意,如果xterm-color安装不正确,这将提供错误消息。强烈建议这样做,因为在不完整的 Emacs 安装中,它会清楚地向您解释什么是错误的,而不是让您想知道为什么颜色不起作用。

但是,如果你真的喜欢不被告知,如果xterm-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))
Run Code Online (Sandbox Code Playgroud)