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
|
// 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>.
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: Display>(I);
impl<I: Display> Colorize for Canvas<I> {
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<Self> {
Canvas(self)
}
methods! { IntoCanvas }
}
impl<I: Display> 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);
}
}
|