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

发布于 2024-09-06 20:29:19 字数 548 浏览 4 评论 0原文

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

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

我正在使用的命令:

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

版本:

  • Emacs 23.1
  • Cucumber 0.8.3
  • Cucumber-rails 0.3.2

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

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浊酒尽余欢 2024-09-13 20:29:19

我用它来打开编译缓冲区中的 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 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)
梦归所梦 2024-09-13 20:29:19

我改进了代码,使其不会像命令一样污染 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))

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))
情何以堪。 2024-09-13 20:29:19

截至 2023 年,最现代的方式似乎是 xterm-color Emacs 包。

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

  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)

请参阅 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))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文