Class TerminalCapabilities
Detects the ANSI capability of a console stream for Auto. The decision
is conservative about whether ANSI is allowed at all (redirected streams,
NO_COLOR,
CLICOLOR=0, and TERM=dumb/empty all disable it) but practical once it is: a normal
interactive non-Windows terminal resolves to Ansi256 without requiring
TERM to advertise 256color. The pure Detect(Func<string, string?>, bool, bool, bool) core takes all environment
inputs as parameters so it can be unit-tested without touching the real process environment.public static class TerminalCapabilities
- Inheritance
-
TerminalCapabilities
- Inherited Members
Fields
DefaultOutputHeight
Deterministic fallback height (in rows) used when a concrete terminal height is required (the
full-screen render buffer and interactive viewport) but cannot be read safely. Structured,
non-interactive output does not clamp to this — it leaves height unbounded under redirection.
public const int DefaultOutputHeight = 30
Field Value
DefaultOutputWidth
Deterministic fallback width (in columns) used by GetSafeOutputWidth(bool) when the real
terminal width cannot be read safely. The fallback affects layout/wrapping only; it does
not imply ANSI/colour support (that is decided separately by Detect(Func<string, string?>, bool, bool, bool)).
public const int DefaultOutputWidth = 120
Field Value
Methods
Detect(Func<string, string?>, bool, bool, bool)
Pure capability detection for Auto.
public static CliAnsiSupport Detect(Func<string, string?> getEnvironmentVariable, bool isRedirected, bool isWindows, bool windowsVtSupported)
Parameters
getEnvironmentVariableFunc<string, string>- Environment-variable lookup (returns
nullwhen unset). isRedirectedbool- Whether the target stream is redirected/captured.
isWindowsbool- Whether the host OS is Windows.
windowsVtSupportedbool- Result of the Windows VT probe for this stream. Ignored on non-Windows.
Returns
ForStderr()
Detects the stderr ANSI capability from the real process environment.
public static CliAnsiSupport ForStderr()
Returns
ForStdout()
Detects the stdout ANSI capability from the real process environment.
public static CliAnsiSupport ForStdout()
Returns
GetSafeOutputHeight(bool)
Returns the current terminal height when it can be read safely, otherwise
null.
A null result means "no soft height bound" — structured output then emits all rows
rather than clamping to a window that may not exist under redirection.public static int? GetSafeOutputHeight(bool forError = false)
Parameters
forErrorbool- When
true, gates on stderr redirection instead of stdout.
Returns
- int?
GetSafeOutputWidth(bool)
Returns the current terminal width when it can be read safely, otherwise
DefaultOutputWidth. The result is always at least 1.
WindowWidth is a terminal capability, not a guaranteed value: reading it when stdout is redirected, piped, captured, or there is no interactive console can throw (e.g. "The handle is invalid.") or report a nonsensical value. Rendering and layout code must call this instead of reading WindowWidth directly.
public static int GetSafeOutputWidth(bool forError = false)
Parameters
forErrorbool- When
true, gates on stderr redirection instead of stdout.
Returns
ResolveHeight(bool, Func<int>)
Pure height-resolution core (see ResolveWidth(bool, Func<int>)). Returns
null when
redirected, when readHeight throws, or when it yields a non-positive value.public static int? ResolveHeight(bool isRedirected, Func<int> readHeight)
Parameters
Returns
- int?
ResolveWidth(bool, Func<int>)
Pure width-resolution core, with redirection state and the raw width reader injected so it can
be unit-tested without touching the real console. Returns DefaultOutputWidth when
redirected, when
readWidth throws, or when it yields a non-positive value;
otherwise the value read (always at least 1).public static int ResolveWidth(bool isRedirected, Func<int> readWidth)