@@ -6,68 +6,82 @@ use crate::*;
66pub mod display_tree;
77pub use display_tree:: * ;
88
9- pub trait Style {
9+ pub trait Style : Display + AsRef < str > {
1010 fn green ( & self ) -> String ;
1111 fn red ( & self ) -> String ;
1212 fn comment ( & self ) -> String ;
1313 fn dimmed ( & self ) -> String ;
1414 fn tooltip ( & self , text : & str ) -> String ;
1515 fn inherited_ref ( & self ) -> String ;
1616 fn code ( & self ) -> String ;
17+
18+ fn wrap_in_tag ( & self , tag_name : & str , tag_args : Option < ( & str , & str ) > ) -> String {
19+ let tag_args = tag_args
20+ . map ( |( k, v) | format ! ( "{k}=\" {v}\" " ) )
21+ . unwrap_or_default ( ) ;
22+ format ! ( "<{tag_name} {tag_args}>{self}</{tag_name}>" )
23+ }
24+ fn span_style ( & self , style : & str ) -> String {
25+ self . wrap_in_tag ( "span" , Some ( ( "style" , style) ) )
26+ }
27+ fn apply_colorize < ' a > ( & ' a self , f : impl Fn ( & ' a str ) -> colored:: ColoredString ) -> String {
28+ // Apply line-by-line so that we can split by line later without messing up escape codes.
29+ self . as_ref ( ) . lines ( ) . map ( |line| f ( line) ) . join ( "\n " )
30+ }
1731}
1832
1933impl < T : Display + AsRef < str > > Style for T {
2034 fn green ( & self ) -> String {
2135 if cfg ! ( target_arch = "wasm32" ) {
22- format ! ( "<span style= \" color: green\" >{self}</span> ")
36+ self . span_style ( " color: green")
2337 } else {
2438 use colored:: Colorize ;
25- <_ as Colorize >:: green ( self . as_ref ( ) ) . to_string ( )
39+ self . apply_colorize ( <_ as Colorize >:: green)
2640 }
2741 }
2842 fn red ( & self ) -> String {
2943 if cfg ! ( target_arch = "wasm32" ) {
30- format ! ( "<span style= \" color: red\" >{self}</span> ")
44+ self . span_style ( " color: red")
3145 } else {
3246 use colored:: Colorize ;
33- <_ as Colorize >:: red ( self . as_ref ( ) ) . to_string ( )
47+ self . apply_colorize ( <_ as Colorize >:: red)
3448 }
3549 }
3650 fn dimmed ( & self ) -> String {
3751 if cfg ! ( target_arch = "wasm32" ) {
38- format ! ( "<span style= \" opacity: 0.5\" >{self}</span> ")
52+ self . span_style ( " opacity: 0.5")
3953 } else {
4054 use colored:: Colorize ;
41- <_ as Colorize >:: dimmed ( self . as_ref ( ) ) . to_string ( )
55+ self . apply_colorize ( <_ as Colorize >:: dimmed)
4256 }
4357 }
4458 fn comment ( & self ) -> String {
4559 if cfg ! ( target_arch = "wasm32" ) {
46- format ! ( "<span style= \" color: dimgray\" >{self}</span> ")
60+ self . span_style ( " color: dimgray")
4761 } else {
4862 use colored:: Colorize ;
49- <_ as Colorize >:: dimmed ( self . as_ref ( ) ) . to_string ( )
63+ self . apply_colorize ( <_ as Colorize >:: dimmed)
5064 }
5165 }
5266 fn tooltip ( & self , text : & str ) -> String {
5367 if cfg ! ( target_arch = "wasm32" ) {
54- format ! ( "< span title= \" { text} \" >{self}</span>" )
68+ self . wrap_in_tag ( " span" , Some ( ( " title" , text) ) )
5569 } else {
5670 self . to_string ( )
5771 }
5872 }
5973 fn inherited_ref ( & self ) -> String {
6074 if cfg ! ( target_arch = "wasm32" ) {
61- format ! ( "< span class= \" inherited-ref\" >{self}</span>" )
75+ self . wrap_in_tag ( " span" , Some ( ( " class" , " inherited-ref" ) ) )
6276 } else {
6377 use colored:: Colorize ;
64- <_ as Colorize >:: dimmed ( self . as_ref ( ) ) . to_string ( )
78+ self . apply_colorize ( <_ as Colorize >:: dimmed)
6579 }
6680 . tooltip ( "inherited reference" )
6781 }
6882 fn code ( & self ) -> String {
6983 if cfg ! ( target_arch = "wasm32" ) {
70- format ! ( "< code>{self}</code>" )
84+ self . wrap_in_tag ( " code" , None )
7185 } else {
7286 format ! ( "`{self}`" )
7387 }
0 commit comments