indxvec/
printing.rs

1use crate::Printing;
2use std::fmt::Write;
3
4/// When interpolated, makes following foreground rendering bold red
5pub const RD: &str = "\x1B[1;31m";
6/// When interpolated, makes following foreground rendering bold green    
7pub const GR: &str = "\x1B[1;32m";
8/// When interpolated, makes following foreground rendering bold yellow    
9pub const YL: &str = "\x1B[1;33m";
10/// When interpolated, makes following foreground rendering bold blue    
11pub const BL: &str = "\x1B[1;34m";
12/// When interpolated, makes following foreground rendering bold magenta    
13pub const MG: &str = "\x1B[1;35m";
14/// When interpolated, makes following foreground rendering bold cyan    
15pub const CY: &str = "\x1B[1;36m";
16/// Returns the terminal rendering to default
17pub const UN: &str = "\x1B[0m";
18
19impl<T> Printing<T> for &T
20where
21    T: std::fmt::Display,
22{
23    fn to_str(self) -> String {
24        self.to_string()
25    }
26    fn to_plainstr(self) -> String {
27        self.to_string()
28    }
29}
30
31impl<T> Printing<T> for &[T]
32where
33    T: std::fmt::Display,
34{
35    fn to_str(self) -> String {
36        match self.len() {
37            0 => "[]".to_string(),
38            1 => format!("[{}]", self[0]),
39            _ => {
40                self.iter()
41                    .skip(1)
42                    .fold(format!("[{}", self[0]), |mut s, item| {
43                        write!(s, " {}", item).ok();
44                        s
45                    })
46                    + "]"
47            }
48        }
49    }
50
51    fn to_plainstr(self) -> String {
52        match self.len() {
53            0 => "".to_string(),
54            1 => format!("{}", self[0]),
55            _ => self
56                .iter()
57                .skip(1)
58                .fold(format!("{}", self[0]), |mut s, item| {
59                    write!(s, " {}", item).ok();
60                    s
61                }),
62        }
63    }
64}
65
66impl<T,U> Printing<T> for &(T,U)
67where
68    T: std::fmt::Display,
69    U: std::fmt::Display
70{
71    fn to_str(self) -> String {
72        format!("({},{})", self.0, self.1)   
73    }
74
75    fn to_plainstr(self) -> String {
76        format!("{} {}", self.0, self.1)   
77    }
78}
79
80impl<T,U,V> Printing<T> for &(T,U,V)
81where
82    T: std::fmt::Display,
83    U: std::fmt::Display,
84    V: std::fmt::Display
85{
86    fn to_str(self) -> String {
87        format!("({},{},{})", self.0, self.1, self.2)   
88    }
89
90    fn to_plainstr(self) -> String {
91        format!("{} {} {}", self.0, self.1, self.2)   
92    }
93}
94
95impl<T,U,V,W> Printing<T> for &(T,U,V,W)
96where
97    T: std::fmt::Display,
98    U: std::fmt::Display,
99    V: std::fmt::Display,
100    W: std::fmt::Display
101{
102    fn to_str(self) -> String {
103        format!("({},{},{},{})", self.0, self.1, self.2, self.3)   
104    }
105
106    fn to_plainstr(self) -> String {
107        format!("{} {} {} {}", self.0, self.1, self.2, self.3)   
108    }
109}
110
111impl<T> Printing<T> for &[&[T]]
112where
113    T: std::fmt::Display,
114{
115    fn to_str(self) -> String {
116        if self.is_empty() {
117            return "[]".to_string();
118        };
119        self.iter().fold("[\n".to_string(), |mut s, &item| {
120            writeln!(s, " {}", item.to_str()).ok();
121            s
122        }) + "]"
123    }
124
125    /// Without the enclosing square brackets
126    fn to_plainstr(self) -> String {
127        if self.is_empty() {
128            return "".to_string();
129        };
130        self.iter().fold("\n".to_string(), |mut s, &item| {
131            writeln!(s, " {}", item.to_str()).ok();
132            s
133        })
134    }
135}
136
137impl<T> Printing<T> for &[Vec<T>]
138where
139    T: std::fmt::Display,
140{
141    fn to_str(self) -> String {
142        if self.is_empty() {
143            return "[]".to_string();
144        };
145        self.iter().fold("[\n".to_string(), |mut s, item| {
146            writeln!(s, " {}", item.to_str()).ok();
147            s
148        }) + "]"
149    }
150
151    fn to_plainstr(self) -> String {
152        if self.is_empty() {
153            return "".to_string();
154        };
155        self.iter().fold("\n".to_string(), |mut s, item| {
156            writeln!(s, " {}", item.to_str()).ok();
157            s
158        })
159    }
160}