@@ -57,6 +57,9 @@ pub struct Span {
5757 items : Vec < Box < Render > > ,
5858 fg : Option < :: termcolor:: Color > ,
5959 bg : Option < :: termcolor:: Color > ,
60+ bold : bool ,
61+ underline : bool ,
62+ intense : bool ,
6063}
6164
6265impl Span {
@@ -74,12 +77,33 @@ impl Span {
7477 self . bg = Some ( color. parse ( ) ?) ;
7578 Ok ( self )
7679 }
80+
81+ pub fn bold ( mut self , yes : bool ) -> Result < Self , Error > {
82+ self . bold = yes;
83+ Ok ( self )
84+ }
85+
86+ pub fn underline ( mut self , yes : bool ) -> Result < Self , Error > {
87+ self . underline = yes;
88+ Ok ( self )
89+ }
90+
91+ pub fn intense ( mut self , yes : bool ) -> Result < Self , Error > {
92+ self . intense = yes;
93+ Ok ( self )
94+ }
7795}
7896
7997impl Render for Span {
8098 fn render_for_humans ( & self , fmt : & mut human:: Formatter ) -> Result < ( ) , Error > {
81- fmt. writer
82- . set_color ( ColorSpec :: new ( ) . set_fg ( self . fg ) . set_bg ( self . bg ) ) ?;
99+ fmt. writer . set_color (
100+ ColorSpec :: new ( )
101+ . set_fg ( self . fg )
102+ . set_bg ( self . bg )
103+ . set_bold ( self . bold )
104+ . set_underline ( self . underline )
105+ . set_intense ( self . intense ) ,
106+ ) ?;
83107 for item in & self . items {
84108 item. render_for_humans ( fmt) ?;
85109 }
@@ -99,41 +123,71 @@ impl Render for Span {
99123mod test {
100124 use super :: span;
101125 use components:: text;
102- use { human, json, Render } ;
126+ use { human, json, Error , Render } ;
103127
104128 #[ test]
105- fn renders_span_children ( ) {
129+ fn renders_span_children ( ) -> Result < ( ) , Error > {
106130 let item = span ( )
107131 . add_item ( text ( "one" ) )
108132 . add_item ( text ( "two" ) )
109133 . add_item ( span ( ) . add_item ( text ( "three" ) ) ) ;
110134
111135 let human_output = human:: test ( ) ;
112- item. render_for_humans ( & mut human_output. formatter ( ) )
113- . unwrap ( ) ;
136+ item. render_for_humans ( & mut human_output. formatter ( ) ) ?;
114137 assert_eq ! ( & human_output. to_string( ) , "onetwothree" ) ;
115138
116139 let json = json:: test ( ) ;
117- item. render_json ( & mut json. formatter ( ) ) . unwrap ( ) ;
140+ item. render_json ( & mut json. formatter ( ) ) ? ;
118141 assert_eq ! ( json. to_string( ) , "\" one\" \n \" two\" \n \" three\" \n " ) ;
142+ Ok ( ( ) )
119143 }
120144
121145 #[ test]
122- fn test_colored_output ( ) {
146+ fn test_colored_output ( ) -> Result < ( ) , Error > {
123147 let test_target = human:: test_with_color ( ) ;
124148 let mut out = :: new ( ) . add_target ( test_target. target ( ) ) ;
125- out. print (
126- span ( )
127- . add_item ( "hello" )
128- . fg ( "green" )
129- . unwrap ( )
130- . bg ( "blue" )
131- . unwrap ( ) ,
132- ) . unwrap ( ) ;
149+ out. print ( span ( ) . add_item ( "hello" ) . fg ( "green" ) ?. bg ( "blue" ) ?) ?;
133150 assert_eq ! (
134151 test_target. to_string( ) ,
135152 "\u{1b} [0m\u{1b} [32m\u{1b} [44mhello\u{1b} [0m\n "
136- )
153+ ) ;
154+ Ok ( ( ) )
155+ }
156+
157+ #[ test]
158+ fn test_bold_output ( ) -> Result < ( ) , Error > {
159+ let test_target = human:: test_with_color ( ) ;
160+ let mut out = :: new ( ) . add_target ( test_target. target ( ) ) ;
161+ out. print ( span ( ) . add_item ( "hello" ) . bold ( true ) ?) ?;
162+ assert_eq ! (
163+ test_target. to_string( ) ,
164+ "\u{1b} [0m\u{1b} [1mhello\u{1b} [0m\n "
165+ ) ;
166+ Ok ( ( ) )
167+ }
168+
169+ #[ test]
170+ fn test_intense_output ( ) -> Result < ( ) , Error > {
171+ let test_target = human:: test_with_color ( ) ;
172+ let mut out = :: new ( ) . add_target ( test_target. target ( ) ) ;
173+ out. print ( span ( ) . add_item ( "hello" ) . fg ( "green" ) ?. intense ( true ) ?) ?;
174+ assert_eq ! (
175+ test_target. to_string( ) ,
176+ "\u{1b} [0m\u{1b} [38;5;10mhello\u{1b} [0m\n "
177+ ) ;
178+ Ok ( ( ) )
179+ }
180+
181+ #[ test]
182+ fn test_underline_output ( ) -> Result < ( ) , Error > {
183+ let test_target = human:: test_with_color ( ) ;
184+ let mut out = :: new ( ) . add_target ( test_target. target ( ) ) ;
185+ out. print ( span ( ) . add_item ( "hello" ) . underline ( true ) ?) ?;
186+ assert_eq ! (
187+ test_target. to_string( ) ,
188+ "\u{1b} [0m\u{1b} [4mhello\u{1b} [0m\n "
189+ ) ;
190+ Ok ( ( ) )
137191 }
138192
139193 // TODO: Add proptest tests
0 commit comments