如何在(emacs)shell-command的输出中有颜色?

时间:2011-01-18 16:07:53

标签: emacs xemacs

执行命令 shell-command 时,关联缓冲区中显示的输出不会着色。

当从emacs中调用测试框架(输出黄色/绿色/红色......)时,这尤其令人讨厌。

如何配置或扩展emacs以使 shell-command 允许在shell中使用彩色输出并在表示输出时保留颜色?

谢谢!

PS。我在UN * X系统上使用Bash shell。

3 个答案:

答案 0 :(得分:3)

这可能是你想要的:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

答案 1 :(得分:2)

你可以实现自己的shell-execute,比如

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

答案 2 :(得分:0)

这增加了一个建议,在shell命令完成后在迷你缓冲区上运行ansi-color-apply-on-region

(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模式或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))