Printing colored text to the terminal with the ANSI Escape Codes

I tried to print colored text to the terminal with the ANSI Escape Codes.
For example, inside a scheme session:

(display "\\033[31mAAA\\033[0m\n")
(display "\\027[31mBBB\\027[0m\n")
(display "\\x1b[31mCCC\\x1b[0m\n")

None of the tests worked.
Nicolas

SOLUTION:

Thanks to @jeroen and @mgubi, the solution is:

(display "\x1B[31mAAA\x1B[0m")

i.e. make the escape code an escape code …

1 Like

What are you trying to achieve? Scheme sessions are not connected to a “useful” terminal. If you want to output something in the output area of the session, you just have to return a value. If you return a TeXmacs tree as value, then TeXmacs insert it in the current document. Try, for example, with

(stree->tree `(equation* (document (sqrt "1+3"))))

Thank you for your answer but I don’t want to write in a document.
I want to output color texts to the terminal, i.e. the console from which I called TeXmacs.
Example with: (display “AAA\n”) in the init file

$ TeXmacs-2.1.4.x86_64.AppImage
AAA
TeXmacs] With linked TrueType support

From TeXmacs, the ANSI escape code to print in color in terminal doesn’t work,
i.e. “\033[31mAAA\033[0m”.
These escape codes work in Linux/Unix and don’t work for Windows OS, as Mac OSX is a variant of Unix BSD, maybe it also works on Mac.

It doesn’t look like you’re printing escape characters. You’ve escaped the slash, so the string contains 4 characters for “\033”, not the ASCII escape character ‘\033’. This works for me in Mogan (S7 Scheme), escaping may be slightly different in TeXmacs (Guile):

(begin (display #\escape) (display "[31m") (display "AAA") (display #\escape) (display "[0m"))
1 Like

I suspect that it is the syntax for the special characters in the string which is wrong. Check e.g. https://www.gnu.org/software/guile/manual/html_node/String-Syntax.html I guess you need to replace \033 by something like \xHH were HH is the hexadecimal representation of the ASCII value you want but in this moment I do not know what \033 means

1 Like
Scheme] (begin (display #\escape) (display "[31m") (display "AAA") (display #\escape) (display "[0m"))
#<unknown port>:1:25: unknown character name escape

And Unicode doesn’t work either:

(display “AAA<#2665>BBB\n”)

Standard escape codes are prefixed with Escape :

  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: \027

I already test all of it

SOLUTION:

It is solved here: Printing Unicode to the terminal

In that case you need to adapt it to TeXmacs Guile scheme. According to @mgubi’s link \x1B should work.

This works for me in TeXmacs:
(display "\x1B[31mAAA\x1B[0m")

This is in Linux.

2 Likes

Yes !!! Thank you all

I tried the Backspace character (ASCII 8): (doesn’t work)

Scheme]  (display "AAA\b\n")
#<unknown port>:1:16: illegal character in escape sequence: #\b
Scheme]  (display "AAA\x08\n")
1 Like

Perhaps because it is not part of Guile 1.8 (http://gnu.ist.utl.pt/software/guile/docs/docs-1.8/guile-ref/String-Syntax.html#String-Syntax), and TeXmacs uses 1.8

1 Like