// 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 . use std::fmt::{Display, Write}; use crate::{ list::{elements, methods}, support::{CSE, CSI, elements_inner}, }; pub(crate) mod custom; mod list; mod support; #[derive(Debug)] pub struct Canvas(I); impl Colorize for Canvas { fn render_into(self, base: &mut String, use_colors: bool) { write!(base, "{}", self.0).expect("Is written into a string"); if use_colors { // Reset the color and style, if we used colours. base.write_str(CSI).expect("In-memory write"); base.write_str("0").expect("In-memory write"); base.write_str(CSE).expect("In-memory write"); } } } pub trait IntoCanvas: Display + Sized { fn into_canvas(self) -> Canvas { Canvas(self) } methods! { IntoCanvas } } impl IntoCanvas for I {} pub trait Colorize: Sized { /// Turn this colorized struct into a string, by writing into the base. fn render_into(self, base: &mut String, use_colors: bool); /// Turn this colorized struct into a string for consumption. fn render(self, use_colors: bool) -> String { let mut base = String::new(); self.render_into(&mut base, use_colors); base } methods! { Colorize } } elements! {} #[cfg(test)] mod tests { use crate::{Colorize, IntoCanvas}; #[test] fn test_colorize_basic() { let base = "Base".green().render(true); #[rustfmt::skip] let expected = concat!( "\x1b[32m", "Base", "\x1b[0m", ); assert_eq!(base.as_str(), expected); } #[test] fn test_colorize_combo() { let base = "Base".green().on_red().bold().strike_through().render(true); #[rustfmt::skip] let expected = concat!( "\x1b[9m", // strike_through "\x1b[1m", // bold "\x1b[41m", // on_red "\x1b[32m", // green "Base", "\x1b[0m", ); assert_eq!(base.as_str(), expected); } }