Expand description
Determines the background and foreground color of the terminal
using the OSC 10
and OSC 11
terminal sequence.
This crate helps answer the question “Is this terminal dark or light?”.
§Features
- Background and foreground color detection.
- Uses a fast and reliable heuristic to detect if the terminal supports color querying.
- Correct perceived lightness calculation.
- Works on Windows (starting with Windows Terminal v1.22).
- Safely restores the terminal from raw mode even if the library errors or panicks.
- Does not send any escape sequences if
TERM=dumb
. - Works even if all of stderr, stdout and stdin are redirected.
- Supports a timeout (for situations with high latency such as an SSH connection).
§Terminal Support
terminal-colorsaurus
works with most modern terminals and has been tested extensively.
It’s also really good at detecting when querying for the terminal’s colors is not supported.
§Example 1: Test If the Terminal Uses a Dark Background
use terminal_colorsaurus::{theme_mode, ThemeMode, QueryOptions};
let theme_mode = theme_mode(QueryOptions::default()).unwrap();
dbg!(theme_mode == ThemeMode::Dark);
§Example 2: Get the Terminal’s Foreground Color
use terminal_colorsaurus::{foreground_color, QueryOptions};
let fg = foreground_color(QueryOptions::default()).unwrap();
println!("rgb({}, {}, {})", fg.r, fg.g, fg.b);
§Optional Dependencies
rgb
— Enable this feature to convert betweenColor
andrgb::RGB16
/rgb::RGB8
.anstyle
— Enable this feature to convertColor
toanstyle::RgbColor
.
Modules§
- comparison
docsrs
- Comparison with other crates in the ecosystem.
- feature_
detection docsrs
- How does colorsaurus detect if a terminal supports querying its colors?
- latency
docsrs
- What kind of latency do I have to expect?
- terminal_
survey docsrs
- The following terminals have known support or non-support for
querying for the background/foreground colors and have been tested
with
terminal-colorsaurus
:
Structs§
- Color
- An RGB color with 16 bits per channel.
You can use
Color::scale_to_8bit
to convert to an 8bit RGB color. - Color
Palette - The subset of the terminal’s color palette needed for
deriving the
ThemeMode
, namely: the foreground and background color. Retrieved by callingcolor_palette
. - Query
Options - Options to be used with
foreground_color
andbackground_color
. You should almost always use the unchangedQueryOptions::default
value.
Enums§
Functions§
- background_
color - Queries the terminal for it’s background color.
If you also need the foreground color it is more efficient to usecolor_palette
instead. - color_
palette - Queries the terminal for it’s color palette (foreground and background color).
- foreground_
color - Queries the terminal for it’s foreground color.
If you also need the foreground color it is more efficient to usecolor_palette
instead. - theme_
mode - Detects if the terminal is dark or light.
Type Aliases§
- Result
- Result used by this library.