After releasing ColoredPrintf I started investigating color terminals, especially how some terminals can display more than the traditional 16 colors.
While on windows console colors are changed using a call to the
SetConsoleTextAttribute API, on unix XTerm and other terminals follow the way the VT100
physical terminal did it with special escape sequences that are sent to the terminal but change it’s
behavior instead of being displayed.
It is especially interesting as such terminals are becomming more present, even in the windows world. ConEmu support a lot even if it’s buggy, the venerable windows terminal can be told to interpret them and it’ll even soon gain True Color support too.
If you’re on windows, expect bugs everywhere but if you have a mac or a linux OS you’re good. In more details :
- On windows ConEmu is still the best you can do, it’s buggy but usable. There is a little trick to
enable more than 16 colors but it’s easy and need to be done only once per session, I’ll explain how to do it
from code in Appendix A but you can simply display one of the ConEmu sample file with
cmd /c type "%ConEmuBaseDir%\Addons\AnsiColors256.ans"and it’ll activate it.
- On macOS (Tested on sierra) the integrated terminal can display 256 colors without problems but will translate any True Color command to 16 colors, giving a very ugly result. But iTerm2 is free works !
- On linux the Gnome default terminal, XTerm and Konsole works flawlessly (XTerm adapt true color codes to 256 colors but it’s algorithm is pretty good).
- Visual Studio Code integrated terminal can do most of them and as the REPL provided by the Ionide plugin uses it we can use the F# REPL directly there. But beware the windows version of the VSCode terminal is pretty buggy at the moment and often completely break when control codes are sent (Well, resizing it is enough to break it, you don’t need much 😁)
Using F# to play with escape sequences
Escape sequences are generated by printing the
0x1B) character followed by at least another character.
Let’s try that in F# :
let esc = string (char 0x1B) System.Console.WriteLine(esc + "[31;1m" + "Hello world" + esc + "[0m")
Hello World in bright red as
m is the graphic command taking sub-commands separated by
31 is the red color and
1 signal that we want bold/bright text. And at the end
0 is the reset sub-command,
without it the terminal would stay red on all following lines.
kprintf we can create a printf-like function and start implementing a few of the escape sequences :
let csi = esc + "[" let printsequencef f = Printf.kprintf (fun s -> System.Console.Write(esc + s)) f let printcsif f = Printf.kprintf (fun s -> System.Console.Write(csi + s)) f let selectGraphicRendition (gr: int list) = printcsif "%sm" (System.String.Join(";", gr |> Seq.map string)) let resetColor() = selectGraphicRendition  let setForeground i = selectGraphicRendition([30 + i]) let setBackground i = selectGraphicRendition([40 + i])
256 colors is enough for everyone
Until then we’ve done nothing very interesting, after all the .Net console already support changing the console
color to bright red, nothing new under the sun. But what the .Net console or Windows native API doesn’t support
48 graphics sub-commands, extended foreground an background colors.
let setExtendedForeground i = selectGraphicRendition [38; 5; i] let setExtendedBackground i = selectGraphicRendition [48; 5; i] let setForegroundRgb r g b = selectGraphicRendition [38; 2; r; g; b] let setBackgroundRgb r g b = selectGraphicRendition [48; 2; r; g; b]
The first variant accept an index in a 256 colors table and the second specify the R, G and B components.
While all the terminal emulators I tested support both of them the colors the display will vary, especially for the R;G;B True Color mode. Some like macOS default terminal are pretty weird in the sense that it support 256 color mode but interpolate any R;G;B code to 16 colors.
Let’s see what the palete looks like :
let extendedBlock i = for j in i..i+5 do setExtendedBackground j printf " " for row in 0..5 do for b in 0..5 do extendedBlock (16 + 36*b + row*6) resetColor() printf " " printfn ""
The result in VS Code (On macOS) :
We need to be true to our colors
Now that we know how to draw with true colors we can draw pictures on the terminal :
open System.Drawing let showBitmap path = let bitmap = Image.FromFile(path) :?> Bitmap for y in 0..bitmap.Size.Height-1 do for x in 0..bitmap.Size.Width-1 do let px = bitmap.GetPixel(x, y) setBackgroundRgb (int px.R) (int px.G) (int px.B) printf " " resetColor() printfn ""
And we can apply it to a 40x40px version of the FSharp logo :
The code for all of that can be found in a Gist.
I didn’t test the windows insider release so can’t tell how well microsoft True Color support in the native console works but it’s the next thing I want to try.
ConEmu support more than the standard 16 colors but if you try it, it won’t work directly, it seem that maybe for compatibility reasons the support isn’t always on.
To trigger it an application must first scroll the terminal far away at least once with the
T command :
let scrollUp (lines: int) = printcsif "%iS" lines let scrollDown (lines: int) = printcsif "%iT" lines scrollDown 9999 setCursorPos 9999 1
After that ConEnu will display what we expect :
While the windows terminal support escape codes, it doesn’t do it directly. They need
to be enabled using
SetConsoleMode API :
#nowarn "9" module DllImports = open System.Runtime.InteropServices open Microsoft.FSharp.NativeInterop let INVALID_HANDLE_VALUE = nativeint -1 let STD_OUTPUT_HANDLE = -11 let ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 [<DllImport("Kernel32")>] extern void* GetStdHandle(int nStdHandle) [<DllImport("Kernel32")>] extern bool GetConsoleMode(void* hConsoleHandle, int* lpMode) [<DllImport("Kernel32")>] extern bool SetConsoleMode(void* hConsoleHandle, int lpMode) let enableVTMode() = let handle = GetStdHandle(STD_OUTPUT_HANDLE) if handle <> INVALID_HANDLE_VALUE then let mode = NativePtr.stackalloc<int> 1 if GetConsoleMode(handle, mode) then let value = NativePtr.read mode let value = value ||| ENABLE_VIRTUAL_TERMINAL_PROCESSING SetConsoleMode(handle, value) else printfn "no get" false else printfn "no handle" false DllImports.enableVTMode() |> ignore