Page
Library
Module
Module type
Parameter
Class
Class type
Source
Spectrum Capabilities detects the color support level of a terminal by examining environment variables, the TERM setting, CI providers, and OS version information. The heuristics are adapted from the JavaScript supports-color library (used by Chalk).
This library has no dependency on the rest of the Spectrum family and can be used standalone.
Detection returns one of four levels:
type color_level =
| Unsupported (* No color support *)
| Basic (* 16 colors: ANSI codes 30-37, 90-97 *)
| Eight_bit (* 256 colors: xterm palette *)
| True_color (* 24-bit RGB *)The typical entry point queries both stdout and stderr at once:
let info = Spectrum_capabilities.Capabilities.supported_color_levels () in
match info.stdout with
| True_color -> print_endline "24-bit color supported"
| Eight_bit -> print_endline "256 colors supported"
| Basic -> print_endline "16 colors supported"
| Unsupported -> print_endline "No color support"For a specific file descriptor, pass the result of Unix.isatty directly:
let level =
Spectrum_capabilities.Capabilities.supported_color_level
(Unix.isatty Unix.stdout)The detection logic checks the following, in priority order:
FORCE_COLOR: returns UnsupportedTERM=dumb: returns Unsupported (unless FORCE_COLOR sets a floor)True_color, >= 10586 for Eight_bit, otherwise Basic)BasicTF_BUILD + AGENT_NAME): returns BasicCOLORTERM=truecolor: returns True_colorTERM_PROGRAM: iTerm.app >= 3.0 returns True_color, Apple_Terminal returns Eight_bitTERM suffix -256color or -256: returns Eight_bitTERM prefix matching known terminals (xterm, screen, vt100, rxvt, ansi, linux, etc.): returns BasicCOLORTERM value: returns BasicUnsupportedThe FORCE_COLOR environment variable acts as a floor for detection:
FORCE_COLOR=0 Unsupported FORCE_COLOR=1 Basic (16 colors) FORCE_COLOR=2 Eight_bit (256 colors) FORCE_COLOR=3 True_color (24-bit) FORCE_COLOR=true Basic FORCE_COLOR=false Unsupported
The Spectrum_capabilities.Capabilities.Make functor accepts custom environment and OS information providers, allowing deterministic testing without real terminal state:
open Spectrum_capabilities.Capabilities
let env_map = StrMap.(
empty
|> add "COLORTERM" "truecolor"
) in
let env = env_provider_of_map env_map in
let os = os_info_provider false None in
let module Caps = Make((val env))((val os)) in
let level = Caps.supported_color_level true in
assert (equal_color_level level True_color)Spectrum_capabilities.Capabilities Terminal color capability detection.