17 releases
new 0.4.6 | Nov 21, 2024 |
---|---|
0.4.4 | Jul 18, 2024 |
0.3.3 | Mar 16, 2024 |
#98 in Command-line interface
4,405 downloads per month
Used in git-delta
46KB
911 lines
terminal-colorsaurus 🦕
Determines the background and foreground color of the terminal
using the OSC 10
and OSC 11
escape codes.
This library helps answer the question «Is this terminal dark or light?».
Works in all major terminals including Windows Terminal (starting with v1.22).
Example
use terminal_colorsaurus::{color_scheme, QueryOptions, ColorScheme};
match color_scheme(QueryOptions::default()).unwrap() {
ColorScheme::Dark => { /* ... */ },
ColorScheme::Light => { /* ... */ },
}
Docs
Inspiration
This crate borrows ideas from many other projects. This list is by no means exhaustive.
- xterm-query: Use
mio
to wait for the terminal's response with a timeout. - termbg: Lists a lot of terminals which served as a good starting point for me to test terminals as well.
- macOS doesn't like polling /dev/tty by Nathan Craddock
- This excellent answer on Stack Overflow for determining the perceived lightness of a color.
- This comment in the Terminal WG for the
DA1
trick to easily detect terminals that don't support querying the colors withOSC 10
/OSC 11
.
License
Licensed under either of
- Apache License, Version 2.0 (license-apache.txt or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (license-mit.txt or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
~0.4–8.5MB
~73K SLoC