执行命令shell-command时,相关缓冲区中显示的输出未着色。
从 emacs 中调用测试框架(输出黄色/绿色/红色...)时,这尤其令人讨厌。
如何配置或扩展 emacs 以使shell 命令允许在 shell 中进行彩色输出并在表示该输出时保留颜色?
谢谢!
附言。我在 UN*X 系统上使用 Bash shell。
这可能是你想要的:
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
您可以实现自己的 shell 执行,例如
(defun my-shell-execute(cmd)
(interactive "sShell command: ")
(shell (get-buffer-create "my-shell-buf"))
(process-send-string (get-buffer-process "my-shell-buf") (concat cmd "\n")))
这增加了ansi-color-apply-on-region
在 shell 命令完成后在 minibuffer 上运行的建议:
(require 'ansi-color)
(defun ansi-color-apply-on-buffer ()
(ansi-color-apply-on-region (point-min) (point-max)))
(defun ansi-color-apply-on-minibuffer ()
(let ((bufs (remove-if-not
(lambda (x) (string-starts-with (buffer-name x) " *Echo Area"))
(buffer-list))))
(dolist (buf bufs)
(with-current-buffer buf
(ansi-color-apply-on-buffer)))))
(defun ansi-color-apply-on-minibuffer-advice (proc &rest rest)
(ansi-color-apply-on-minibuffer))
(advice-add 'shell-command :after #'ansi-color-apply-on-minibuffer-advice)
;; (advice-remove 'shell-command #'ansi-color-apply-on-minibuffer-advice)
它不依赖于 shell-mode 或 comint。我将其与以下内容一起使用以获得不错的测试输出(一个绿色笑脸,其中包含成功的 doctests 计数。
(defun add-test-function (cmd)
(interactive "sCommand to run: ")
(setq my-testall-test-function cmd)
(defun my-testall ()
(interactive)
(shell-command my-testall-test-function))
(local-set-key [f9] 'my-testall))