This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

terminal color output

11 posts · 10890 views

Hi,

is there a simple way of getting a colored output with sbcl? I need the same output as from the
echo -e "\033[34mBlah"
bash command. Any ideas ;)?

Thanks
--
JB
--
JB

Re: terminal color output

I'm not sure why #\Esc or the like don't work, but the following does.
(format t "~C[34mBlah" (code-char 27))

Re: terminal color output

nuntius wrote:I'm not sure why #\Esc or the like don't work, but the following does.
(format t "~C[34mBlah" (code-char 27))
You mean like this:
(format t "~C[34mBlah" #\Esc)
? This works fine here.

Re: terminal color output

What I couldn't get was something like "#\Esc[34mBlah". The code-char was simply cruft from experimentation.

Re: terminal color output

nuntius wrote:What I couldn't get was something like "#\Esc[34mBlah". The code-char was simply cruft from experimentation.
You want cl-interpol for that sort of thing.

Re: terminal color output

Thank you all for your answers, it works!
--
JB

Re: terminal color output

nuntius wrote:What I couldn't get was something like "#\Esc[34mBlah". The code-char was simply cruft from experimentation.
Well, that wouldn't work. the '#\' is a reader macro as much as, for instance, '#()', and you can't place a vector inside a string as well.

Just note that (code-char 27) will not necessarily return #\Esc according to the ANSI spec. All implementation that I know of use either ASCII or Unicode, which means that it will work in all of them, but this is not required - as much as the ANSI spec concerns, you can create an implementation which uses characters with your own encoding.

Re: terminal color output

gugamilare wrote:Just note that (code-char 27) will not necessarily return #\Esc according to the ANSI spec. All implementation that I know of use either ASCII or Unicode, which means that it will work in all of them, but this is not required - as much as the ANSI spec concerns, you can create an implementation which uses characters with your own encoding.
Please... If (char-code #\Esc) is not 27, there's little chance of the terminal understanding either... A real programmer would write a raw byte array anyway. ;)

Re: terminal color output

nuntius wrote:Please... If (char-code #\Esc) is not 27, there's little chance of the terminal understanding either... A real programmer would write a raw byte array anyway. ;)
Theoretically, an implementation can create some kind of internal codification and use ASCII or Unicode to communicate with the terminal (the same way that an implementation uses Unicode internally and is able to write ISO-8859-1 files). But, you are right, it would be a waste of time to create such an implementation :)

Re: terminal color output

Jose Brava wrote:Hi,

is there a simple way of getting a colored output with sbcl? I need the same output as from the
echo -e "\033[34mBlah"
bash command. Any ideas ;)?

Thanks
--
JB
http://users.actrix.co.nz/mycroft/terminfo.lisp

(ti:set-terminal)
(ti:tputs ti:set-a-foreground 4)

Re: terminal color output

Paul wrote:
Jose Brava wrote:Hi,

is there a simple way of getting a colored output with sbcl? I need the same output as from the
echo -e "\033[34mBlah"
bash command. Any ideas ;)?

Thanks
--
JB
http://users.actrix.co.nz/mycroft/terminfo.lisp

(ti:set-terminal)
(ti:tputs ti:set-a-foreground 4)
Yes, highly recommended.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/