I want to colorize my string with ASCII colors, but the following code...
data Color = White | Red | Green | Yellow
colorizeWith :: Color -> String -> String
colorizeWith Green s = "\031[0;33m" <> s <> "\033[0m"
colorizeWith Red s = "\032[0;33m" <> s <> "\033[0m"
colorizeWith Yellow s = "\033[0;33m" <> s <> "\033[0m"
colorizeWith White s = "\097[0;33m" <> s <> "\033[0m"
...doesn't work. It shows this build error:
Error found:
at src/Main.purs:33:27 - 33:28 (line 33, column 27 - line 33, column 28)
Unable to parse module:
Illegal character escape code
How can I fix this?
First off, in PureScript string literals escaped character codes start with \x
and are required to be hexadecimal. So, for example:
"\x31\x32\x33" == "123"
But beyond that, you seem to have your escape sequences all mixed up.
First, the escape symbol, decimal 27
, hexadecimal 1B
, octal 33
, which is apparently what you're trying to do, but you put it in the wrong place - as color parameter instead of the escape sequence opening. You seem to have swapped the color codes with the opening escape symbols.
Second, you seem to have swapped red and green. Red is 31, green is 32.
So, taking into account all of the above, this is how your code should look:
colorizeWith :: Color -> String -> String
colorizeWith Green s = "\x1B[0;32m" <> s <> "\x1B[0m"
colorizeWith Red s = "\x1B[0;31m" <> s <> "\x1B[0m"
colorizeWith Yellow s = "\x1B[0;33m" <> s <> "\x1B[0m"
colorizeWith White s = "\x1B[0;97m" <> s <> "\x1B[0m"