在Lisp / Emacs中打印彩色字符

时间:2010-03-10 17:50:05

标签: emacs lisp

我正在用Lisp编写一个简单的connect-4程序,理想情况下,每个玩家(红色,黑色)在显示游戏状态时都会有自己的颜色。有谁知道如何打印彩色ASCII字符?这一般是怎么做的?我正在使用emacs 23,因此解决方案可能特定于emacs本身。

无论如何,我已经检查过超级规格,看看FORMAT能否做到但到目前为止还没有运气。提前谢谢。

4 个答案:

答案 0 :(得分:6)

Emacs中文本的外观由 faces 控制。可以通过 overlay 文本属性更改Face。以下是使用后者的示例:

;; Emacs-Lisp
(insert (propertize "foo" 'font-lock-face '(:foreground "red")))

但是,如果游戏是在SBCL中实现的,那么您需要一种与SBCL程序中的Emacs通信的方法。因为看起来你正在使用Slime,使用Swank,这是Slime的一部分,可能是最方便的:

;; Common-Lisp
(swank::eval-in-emacs
 '(with-current-buffer (slime-repl-buffer)
    (insert (propertize "foo" 'font-lock-face '(:foreground "red")))))

答案 1 :(得分:2)

无耻的自我插件:您可能想尝试this,这是在Web浏览器中运行的Common Lisp的图形终端。它使用html进行打印,因此您可以执行以下操作:

(gtfl-out (:p :style "color:red;" "some characters"))

答案 2 :(得分:1)

我刚刚写了一个Elisp defun来测试颜色。例如,在设置Linux VT的常规颜色时,它可能很有用。请注意,颜色列表(“黑色”,“红色”等)仅是常规和通常定义的方式 - 如果将它们设置为其他颜色,则红色将不再是红色。不过,测试它会很有用。

;;;; insert colored and/or bright text
(defun insert-colored-text (str clr bright)
  "Inserts str at point, in color clr, bright or not."
  (interactive (list (read-string " String: ")
                     (read-string " Color: ")
                     (y-or-n-p    " Bright? ") ))
  (insert (propertize str 'font-lock-face
          `(:weight ,(if bright 'bold 'normal) :foreground ,clr) )))
(defalias 'ict 'insert-colored-text)

(defun test-all-faces ()
  "Prints a test string in al colors, both normal and bright."
  (interactive)
  (let ((str "This is what it looks like"))
    (dolist (bold '(nil t) nil)
      (dolist (color
               '("black" "red" "green" "yellow" "blue"
                 "magenta" "cyan" "white") nil)
        (insert-colored-text
         (format "%s in %s (that is %sbold)\n" str color
                 (if bold "" "not ")) color bold) ))))
(defalias 'taf 'test-all-faces)

example output http://user.it.uu.se/~embe8573/cols.png

答案 3 :(得分:1)

我正在寻找相同的答案,并且在这里,过了一段时间,从另一个链接[Change the color of the text in the Common Lisp REPL找到答案。

我想也许你需要它,答案是:

  

块引用

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

(格式t" ~c [31mabc~c [0m~%"#\ ESC#\ ESC];这会打印出一个红色的" abc"对于大多数现代终端 我不确定这是否适用于粘液,但是。

分享改进这个答案 在10月6日和13日在16:06回答

SaltyEgg 626412

  

块引用

相关问题