ctl-term
Terminal output for command-line programs.
ctl-term decides, once per output stream, whether to write colour, at what colour depth, and how wide the output is.
It then writes styled lines as the bytes a terminal reads.
The crate works on the plain lines ctl-text produces, so layout never sees an escape byte.
What it provides
Terminalholds the decision for one stream. It followsNO_COLOR,CLICOLOR,CLICOLOR_FORCE,FORCE_COLOR,COLORTERM,TERM, andCOLUMNS, in a fixed order.ColorMode::AlwaysandColorMode::Neverskip the variables that decide whether to write colour, and the depth and width still follow theirs.Encoderis the one place an escape sequence is written. It converts every colour to the nearest one the depth can show.Consolerenders anyctl-textwidget at the stream's width and writes it. A reader that closes the pipe early, asheaddoes, givesError::BrokenPipe, so the caller can exit quietly.
Example
A program calls Console::from_process(Stream::Stdout, ColorMode::Auto, DetectOptions::new()) and then print.
This example states the stream's facts instead, so it runs the same with or without a terminal.
use AnsiColor;
use ;
use ;
let terminal = from_probe;
let console = new;
let red = new.fg_color;
let text = styled;
assert_eq!;
Features
detect, on by default, asks the operating system about a stream. It addsProbe::of,Terminal::detect, thefrom_processconstructors, andConsole::print, and brings interminal_size. Without it the crate holds theEncoder, the colour conversion, andTerminal::from_probe, which need no terminal.
ctl-term supports and tests Unix only.
It has no Windows path: it does not turn on virtual-terminal mode, and it reports no window width on another target.
Windows is to come with a Windows CI job.
Documentation
The API reference, including the colour, width, and closed-pipe rules, is on docs.rs.
License
MIT.