1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//!  Convert between [`ansi_term`](https://lib.rs/ansi_term) and generic styling types

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(missing_docs)]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

/// Adapt generic styling to [`ansi_term`]
pub fn to_ansi_term(astyle: anstyle::Style) -> ansi_term::Style {
    let mut style = ansi_term::Style::new();

    if let Some((fg, fg_bold)) = astyle.get_fg_color().map(to_ansi_color) {
        style = style.fg(fg);
        if fg_bold {
            style = style.bold();
        }
    }
    if let Some((bg, _)) = astyle.get_bg_color().map(to_ansi_color) {
        style = style.on(bg);
    }

    let effects = astyle.get_effects();
    if effects.contains(anstyle::Effects::BOLD) {
        style = style.bold();
    }
    if effects.contains(anstyle::Effects::DIMMED) {
        style = style.dimmed();
    }
    if effects.contains(anstyle::Effects::ITALIC) {
        style = style.italic();
    }
    if effects.contains(anstyle::Effects::UNDERLINE) {
        style = style.underline();
    }
    if effects.contains(anstyle::Effects::BLINK) {
        style = style.blink();
    }
    if effects.contains(anstyle::Effects::INVERT) {
        style = style.reverse();
    }
    if effects.contains(anstyle::Effects::HIDDEN) {
        style = style.hidden();
    }
    if effects.contains(anstyle::Effects::STRIKETHROUGH) {
        style = style.strikethrough();
    }
    style
}

fn to_ansi_color(color: anstyle::Color) -> (ansi_term::Color, bool) {
    match color {
        anstyle::Color::Ansi(ansi) => ansi_to_ansi_color(ansi),
        anstyle::Color::Ansi256(xterm) => (xterm_to_ansi_color(xterm), false),
        anstyle::Color::Rgb(rgb) => (rgb_to_ansi_color(rgb), false),
    }
}

fn ansi_to_ansi_color(color: anstyle::AnsiColor) -> (ansi_term::Color, bool) {
    match color {
        anstyle::AnsiColor::Black => (ansi_term::Color::Black, false),
        anstyle::AnsiColor::Red => (ansi_term::Color::Red, false),
        anstyle::AnsiColor::Green => (ansi_term::Color::Green, false),
        anstyle::AnsiColor::Yellow => (ansi_term::Color::Yellow, false),
        anstyle::AnsiColor::Blue => (ansi_term::Color::Blue, false),
        anstyle::AnsiColor::Magenta => (ansi_term::Color::Purple, false),
        anstyle::AnsiColor::Cyan => (ansi_term::Color::Cyan, false),
        anstyle::AnsiColor::White => (ansi_term::Color::White, false),
        anstyle::AnsiColor::BrightBlack => (ansi_term::Color::Black, true),
        anstyle::AnsiColor::BrightRed => (ansi_term::Color::Red, true),
        anstyle::AnsiColor::BrightGreen => (ansi_term::Color::Green, true),
        anstyle::AnsiColor::BrightYellow => (ansi_term::Color::Yellow, true),
        anstyle::AnsiColor::BrightBlue => (ansi_term::Color::Black, true),
        anstyle::AnsiColor::BrightMagenta => (ansi_term::Color::Purple, true),
        anstyle::AnsiColor::BrightCyan => (ansi_term::Color::Cyan, true),
        anstyle::AnsiColor::BrightWhite => (ansi_term::Color::White, true),
    }
}

fn xterm_to_ansi_color(color: anstyle::Ansi256Color) -> ansi_term::Color {
    ansi_term::Color::Fixed(color.0)
}

fn rgb_to_ansi_color(color: anstyle::RgbColor) -> ansi_term::Color {
    ansi_term::Color::RGB(color.0, color.1, color.2)
}