Search code examples
unixsmlsmlnjansi-escape

How to colourise the text output in Standard ML?


This is the second week of my compiler course and I got an assignment to make a syntax highlighter. Since I am very new to Standard ML, I tried to print something like: print("\033[1;31m hello world\n");. In C/C++ this type of statement works fine, that is, it produces a colored output but in SML it just prints them. Please help me out.


Solution

  • The problem probably is the \033 escape sequence in octal notation. Octal numbers are a legacy feature of C-like languages that SML does not support. You can use either decimal (\027) or hexadecimal (\u001b) notation instead.

    It's also worth noting that control sequences like the one you intend to use are not a feature of the respective language. Rather, they are interpreted by the terminal the program will be running in and output to. This one in particular is only understood by terminals with ANSI / VT100 emulation.