S4M
S4M

Reputation: 4661

Change the color of the text in the Common Lisp REPL

I'd like to control the color of the text displayed in Common Lisp. Something like this pseudo-code:

(print-color (:red "hello") (:blue "world"))

Is there any way this can be done? I use SBCL and my repl is inside emacs. Thanks!

Upvotes: 8

Views: 3152

Answers (3)

Devon
Devon

Reputation: 1093

To enable ANSI color escape sequences, load the http://melpa.org/#/slime-repl-ansi-color package — but due to a bug, you may have to M-x slime-repl-ansi-color-mode RET in the REPL buffer. Distilled from various abandoned buggy versions, find the best and latest version at https://gitlab.com/augfab/slime-repl-ansi-color

slime-repl-ansi-color.el

(require 'ansi-color)
(require 'slime)

(define-minor-mode slime-repl-ansi-color-mode
  "Process ANSI colors in Lisp output."
  nil
  :lighter " SlimeANSI")

(define-slime-contrib slime-repl-ansi-color
  "Turn on ANSI colors in REPL output"
  (:authors "Max Mikhanosha")
  (:license "GPL")
  (:slime-dependencies slime-repl)
  (:on-load
   (add-hook 'slime-repl-mode-hook 'slime-repl-ansi-color-mode)))

(defadvice slime-repl-emit (around slime-repl-ansi-colorize activate compile)
  "Process ANSI colors in the Lisp output."
  (with-current-buffer (slime-output-buffer)
    (let ((start slime-output-start))
      (setq ad-return-value ad-do-it)
      (when slime-repl-ansi-color-mode
        (ansi-color-apply-on-region start slime-output-end)))))

(provide 'slime-repl-ansi-color)

In your .emacs init file, something like

(add-to-list 'slime-contribs 'slime-repl-ansi-color)

should enable the slime repl expression

(format t "~c[31mRed~:*~c[32mGreen~:*~c[34mBlue~:*~c[mPlain~%" (code-char 27))

to produce varicolored output. Try

(ql:quickload :cl-ansi-text)

(cl-ansi-text:with-color (:green :style :background)
  (cl-ansi-text:with-color (:yellow :effect :bright)
    (princ " Yellow on Green ")))

(princ (cl-ansi-text:green
        (cl-ansi-text:yellow " Yellow on Green " :effect :bright)
        :style :background))

Upvotes: 1

ulego
ulego

Reputation: 1

(defun color-text (string color); ANSI escape code 
 (let((color
  (cond
    ((string= color "red") "31")
    ((string= color "green") "32")
    ((string= color "yellow") "33")
    ((string= color "white") "37")
    ((string= color "bright blue") "94")
    ((string= color "bright yellow") "93")
    ((string= color "bright cyan") "96")
    ((string= color "bright magneta") "95")
    (t "90")
 )))  
  (format t (concatenate 'string "~c[" color "m" ) #\ESC )
   (eval(read-from-string string))
  (format t (concatenate 'string "~c[" color "m~c[0m"  ) #\ESC #\ESC))
); (color-text "(format t \"~a\" \"ADASDASDASDA dsfsdf\")" "red")

Upvotes: 0

SaltyEgg
SaltyEgg

Reputation: 1558

You can use ANSI escape code to print colorful texts:

(format t "~c[31mabc~c[0m~%" #\ESC #\ESC) ; this prints a red "abc" for most modern terminals

I'm not sure whether this works in slime, although.

Upvotes: 10

Related Questions