11use crate::errors::{AnyError, ErrorNode};
22use crate::{Location, Token};
3+ use colored::*;
34
45fn escape_string(s: &str) -> String {
56 s.replace('\\', "\\\\")
@@ -10,15 +11,15 @@ fn escape_string(s: &str) -> String {
1011}
1112
1213fn format_string_value(value: &str) -> String {
13- format!("\"{}\"", escape_string(value))
14+ format!("\"{}\"", escape_string(value)).green().to_string()
1415}
1516
1617fn format_token_value(token: &Option< Token > ) -> String {
17- token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "β
".to_string())
18+ token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "β
".magenta(). to_string())
1819}
1920
2021fn format_bool_value(value: bool) -> String {
21- value.to_string()
22+ value.to_string().magenta().bold().to_string()
2223}
2324
2425fn format_node_value< T: Node + ?Sized > (node: &Option< Box < T > > , prefix: &str, add_spacing: bool) -> String {
@@ -31,7 +32,7 @@ fn format_node_value<T: Node + ?Sized>(node: &Option<Box<T>>, prefix: &str, add_
3132 }
3233 output
3334 } else {
34- "β
\n".to_string( )
35+ format!("{} \n", "β
".magenta() )
3536 }
3637}
3738
@@ -49,22 +50,24 @@ fn inspect_errors(errors: &[AnyError], prefix: &str) -> String {
4950 }
5051
5152 let mut output = String::new();
52- output.push_str(&format!("βββ errors: ({} error{})\n",
53- errors.len(),
54- if errors.len() == 1 { "" } else { "s" }
53+ output.push_str(&format!("{} {}: {}\n",
54+ "βββ".white(),
55+ "errors".red().bold(),
56+ format!("({} error{})", errors.len(), if errors.len() == 1 { "" } else { "s" }).dimmed()
5557 ));
5658
5759 for (i, error) in errors.iter().enumerate() {
5860 let is_last = i == errors.len() - 1;
5961 let symbol = if is_last { "βββ " } else { "βββ " };
60- let next_prefix = if is_last { " " } else { "β " };
62+ let next_prefix_str = if is_last { " " } else { "β " };
63+ let next_prefix = next_prefix_str.white().to_string();
6164
6265 let tree = error.tree_inspect();
6366 let tree = tree.trim_end_matches('\n');
64- output.push_str(&format!("{}{}{}\n", prefix, symbol, tree.replace('\n', &format!("\n{}{}", prefix, next_prefix))));
67+ output.push_str(&format!("{}{}{}\n", prefix, symbol.white() , tree.replace('\n', &format!("\n{}{}", prefix, next_prefix))));
6568
6669 if !is_last {
67- output.push_str(&format!("{}β \n", prefix));
70+ output.push_str(&format!("{}{} \n", prefix, "β ".white() ));
6871 }
6972 }
7073
@@ -74,28 +77,29 @@ fn inspect_errors(errors: &[AnyError], prefix: &str) -> String {
7477
7578fn inspect_array(array: &[AnyNode], prefix: &str) -> String {
7679 if array.is_empty() {
77- return "[] \n".to_string( );
80+ return format!("{} \n", "[]".dimmed() );
7881 }
7982
8083 let mut output = String::new();
81- output.push_str(&format!("( {} item{})\n ", array.len(), if array.len() == 1 { "" } else { "s" }));
84+ output.push_str(&format!("{}\n", format!("( {} item{})", array.len(), if array.len() == 1 { "" } else { "s" }).dimmed() ));
8285
8386 for (i, item) in array.iter().enumerate() {
8487 let is_last = i == array.len() - 1;
8588 let symbol = if is_last { "βββ " } else { "βββ " };
86- let next_prefix = if is_last { " " } else { "β " };
89+ let next_prefix_str = if is_last { " " } else { "β " };
90+ let next_prefix = next_prefix_str.white().to_string();
8791
8892 let tree = item.tree_inspect();
8993 let tree = tree.trim_end_matches('\n');
9094
9195 output.push_str(prefix);
92- output.push_str(symbol);
96+ output.push_str(& symbol.white().to_string() );
9397 output.push_str(&tree.replace('\n', &format!("\n{}{}", prefix, next_prefix)));
9498 output.push('\n');
9599
96100 if !is_last {
97101 output.push_str(prefix);
98- output.push_str(next_prefix);
102+ output.push_str(& next_prefix);
99103 output.push('\n');
100104 }
101105 }
@@ -109,12 +113,12 @@ fn inspect_node_field<T: Node + ?Sized>(node: &T, prefix: &str) -> String {
109113
110114 let lines: Vec< &str> = tree.split('\n').collect();
111115 if lines.is_empty() {
112- return "β
\n".to_string( );
116+ return format!("{} \n", "β
".magenta() );
113117 }
114118
115119 let mut result = String::new();
116120 result.push_str(prefix);
117- result.push_str("βββ ");
121+ result.push_str(& "βββ ".white().to_string() );
118122 result.push_str(lines[0]);
119123 result.push('\n');
120124
@@ -336,27 +340,31 @@ impl Node for <%= node.name %> {
336340 fn tree_inspect(&self) -> String {
337341 let mut output = String::new();
338342
339- output.push_str(&format!("@ <%= node . name %> (location: {})\n", self.location));
340- output.push_str(&format_errors_field(&self.errors, <%- if node . fields . any? -%> "β "<%- else -%> " "<%- end -%> ));
343+ output.push_str(&format!("{} {} {}\n",
344+ "@".white(),
345+ "<%= node . name %> ".yellow().bold(),
346+ format!("(location: {})", self.location).dimmed()
347+ ));
348+ output.push_str(&format_errors_field(&self.errors, &<%- if node . fields . any? -%> "β "<%- else -%> " "<%- end -%> .white().to_string()));
341349 <%- if node . fields . any? -%>
342350 <%- node . fields . each_with_index do |field , index | -%>
343351 <%- is_last = index == node . fields . length - 1 -%>
344352 <%- symbol = is_last ? "βββ " : "βββ " -%>
345353 <%- case field -%>
346354 <%- when Herb ::Template ::StringField , Herb ::Template ::ElementSourceField -%>
347- output.push_str(&format!("<%= symbol %> <%= field . name %> : {}\n" , format_string_value(&self.<%= field . name %> )));
355+ output.push_str(&format!("{}{}: {}\n", " <%= symbol %> ".white(), " <%= field . name %> ".white() , format_string_value(&self.<%= field . name %> )));
348356 <%- when Herb ::Template ::TokenField -%>
349- output.push_str(&format!("<%= symbol %> <%= field . name %> : {}\n" , format_token_value(&self.<%= field . name %> )));
357+ output.push_str(&format!("{}{}: {}\n", " <%= symbol %> ".white(), " <%= field . name %> ".white() , format_token_value(&self.<%= field . name %> )));
350358 <%- when Herb ::Template ::BooleanField -%>
351- output.push_str(&format!("<%= symbol %> <%= field . name %> : {}\n" , format_bool_value(self.<%= field . name %> )));
359+ output.push_str(&format!("{}{}: {}\n", " <%= symbol %> ".white(), " <%= field . name %> ".white() , format_bool_value(self.<%= field . name %> )));
352360 <%- when Herb ::Template ::ArrayField -%>
353- output.push_str(&format!("<%= symbol %> <%= field . name %> : {}" , format_array_value(&self.<%= field . name %> , "<%= is_last ? " " : "β " %> ")));
361+ output.push_str(&format!("{}{}: {}", " <%= symbol %> ".white(), " <%= field . name %> ".white() , format_array_value(&self.<%= field . name %> , & "<%= is_last ? " " : "β " %> ".white().to_string() )));
354362 <%- when Herb ::Template ::NodeField -%>
355- output.push_str(&format!("<%= symbol %> <%= field . name %> : {}" , format_node_value(&self.<%= field . name %> , "<%= is_last ? " " : "β " %> ", <%= !is_last %> )));
363+ output.push_str(&format!("{}{}: {}", " <%= symbol %> ".white(), " <%= field . name %> ".white() , format_node_value(&self.<%= field . name %> , & "<%= is_last ? " " : "β " %> ".white().to_string() , <%= !is_last %> )));
356364 <%- end -%>
357365 <%- end -%>
358366 <%- else -%>
359- output.push_str(" βββ (no fields)\n" );
367+ output.push_str(&format!("{} {}\n", " βββ".white(), " (no fields)".dimmed()) );
360368 <%- end -%>
361369
362370 output
0 commit comments