Skip to main content

Module environment

Module environment 

Source
Expand description

Injectable environment detection.

This module centralizes process-global detection of terminal properties — width, TTY status, and ANSI color capability — behind overridable function pointers so tests can force specific values without touching real environment state.

It follows the same pattern used by set_theme_detector and set_icon_detector.

§Usage

In application code, call the detect_* functions. They resolve to real terminal queries by default:

use standout_render::{detect_terminal_width, detect_is_tty, detect_color_capability};

let _width = detect_terminal_width();
let _tty = detect_is_tty();
let _color = detect_color_capability();

In tests, override any of them with a function pointer or a non-capturing closure (both coerce to fn(...) -> T):

use standout_render::{set_terminal_width_detector, detect_terminal_width};

set_terminal_width_detector(|| Some(80));
assert_eq!(detect_terminal_width(), Some(80));

Capturing closures are not supported — if you need per-test state, route it through a thread-local or a static the detector reads from.

Overrides are process-global, so tests that set them should be annotated with #[serial] (via the serial_test crate) and should use DetectorGuard to guarantee cleanup even when the test panics.

Structs§

DetectorGuard
RAII guard that calls reset_detectors when dropped.

Functions§

detect_color_capability
Returns true when ANSI color output is supported on stdout.
detect_is_tty
Returns true when stdout is attached to a terminal.
detect_terminal_width
Returns the current terminal width in columns, or None when unavailable.
reset_detectors
Resets every environment detector in this module to its default (real-terminal) implementation.
set_color_capability_detector
Overrides the detector used to check whether ANSI color is supported on stdout.
set_terminal_width_detector
Overrides the detector used to query terminal width.
set_tty_detector
Overrides the detector used to check whether stdout is a TTY.