phpup/
decorized.rs

1use color::Color;
2use colored::Colorize;
3
4pub trait Decorized: std::fmt::Display {
5    type Color: Color;
6    fn decorized(&self) -> colored::ColoredString {
7        self.to_string().color(Self::Color::color())
8    }
9    fn decorized_with_prefix(&self) -> colored::ColoredString {
10        self.decorized()
11    }
12}
13
14impl Decorized for crate::version::Version {
15    type Color = color::Cyan;
16    fn decorized_with_prefix(&self) -> colored::ColoredString {
17        let with_prefix = format!("PHP {}", self);
18        with_prefix.color(Self::Color::color())
19    }
20}
21impl Decorized for crate::version::Local {
22    type Color = color::Cyan;
23    fn decorized_with_prefix(&self) -> colored::ColoredString {
24        let with_prefix = format!("PHP {}", self);
25        with_prefix.color(Self::Color::color())
26    }
27}
28impl Decorized for crate::version::Alias {
29    type Color = color::Cyan;
30}
31impl Decorized for std::path::Display<'_> {
32    type Color = color::Yellow;
33}
34
35pub mod color {
36    pub trait Color {
37        fn color() -> colored::Color;
38    }
39
40    pub struct Red {}
41    impl Color for Red {
42        fn color() -> colored::Color {
43            colored::Color::Red
44        }
45    }
46    pub struct Green {}
47    impl Color for Green {
48        fn color() -> colored::Color {
49            colored::Color::Green
50        }
51    }
52    pub struct Yellow {}
53    impl Color for Yellow {
54        fn color() -> colored::Color {
55            colored::Color::Yellow
56        }
57    }
58    pub struct Cyan {}
59    impl Color for Cyan {
60        fn color() -> colored::Color {
61            colored::Color::Cyan
62        }
63    }
64}
65
66#[cfg(test)]
67mod test {
68    use super::*;
69    use derive_more::Display;
70
71    #[derive(Debug, Display)]
72    struct Phantom(String);
73    impl Decorized for Phantom {
74        type Color = color::Red;
75    }
76
77    #[test]
78    fn test() {
79        let phantom = Phantom("phantom data".to_owned());
80        println!("normal: {}", phantom);
81        println!("decorized: {}", phantom.decorized())
82    }
83
84    #[test]
85    fn path() {
86        let path = std::env::current_dir().unwrap();
87        println!("{}", path.display().decorized());
88    }
89}