How to have colors in the output of (emacs) shell-command?
Asked Answered
D

4

4

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.

Dredger answered 18/1, 2011 at 16:7 Comment(0)
A
3

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")))
Atkins answered 18/1, 2011 at 22:1 Comment(2)
nice hack, i did implement it with a little tweak: (concat "reset\n" cmd "\nexit 0 &> /dev/null\n")Dredger
Very cool, and @Dredger hack prevents triggering the shell init prompt at each subsequent invocation. My Emacs looks like an IDE now :)Yingyingkow
E
3

This is probably what you want :

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
Escutcheon answered 18/1, 2011 at 21:45 Comment(2)
had tried that hook before, in combination with (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t), but it seems to only affect the shell-mode and not a simple shell-command.Dredger
@mgodinho: Well, the accepted answer is not doing a shell-command is opening a shell-mode (with a renamed buffer) and without this add-hook the accepted answer is not producing any color. at least for me.Canoewood
A
3

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")))
Atkins answered 18/1, 2011 at 22:1 Comment(2)
nice hack, i did implement it with a little tweak: (concat "reset\n" cmd "\nexit 0 &> /dev/null\n")Dredger
Very cool, and @Dredger hack prevents triggering the shell init prompt at each subsequent invocation. My Emacs looks like an IDE now :)Yingyingkow
P
1

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))
Pokpoke answered 8/3, 2017 at 8:6 Comment(0)
T
0

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)

Theodora answered 5/5, 2022 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.