Function terminal_colorsaurus::color_scheme
source · pub fn color_scheme(options: QueryOptions) -> Result<ColorScheme>
Expand description
Queries the terminal for it’s color scheme (foreground and background color).
§Caveats
Extra care needs to be taken on Unix if your program might share
the terminal with another program. This might be the case
if you expect your output to be used with a pager e.g. your_program
| less
.
In that case, a race condition exists because the pager will also set the terminal to raw mode.
The pager
example shows a heuristic to deal with this issue.
Examples found in repository?
examples/pager.rs (line 30)
26 27 28 29 30 31 32 33 34 35 36 37
fn main() -> Result<(), Box<dyn Error>> {
if stdout().is_terminal() {
eprintln!(
"Here's the color scheme: {:#?}",
color_scheme(QueryOptions::default())?
);
} else {
eprintln!("No color scheme for you today :/");
}
Ok(())
}
More examples
examples/theme.rs (line 8)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
fn main() -> Result<(), Box<dyn Error>> {
let colors = color_scheme(QueryOptions::default())?;
let theme = if colors.is_light_on_dark() {
"light on dark"
} else {
"dark on light"
};
println!(
"{theme}, fg: {}, bg: {}",
colors.foreground.perceived_lightness(),
colors.background.perceived_lightness()
);
Ok(())
}