0

Ideally, this line of PolyML code should give desired result:

print "\033[31m RED \033[0m NORMAL \n";

But the \033 turns out to be just an exclamation mark, not a special symbol for color encoding. I use the following "way-around" approach, but it doesn't allow to do anything interactively: I just take the output of my program and color it.

echo "\\\\033[31m RED \\\\033[0m NORMAL \\\\n" | xargs echo -e

What are the possible solutions to this problem? Is it possible to solve it within the standard PolyML instruments?

added: I checked how Ocaml do the same thing with

Printf.printf "\033[31m RED \033[0m NORMAL \n";;

-- situation is the same: no color obtained.

p.s. this question is not a dublicate because it is about differences between echo -e and print in ML languages

4

2 回答 2

2

\033 is a character escape sequence which in bash and many other languages is interpreted as the character with ASCII code corresponding to the octal number 33.

In OCaml however, this escape sequence is interpreted as decimal. We can either convert the number from octal (33) to decimal (27) and continue to use this syntax, or use the correct syntax for octal escape sequences (\o027). Or we could even use hex (\x1b) if we want to be a bit more adventurous.

All of these will work in OCaml, and possibly also PolyML (if you replace Printf.printf with print of course):

Printf.printf "\027[31m RED \027[0m NORMAL \n";;
Printf.printf "\o033[31m RED \o033[0m NORMAL \n";;
Printf.printf "\x1b[31m RED \x1b[0m NORMAL \n";;

Source: The OCaml manual Chapter 9.1: Lexical conventions

于 2022-01-02T12:29:20.340 回答
1

Thanks to this question: OCaml color console output

The correct answer is to use code \027 instead of \033 both in Ocaml and PolyML:

print "\027[31m RED \027[0m NORMAL \n";
于 2022-01-02T11:56:11.950 回答