Search code examples
formattingcommon-lispstdout

How do I print text in Common Lisp so that I could format it with escape sequences (akin to display in Racket)?


Ho do I print a formatted output in Common Lisp? In Racket I do it with display, like so:

(display "\33[3min italics\33[m\n")

I've tried with (format t "~ain italics~a" "\33[3m" "\33[m") but it does not work. Neither does this: (format t "~cin italics~c" #\33[3m #\33[m).


Solution

  • The main issue here is how to get the proper sequence of characters. \33 is octal for ascii char 27 or #\Esc in Common Lisp.

    (format t "~C[3min italics~C[m~%" #\Esc #\Esc)
    

    would do what you want.

    But you could do better than that. There is a library called cl-interpol which demonstrates flexibility of Common Lisp by modifying the reader so you could use the already familiar syntax. For example:

    * (ql:quickload 'cl-interpol)
    To load "cl-interpol":
      Load 1 ASDF system:
        cl-interpol
    ; Loading "cl-interpol"
    ...
    (CL-INTERPOL)
    
    * (named-readtables:in-readtable :interpol-syntax)
    #<NAMED-READTABLE :INTERPOL-SYNTAX {1002E6D6B3}>
    
    * (format t #?"\33[3min italics\33[m\n")
    in italics
    NIL