Reputation: 273
I'm writing a simple connect-4 program in Lisp and ideally each player (red, black) would have their own color when the game state is displayed. Does anyone know how to print colored ASCII characters? How is this done in general? I'm using emacs 23, so the solution might be specific to emacs itself.
Anyways, I've checked the hyperspec to see if FORMAT can do it but no luck so far. Thanks in advance.
Upvotes: 6
Views: 4343
Reputation: 21
I am looking for the same answer and been here, and after a while found the answer from another link[Change the color of the text in the Common Lisp REPL.
I think maybe you need it,the answer is:
Blockquote
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.
shareimprove this answer answered Oct 6 '13 at 16:06
SaltyEgg 626412
Blockquote
Upvotes: 2
Reputation: 499
I just wrote an Elisp defun to test the colors. It could be useful for example when setting up the general colors of the Linux VTs. Note that the color list ("black", "red", etc.) is only how it is conventionally and usually defined - if you set them up as other colors, perhaps red won't be red anymore. Still, it can be useful to test.
;;;; 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
Upvotes: 2
Reputation: 26539
The appearance of text in Emacs is controlled by faces. Face can be changed through either overlay or text properties. Here is an example using the latter:
;; Emacs-Lisp (insert (propertize "foo" 'font-lock-face '(:foreground "red")))
However, if the game is implemented in SBCL, you'll need a way to communicate with Emacs from your SBCL program. As it seems that you're using Slime, using Swank, which is a part of Slime, might be the most convenient:
;; Common-Lisp (swank::eval-in-emacs '(with-current-buffer (slime-repl-buffer) (insert (propertize "foo" 'font-lock-face '(:foreground "red")))))
Upvotes: 6
Reputation: 642
Shameless self plug: you might want to try this, which is a graphical terminal for Common Lisp running in a web browser. It uses html for printing stuff, so you could do something like:
(gtfl-out (:p :style "color:red;" "some characters"))
Upvotes: 4