about summary refs log tree commit diff stats
path: root/crates/colors/src/support.rs
blob: 3c3f87d3aa095c1ab24d05a683ce5b1039c8cf07 (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

pub(super) const CSI: &str = "\x1b[";
pub(super) const CSE: &str = "m";

macro_rules! elements_inner {
    (
        $(
            $name:ident $_:ident $number:tt
        ),*
        $(,)?
    ) => {
        $(
            #[derive(Debug)]
            pub struct $name<I: Colorize>(I);

            impl<I: Colorize> Colorize for $name<I> {
                fn render_into(self, base: &mut String, use_colors: bool) {
                    elements_inner! {@parse_number $number}

                    if use_colors {
                        base.write_str(CSI).expect("In-memory write");
                        base.write_str(NUMBERS).expect("In-memory write");
                        base.write_str(CSE).expect("In-memory write");
                    }
                    self.0.render_into(base, use_colors);
                    // The canvas is resetting the colours again.
                }
            }
        )*
    };

    (@parse_number $single:literal) => {
        const NUMBERS: &str = stringify!($single);
    };
    (@parse_number ($red:literal, $green:literal, $blue:literal)) => {
        const NUMBERS_U8: [u8; 18] = $crate::custom::rgb_to_ansi($red, $green, $blue, $crate::custom::Plane::Fg);

        const NUMBERS: &str = $crate::custom::bytes_to_str(&NUMBERS_U8);
    }
}
pub(super) use elements_inner;

macro_rules! methods_inner {
    (
        Colorize

        $(
            $struct_name:ident $fn_name:ident $_:tt
        ),*
        $(,)?
    ) => {
        $(
            fn $fn_name(self) -> $struct_name<Self> {
                $struct_name(self)
            }
        )*
    };
    (
        IntoCanvas

        $(
            $struct_name:ident $fn_name:ident $_:tt
        ),*
        $(,)?
    ) => {
        $(
            fn $fn_name(self) -> $struct_name<Canvas<Self>> {
                $struct_name(Canvas(self))
            }
        )*
    };
}
pub(super) use methods_inner;

macro_rules! prepend_input {
    (
        $(
            $existing_macro_name:path as $new_macro_name:ident $(($macro_rule:tt -> $macro_apply:tt))?
        ),*
        $(,)?

        <shared_input>
        $shared_input:tt
    ) => {
        $(
            prepend_input! {
                @generate_macro
                $existing_macro_name as $new_macro_name $(($macro_rule -> $macro_apply))?
                <shared_input>
                $shared_input
            }
        )*
    };

    (
        @generate_macro
        $existing_macro_name:path as $new_macro_name:ident $((($($macro_rule:tt)*) -> {$($macro_apply:tt)*}))?

        <shared_input>
        {
            $(
                $shared_input:tt
            )*
        }
    ) => {
        macro_rules! $new_macro_name {
            ($($($macro_rule)*)?) => {
                $existing_macro_name! {
                    $($($macro_apply)*)?
                    $($shared_input)*
                }
            }
        }
        pub(super) use $new_macro_name;
    }
}
pub(crate) use prepend_input;