CliCapabilities.detect constructor
Detects the capabilities from what stdio reports and from
environment, as twk_core reads it where it opens a terminal, with
FORCE_COLOR, --no-color and --no-input on top.
Styling: TWK_PLAIN=1 (twk_core's plainAsked) switches it off, then
FORCE_COLOR wins over the rest (0 or false switch styling off,
any other non-empty value on); then where the environment takes no
colors (twk_core's colorModeFor: a non-empty NO_COLOR,
TERM=dumb) it is off; otherwise output must be a terminal that
supports ANSI escapes.
Input is interactive only on a terminal, and there only where the
environment does not say CI (twk_core's runsInCi: CI,
CONTINUOUS_INTEGRATION, BUILD_NUMBER or RUN_ID); interactive
replaces the CI rule, so false is --no-input, and true asks
under CI: still only on a terminal, since a pipe has nobody behind
it.
ansi replaces the styling rules (--no-color) and hyperlinks the
default of styled output on a terminal. The colors are twk_core's
(colorModeFor, windows for a Windows console): 24-bit, 256 or 16
by COLORTERM, TERM and TERM_PROGRAM, and 16 where styling is
forced on an environment that takes none; the underline styles are
shown as twk_core's styledUnderlinesFor has it: on all terminals
but Terminal.app.
Implementation
factory CliCapabilities.detect({
required Map<String, String> environment,
required bool inputIsTerminal,
required bool outputIsTerminal,
required bool outputSupportsAnsi,
bool? errorIsTerminal,
bool? ansi,
bool? interactive,
bool? hyperlinks,
bool windows = false,
}) {
final colors = colorModeFor(environment, windows: windows);
return CliCapabilities(
ansi:
ansi ??
_detectAnsi(
environment,
colors,
outputIsTerminal: outputIsTerminal,
outputSupportsAnsi: outputSupportsAnsi,
),
interactiveInput:
inputIsTerminal && (interactive ?? !runsInCi(environment)),
outputIsTerminal: outputIsTerminal,
errorIsTerminal: errorIsTerminal,
hyperlinks: hyperlinks,
colorMode: colors == ColorMode.none ? ColorMode.ansi16 : colors,
styledUnderlines: styledUnderlinesFor(environment),
);
}