5

我想控制在 Common Lisp 中显示的文本的颜色。像这样的伪代码:

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

有什么办法可以做到这一点?我使用 SBCL,我的 repl 在 emacs 中。谢谢!

4

3 回答 3

7

您可以使用ANSI 转义码打印彩色文本:

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

不过,我不确定这是否适用于粘液。

于 2013-10-06T16:06:03.060 回答
1

要启用 ANSI 颜色转义序列,请加载http://melpa.org/#/slime-repl-ansi-color包 - 但由于错误,您可能必须M-x slime-repl-ansi-color-mode RET在 REPL 缓冲区中。从各种废弃的 buggy 版本中提取,在https://gitlab.com/augfab/slime-repl-ansi-color找到最好和最新的版本

粘液-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)

在你的.emacs初始化文件中,类似

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

应该启用 slime repl 表达式

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

产生杂色输出。尝试

(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))
于 2020-07-12T17:51:22.637 回答
0
(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")
于 2020-03-02T12:54:04.907 回答