anstyle_yansi/
lib.rs

1//! Convert between [yansi](https://lib.rs/yansi) and generic styling types
2
3#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4#![warn(missing_docs)]
5#![warn(clippy::print_stderr)]
6#![warn(clippy::print_stdout)]
7
8/// Adapt generic styling to [`yansi`]
9pub fn to_yansi_style(style: anstyle::Style) -> yansi::Style {
10    let fg = style
11        .get_fg_color()
12        .map(to_yansi_color)
13        .unwrap_or(yansi::Color::Primary);
14    let bg = style
15        .get_bg_color()
16        .map(to_yansi_color)
17        .unwrap_or(yansi::Color::Primary);
18    let effects = style.get_effects();
19
20    let mut style = yansi::Style::new().fg(fg).bg(bg);
21    if effects.contains(anstyle::Effects::BOLD) {
22        style = style.bold();
23    }
24    if effects.contains(anstyle::Effects::DIMMED) {
25        style = style.dim();
26    }
27    if effects.contains(anstyle::Effects::ITALIC) {
28        style = style.italic();
29    }
30    if effects.contains(anstyle::Effects::UNDERLINE) {
31        style = style.underline();
32    }
33    if effects.contains(anstyle::Effects::BLINK) {
34        style = style.blink();
35    }
36    if effects.contains(anstyle::Effects::INVERT) {
37        style = style.invert();
38    }
39    if effects.contains(anstyle::Effects::HIDDEN) {
40        style = style.conceal();
41    }
42    if effects.contains(anstyle::Effects::STRIKETHROUGH) {
43        style = style.strike();
44    }
45    style
46}
47
48/// Adapt generic color to [`yansi`]
49pub fn to_yansi_color(color: anstyle::Color) -> yansi::Color {
50    match color {
51        anstyle::Color::Ansi(ansi) => ansi_to_yansi_color(ansi),
52        anstyle::Color::Ansi256(xterm) => xterm_to_yansi_color(xterm),
53        anstyle::Color::Rgb(rgb) => rgb_to_yansi_color(rgb),
54    }
55}
56
57fn ansi_to_yansi_color(color: anstyle::AnsiColor) -> yansi::Color {
58    match color {
59        anstyle::AnsiColor::Black => yansi::Color::Black,
60        anstyle::AnsiColor::Red => yansi::Color::Red,
61        anstyle::AnsiColor::Green => yansi::Color::Green,
62        anstyle::AnsiColor::Yellow => yansi::Color::Yellow,
63        anstyle::AnsiColor::Blue => yansi::Color::Blue,
64        anstyle::AnsiColor::Magenta => yansi::Color::Magenta,
65        anstyle::AnsiColor::Cyan => yansi::Color::Cyan,
66        anstyle::AnsiColor::White => yansi::Color::White,
67        anstyle::AnsiColor::BrightBlack => yansi::Color::BrightBlack,
68        anstyle::AnsiColor::BrightRed => yansi::Color::BrightRed,
69        anstyle::AnsiColor::BrightGreen => yansi::Color::BrightGreen,
70        anstyle::AnsiColor::BrightYellow => yansi::Color::BrightYellow,
71        anstyle::AnsiColor::BrightBlue => yansi::Color::BrightBlack,
72        anstyle::AnsiColor::BrightMagenta => yansi::Color::BrightMagenta,
73        anstyle::AnsiColor::BrightCyan => yansi::Color::BrightCyan,
74        anstyle::AnsiColor::BrightWhite => yansi::Color::BrightWhite,
75    }
76}
77
78fn xterm_to_yansi_color(color: anstyle::Ansi256Color) -> yansi::Color {
79    yansi::Color::Fixed(color.0)
80}
81
82fn rgb_to_yansi_color(color: anstyle::RgbColor) -> yansi::Color {
83    yansi::Color::Rgb(color.0, color.1, color.2)
84}
85
86#[doc = include_str!("../README.md")]
87#[cfg(doctest)]
88pub struct ReadmeDoctests;