Skip to main content

detect_light

Function detect_light 

Source
pub fn detect_light(timeout: Duration) -> Option<bool>
Expand description

Query the terminal for its background colour and decide light vs. dark. Returns Some(true) for light, Some(false) for dark, None when the terminal didn’t respond within timeout.

Implementation (Unix):

  1. Write OSC 11 query (ESC ] 11 ; ? BEL) to stdout.
  2. Wait up to timeout for stdin to become readable (libc::poll, single fd).
  3. Read available bytes via libc::read, parse the rgb:RRRR/GGGG/BBBB payload.
  4. Compute Rec. 709 relative luminance, threshold at 128/255.

Windows / non-Unix: returns None immediately. Windows conhost doesn’t respond to OSC 11 at all; Windows Terminal does but the libc::poll path isn’t available there. A future improvement can add a Win32-specific path via PeekConsoleInput.