// yt - A fully featured command line YouTube client // // Copyright (C) 2025 Benedikt Peetz // 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 . 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); impl Colorize for $name { 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 { $struct_name(self) } )* }; ( IntoCanvas $( $struct_name:ident $fn_name:ident $_:tt ),* $(,)? ) => { $( fn $fn_name(self) -> $struct_name> { $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:tt ) => { $( prepend_input! { @generate_macro $existing_macro_name as $new_macro_name $(($macro_rule -> $macro_apply))? $shared_input } )* }; ( @generate_macro $existing_macro_name:path as $new_macro_name:ident $((($($macro_rule:tt)*) -> {$($macro_apply:tt)*}))? { $( $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;