如何在 (emacs) shell 命令的输出中添加颜色?

发布于 2024-10-12 14:32:01 字数 216 浏览 2 评论 0原文

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

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

如何配置或扩展 emacs,以便让 shell 命令允许在 shell 中进行彩色输出并在表示该输出时保留颜色?

谢谢!

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

When executing the command shell-command, the output shown in the associated buffer is not colorized.

This is particularly annoying when calling a testing framework (outputting yellow/green/red...) from within emacs.

How can I configure, or extend, emacs in order to have shell-command allowing colorized output in the shell and preserving the colors while representing that output?

Thanks!

ps. I'm using the Bash shell, on a UN*X system.

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

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

发布评论

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

评论(4

帥小哥 2024-10-19 14:32:01

你可以实现你自己的 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")))

You can implement your own shell-execute, something like

(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")))
简单气质女生网名 2024-10-19 14:32:01

这可能就是您想要的:

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

This is probably what you want :

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
鱼忆七猫命九 2024-10-19 14:32:01

这会添加一条建议,以便在 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。我将其与类似以下内容一起使用以获得良好的测试输出(带有成功文档测试计数的绿色笑脸。

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

This adds an advice to run ansi-color-apply-on-region on the minibuffer after shell-command finishes:

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

It does not rely on shell-mode or comint. I accompany it with something like the following to get nice test output (a green smiley with the count of successful 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))
我家小可爱 2024-10-19 14:32:01

此解决方案的灵感来自 @ArneBabenhauserheide,但使用 xterm-color 而不是 ansi-color。它还为 *Shell Command Output* 缓冲区以及迷你缓冲区着色。

(defun xterm-color-colorize-shell-command-output ()
  "Colorize `shell-command' output."
  (let ((bufs
         (seq-remove
          (lambda (x)
            (not (or (string-prefix-p " *Echo Area" (buffer-name x))
                     (string-prefix-p "*Shell Command" (buffer-name x)))))
          (buffer-list))))
    (dolist (buf bufs)
      (with-current-buffer buf
        (xterm-color-colorize-buffer)))))

(defun xterm-color-colorize-shell-command-output-advice (proc &rest rest)
  (xterm-color-colorize-shell-command-output))

(advice-add 'shell-command :after #'xterm-color-colorize-shell-command-output-advice)
;; (advice-remove 'shell-command #'xterm-color-colorize-shell-command-output-advice)

This solution is inspired by @ArneBabenhauserheide's but uses xterm-color instead of ansi-color. It also colorizes the *Shell Command Output* buffer as well as the mini

(defun xterm-color-colorize-shell-command-output ()
  "Colorize `shell-command' output."
  (let ((bufs
         (seq-remove
          (lambda (x)
            (not (or (string-prefix-p " *Echo Area" (buffer-name x))
                     (string-prefix-p "*Shell Command" (buffer-name x)))))
          (buffer-list))))
    (dolist (buf bufs)
      (with-current-buffer buf
        (xterm-color-colorize-buffer)))))

(defun xterm-color-colorize-shell-command-output-advice (proc &rest rest)
  (xterm-color-colorize-shell-command-output))

(advice-add 'shell-command :after #'xterm-color-colorize-shell-command-output-advice)
;; (advice-remove 'shell-command #'xterm-color-colorize-shell-command-output-advice)

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