Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI coloring in emacs start-process output buffer

Tags:

emacs

I am running a process from within emacs which uses ANSI color codes in its output.

I'm using start-process to create the process, and provided a custom buffer name for the process's output

(start-process "foo" "*Foo*" foo-command foo-args)

If I open *Foo* buffer, the ANSI color codes are printed in their raw format to the buffer

^[[36msome output message^[[m
^[[1;35msome output message^[[m
^[[1;34msome output message^[[m

I see in this SO answer it is possible to add colorization to a buffer; however I'm unsure as to how to do it with a buffer created using start-process

Is it possible to convert those ANSI color codes to colorised output in my *Foo* buffer?

like image 360
Steve Lorimer Avatar asked Jun 03 '17 20:06

Steve Lorimer


2 Answers

It doesn't look like the linked answers had any solutions not involving comint-mode. You can use ansi-color-apply-on-region to colorize the output buffer, eg.

(set-process-sentinel
 (start-process "foo" "*Foo*" foo-command foo-args)
 (lambda (p _m)
   (when (eq 0 (process-exit-status p))
     (with-current-buffer (process-buffer p)
       (ansi-color-apply-on-region (point-min) (point-max))))))
like image 124
Rorschach Avatar answered Nov 15 '22 08:11

Rorschach


Here's a solution that will display the colors while the process is executing (rather than after the process is complete and calling ansi-color-apply-on-region)

The key is the last line: set-process-filter.

(defun runserver ()
  (interactive)
  (setq *server-buffer* (get-buffer-create "server"))
  (with-current-buffer *server-buffer*
    (ansi-color-for-comint-mode-on)
    (comint-mode))
  (let ((default-directory "~/server/"))
    (setq *server-process*
          (start-process-shell-command
           "server" *server-buffer* "./run_server")))
  (set-process-filter *server-process* 'comint-output-filter))

A process filter function is a function that receives the standard output from the associated process. All output from that process is passed to the filter. The default filter simply outputs directly to the process buffer.

like image 28
Eric Ihli Avatar answered Nov 15 '22 10:11

Eric Ihli