Crate terminal_light[][src]

Expand description

This crate answers the question “Is the terminal dark or light?”.

It provides

  • the background color as RGB
  • the background color’s luma, which varies from 0 (black) to 1 (white)

A use case in a TUI is to determine what set of colors would be most suitable depending on the terminal’s background:

let should_use_light_skin = terminal_light::luma()
    .map_or(false, |luma| luma > 0.6);

The behavior and result of the bg_color and luma functions depend:

On non unix-like platforms, you’ll receive a TlError::Unsupported error (help welcome to improve this).

On unix-like (linux, Darwin, etc.), a terminal sequence is sent to stdout and the response is read (with timeout) on stdin.

If the terminal is somehow modern, the answer is received and analyzed to give you the color.

If the terminal doesn’t answer fast enough, a TlError::Timeout error is returned (current timeout is 20ms).

If the terminal’s answer isn’t understood, an other type of error is returned.

Structs

RGB color, with u16 components

Enums

terminal-light error type

Functions

Try to return the RGB background color of the terminal.

Try to return the “luma” value of the terminal’s background, characterizing the “light” of the color, going from 0 (black) to 1 (white).