3131//! styling.
3232//!
3333//! The above snippet has been built out of the following structure:
34+ use crate :: snippet;
3435use std:: cmp:: { max, min} ;
3536use std:: fmt:: { Display , Write } ;
3637use std:: { cmp, fmt} ;
38+ use yansi_term:: Style ;
3739
38- use crate :: formatter:: style:: { Style , StyleClass } ;
39- use crate :: formatter:: { get_term_style, style:: Stylesheet } ;
40- use crate :: snippet;
40+ use crate :: renderer:: stylesheet:: Stylesheet ;
4141
4242/// List of lines to be displayed.
4343pub struct DisplayList < ' a > {
4444 pub body : Vec < DisplayLine < ' a > > ,
45- pub stylesheet : Box < dyn Stylesheet > ,
45+ pub stylesheet : Stylesheet ,
4646 pub anonymized_line_numbers : bool ,
4747 pub margin : Option < Margin > ,
4848}
@@ -52,7 +52,7 @@ impl<'a> From<Vec<DisplayLine<'a>>> for DisplayList<'a> {
5252 Self {
5353 body,
5454 anonymized_line_numbers : false ,
55- stylesheet : get_term_style ( false ) ,
55+ stylesheet : Stylesheet :: default ( ) ,
5656 margin : None ,
5757 }
5858 }
@@ -109,7 +109,7 @@ impl<'a> Display for DisplayList<'a> {
109109
110110impl < ' a > From < snippet:: Snippet < ' a > > for DisplayList < ' a > {
111111 fn from ( snippet : snippet:: Snippet < ' a > ) -> DisplayList < ' a > {
112- Self :: new ( snippet, false , false , None )
112+ Self :: new ( snippet, Stylesheet :: default ( ) , false , None )
113113 }
114114}
115115
@@ -127,7 +127,7 @@ impl<'a> DisplayList<'a> {
127127 footer,
128128 slices,
129129 } : snippet:: Snippet < ' a > ,
130- color : bool ,
130+ stylesheet : Stylesheet ,
131131 anonymized_line_numbers : bool ,
132132 margin : Option < Margin > ,
133133 ) -> DisplayList < ' a > {
@@ -151,7 +151,7 @@ impl<'a> DisplayList<'a> {
151151
152152 Self {
153153 body,
154- stylesheet : get_term_style ( color ) ,
154+ stylesheet,
155155 anonymized_line_numbers,
156156 margin,
157157 }
@@ -183,28 +183,28 @@ impl<'a> DisplayList<'a> {
183183 }
184184 }
185185
186- fn get_annotation_style ( & self , annotation_type : & DisplayAnnotationType ) -> Box < dyn Style > {
187- self . stylesheet . get_style ( match annotation_type {
188- DisplayAnnotationType :: Error => StyleClass :: Error ,
189- DisplayAnnotationType :: Warning => StyleClass :: Warning ,
190- DisplayAnnotationType :: Info => StyleClass :: Info ,
191- DisplayAnnotationType :: Note => StyleClass :: Note ,
192- DisplayAnnotationType :: Help => StyleClass :: Help ,
193- DisplayAnnotationType :: None => StyleClass :: None ,
194- } )
186+ fn get_annotation_style ( & self , annotation_type : & DisplayAnnotationType ) -> & Style {
187+ match annotation_type {
188+ DisplayAnnotationType :: Error => self . stylesheet . error ( ) ,
189+ DisplayAnnotationType :: Warning => self . stylesheet . warning ( ) ,
190+ DisplayAnnotationType :: Info => self . stylesheet . info ( ) ,
191+ DisplayAnnotationType :: Note => self . stylesheet . note ( ) ,
192+ DisplayAnnotationType :: Help => self . stylesheet . help ( ) ,
193+ DisplayAnnotationType :: None => self . stylesheet . none ( ) ,
194+ }
195195 }
196196
197197 fn format_label (
198198 & self ,
199199 label : & [ DisplayTextFragment < ' _ > ] ,
200200 f : & mut fmt:: Formatter < ' _ > ,
201201 ) -> fmt:: Result {
202- let emphasis_style = self . stylesheet . get_style ( StyleClass :: Emphasis ) ;
202+ let emphasis_style = self . stylesheet . emphasis ( ) ;
203203
204204 for fragment in label {
205205 match fragment. style {
206206 DisplayTextStyle :: Regular => fragment. content . fmt ( f) ?,
207- DisplayTextStyle :: Emphasis => emphasis_style. paint ( fragment. content , f) ?,
207+ DisplayTextStyle :: Emphasis => emphasis_style. paint ( fragment. content ) . fmt ( f) ?,
208208 }
209209 }
210210 Ok ( ( ) )
@@ -231,27 +231,26 @@ impl<'a> DisplayList<'a> {
231231 if formatted_len == 0 {
232232 self . format_label ( & annotation. label , f)
233233 } else {
234- color. paint_fn (
235- Box :: new ( |f| {
234+ color
235+ . paint_fn ( Box :: new ( |f : & mut fmt :: Formatter < ' _ > | {
236236 Self :: format_annotation_type ( & annotation. annotation_type , f) ?;
237237 if let Some ( id) = & annotation. id {
238238 f. write_char ( '[' ) ?;
239239 f. write_str ( id) ?;
240240 f. write_char ( ']' ) ?;
241241 }
242242 Ok ( ( ) )
243- } ) ,
244- f ,
245- ) ? ;
243+ } ) )
244+ . fmt ( f ) ? ;
245+
246246 if !is_annotation_empty ( annotation) {
247247 if in_source {
248- color. paint_fn (
249- Box :: new ( |f| {
248+ color
249+ . paint_fn ( Box :: new ( |f : & mut fmt :: Formatter < ' _ > | {
250250 f. write_str ( ": " ) ?;
251251 self . format_label ( & annotation. label , f)
252- } ) ,
253- f,
254- ) ?;
252+ } ) )
253+ . fmt ( f) ?;
255254 } else {
256255 f. write_str ( ": " ) ?;
257256 self . format_label ( & annotation. label , f) ?;
@@ -362,27 +361,25 @@ impl<'a> DisplayList<'a> {
362361 _ => range. 0 ,
363362 } ;
364363
365- color. paint_fn (
366- Box :: new ( |f| {
364+ color
365+ . paint_fn ( |f| {
367366 format_repeat_char ( indent_char, indent_length + 1 , f) ?;
368367 format_repeat_char ( mark, range. 1 - indent_length, f)
369- } ) ,
370- f,
371- ) ?;
368+ } )
369+ . fmt ( f) ?;
372370
373371 if !is_annotation_empty ( annotation) {
374372 f. write_char ( ' ' ) ?;
375- color. paint_fn (
376- Box :: new ( |f| {
373+ color
374+ . paint_fn ( |f| {
377375 self . format_annotation (
378376 annotation,
379377 annotation_part == & DisplayAnnotationPart :: LabelContinuation ,
380378 true ,
381379 f,
382380 )
383- } ) ,
384- f,
385- ) ?;
381+ } )
382+ . fmt ( f) ?;
386383 }
387384
388385 Ok ( ( ) )
@@ -407,11 +404,11 @@ impl<'a> DisplayList<'a> {
407404 DisplayHeaderType :: Initial => "-->" ,
408405 DisplayHeaderType :: Continuation => ":::" ,
409406 } ;
410- let lineno_color = self . stylesheet . get_style ( StyleClass :: LineNo ) ;
407+ let lineno_color = self . stylesheet . line_no ( ) ;
411408
412409 if let Some ( ( col, row) ) = pos {
413410 format_repeat_char ( ' ' , lineno_width, f) ?;
414- lineno_color. paint ( header_sigil, f) ?;
411+ lineno_color. paint ( header_sigil) . fmt ( f) ?;
415412 f. write_char ( ' ' ) ?;
416413 path. fmt ( f) ?;
417414 f. write_char ( ':' ) ?;
@@ -420,7 +417,7 @@ impl<'a> DisplayList<'a> {
420417 row. fmt ( f)
421418 } else {
422419 format_repeat_char ( ' ' , lineno_width, f) ?;
423- lineno_color. paint ( header_sigil, f) ?;
420+ lineno_color. paint ( header_sigil) . fmt ( f) ?;
424421 f. write_char ( ' ' ) ?;
425422 path. fmt ( f)
426423 }
@@ -434,10 +431,10 @@ impl<'a> DisplayList<'a> {
434431 if * continuation {
435432 format_repeat_char ( ' ' , lineno_width + 3 , f) ?;
436433 } else {
437- let lineno_color = self . stylesheet . get_style ( StyleClass :: LineNo ) ;
434+ let lineno_color = self . stylesheet . line_no ( ) ;
438435 format_repeat_char ( ' ' , lineno_width, f) ?;
439436 f. write_char ( ' ' ) ?;
440- lineno_color. paint ( "=" , f) ?;
437+ lineno_color. paint ( "=" ) . fmt ( f) ?;
441438 f. write_char ( ' ' ) ?;
442439 }
443440 }
@@ -460,26 +457,24 @@ impl<'a> DisplayList<'a> {
460457 inline_marks,
461458 line,
462459 } => {
463- let lineno_color = self . stylesheet . get_style ( StyleClass :: LineNo ) ;
460+ let lineno_color = self . stylesheet . line_no ( ) ;
464461 if self . anonymized_line_numbers && lineno. is_some ( ) {
465- lineno_color. paint_fn (
466- Box :: new ( |f| {
462+ lineno_color
463+ . paint_fn ( Box :: new ( |f : & mut fmt :: Formatter < ' _ > | {
467464 f. write_str ( Self :: ANONYMIZED_LINE_NUM ) ?;
468465 f. write_str ( " |" )
469- } ) ,
470- f,
471- ) ?;
466+ } ) )
467+ . fmt ( f) ?;
472468 } else {
473- lineno_color. paint_fn (
474- Box :: new ( |f| {
469+ lineno_color
470+ . paint_fn ( Box :: new ( |f : & mut fmt :: Formatter < ' _ > | {
475471 match lineno {
476472 Some ( n) => write ! ( f, "{:>width$}" , n, width = lineno_width) ,
477473 None => format_repeat_char ( ' ' , lineno_width, f) ,
478474 } ?;
479475 f. write_str ( " |" )
480- } ) ,
481- f,
482- ) ?;
476+ } ) )
477+ . fmt ( f) ?;
483478 }
484479 if * line != DisplaySourceLine :: Empty {
485480 if !inline_marks. is_empty ( ) || 0 < inline_marks_width {
@@ -513,15 +508,14 @@ impl<'a> DisplayList<'a> {
513508 ) -> fmt:: Result {
514509 format_repeat_char ( ' ' , inline_marks_width - inline_marks. len ( ) , f) ?;
515510 for mark in inline_marks {
516- self . get_annotation_style ( & mark. annotation_type ) . paint_fn (
517- Box :: new ( |f| {
511+ self . get_annotation_style ( & mark. annotation_type )
512+ . paint_fn ( Box :: new ( |f : & mut fmt :: Formatter < ' _ > | {
518513 f. write_char ( match mark. mark_type {
519514 DisplayMarkType :: AnnotationThrough => '|' ,
520515 DisplayMarkType :: AnnotationStart => '/' ,
521516 } )
522- } ) ,
523- f,
524- ) ?;
517+ } ) )
518+ . fmt ( f) ?;
525519 }
526520 Ok ( ( ) )
527521 }
0 commit comments