11pub mod styles;
22
3+ #[ cfg( feature ="ansi_term" ) ]
4+ use crate :: renderers:: ascii_default:: styles:: color:: Style ;
5+ #[ cfg( feature ="termcolor" ) ]
6+ use crate :: renderers:: ascii_default:: styles:: color2:: Style ;
7+ #[ cfg( all( not( feature ="ansi_term" ) , not( feature ="termcolor" ) ) ) ]
8+ use crate :: renderers:: ascii_default:: styles:: plain:: Style ;
9+
310use super :: Renderer as RendererTrait ;
411use crate :: annotation:: AnnotationType ;
5- use crate :: display_list:: annotation:: Annotation ;
612use crate :: display_list:: line:: DisplayLine ;
713use crate :: display_list:: line:: DisplayMark ;
814use crate :: display_list:: line:: DisplayMarkType ;
@@ -13,6 +19,7 @@ use std::cmp;
1319use std:: io:: Write ;
1420use std:: marker:: PhantomData ;
1521use styles:: Style as StyleTrait ;
22+ use styles:: StyleType ;
1623
1724fn digits ( n : usize ) ->usize {
1825let mut n = n;
@@ -28,6 +35,10 @@ pub struct Renderer<S: StyleTrait> {
2835style : PhantomData < S > ,
2936}
3037
38+ pub fn get_renderer ( ) ->impl RendererTrait {
39+ Renderer :: < Style > :: new ( )
40+ }
41+
3142impl < S : StyleTrait > Renderer < S > {
3243pub fn new ( ) ->Self {
3344Renderer { style : PhantomData }
@@ -70,11 +81,15 @@ impl<S: StyleTrait> Renderer<S> {
7081 line,
7182} =>{
7283if let Some ( lineno) = lineno{
73- write ! ( w, "{:>1$}" , lineno, lineno_max) ?;
84+ S :: fmt (
85+ w,
86+ format_args ! ( "{:>1$}" , lineno, lineno_max) ,
87+ & [ StyleType :: LineNo , StyleType :: Bold ] ,
88+ ) ?;
7489} else {
7590write ! ( w, "{:>1$}" , "" , lineno_max) ?;
7691}
77- write ! ( w, " | " ) ?;
92+ S :: fmt ( w, " | " , & [ StyleType :: LineNo , StyleType :: Bold ] ) ?;
7893write ! ( w, "{:>1$}" , "" , inline_marks_width - inline_marks. len( ) ) ?;
7994for markin inline_marks{
8095self . fmt_display_mark ( w, mark) ?;
@@ -93,18 +108,17 @@ impl<S: StyleTrait> Renderer<S> {
93108) -> std:: io:: Result < ( ) > {
94109match line{
95110DisplaySourceLine :: Content { text} =>write ! ( w, " {}" , text) ,
96- DisplaySourceLine :: Annotation {
97- annotation,
98- range : ( start, end) ,
99- } =>{
100- let indent =if start ==& 0 { 0 } else { start +1 } ;
111+ DisplaySourceLine :: Annotation { annotation, range} =>{
112+ let ( _, style) =self . get_annotation_type_style ( & annotation. annotation_type ) ;
113+ let styles =[ StyleType :: Bold , style] ;
114+ let indent =if range. start ==0 { 0 } else { range. start +1 } ;
101115write ! ( w, "{:>1$}" , "" , indent) ?;
102- if start ==& 0 {
103- write ! ( w, "{:_>1$}" , "^" , end - start +1 ) ?;
116+ if range . start ==0 {
117+ S :: fmt ( w, format_args ! ( "{:_>1$} " , "^" , range . len ( ) +1 ) , & styles ) ?;
104118} else {
105- write ! ( w, "{:->1$}" , "" , end - start ) ?;
119+ S :: fmt ( w, format_args ! ( "{:->1$} " , "" , range . len ( ) ) , & styles ) ?;
106120}
107- write ! ( w, " {}" , annotation. label)
121+ S :: fmt ( w, annotation. label , & styles )
108122}
109123DisplaySourceLine :: Empty =>Ok ( ( ) ) ,
110124}
@@ -118,35 +132,41 @@ impl<S: StyleTrait> Renderer<S> {
118132) -> std:: io:: Result < ( ) > {
119133match line{
120134DisplayRawLine :: Origin { path, pos} =>{
121- S :: fmt ( w, format_args ! ( "{:>1$}" , "" , lineno_max) ) ?;
122- S :: fmt ( w, format_args ! ( "--> {}" , path) ) ?;
135+ write ! ( w, "{:>1$}" , "" , lineno_max) ?;
136+ S :: fmt ( w, "-->" , & [ StyleType :: Bold , StyleType :: LineNo ] ) ?;
137+ write ! ( w, " {}" , path) ?;
123138if let Some ( line) = pos. 0 {
124- S :: fmt ( w, format_args ! ( ":{}" , line) ) ?;
139+ write ! ( w, ":{}" , line) ?;
125140}
126141write ! ( w, "\n " )
127142}
128143DisplayRawLine :: Annotation { annotation, ..} =>{
129- self . fmt_annotation ( w, annotation) ?;
144+ let ( desc, style) =self . get_annotation_type_style ( & annotation. annotation_type ) ;
145+ let s =[ StyleType :: Bold , style] ;
146+ S :: fmt ( w, desc, & s) ?;
130147if let Some ( id) = annotation. id {
131- write ! ( w, "[{}]" , id) ?;
148+ S :: fmt ( w, format_args ! ( "[{}]" , id) , & s ) ?;
132149}
133- writeln ! ( w, ": {}" , annotation. label)
150+ S :: fmt (
151+ w,
152+ format_args ! ( ": {}\n " , annotation. label) ,
153+ & [ StyleType :: Bold ] ,
154+ )
134155}
135156}
136157}
137158
138- fn fmt_annotation (
159+ fn get_annotation_type_style (
139160& self ,
140- w : & mut impl std:: io:: Write ,
141- annotation : & Annotation ,
142- ) -> std:: io:: Result < ( ) > {
143- match annotation. annotation_type {
144- AnnotationType :: None =>Ok ( ( ) ) ,
145- AnnotationType :: Error =>write ! ( w, "error" ) ,
146- AnnotationType :: Warning =>write ! ( w, "warning" ) ,
147- AnnotationType :: Info =>write ! ( w, "info" ) ,
148- AnnotationType :: Note =>write ! ( w, "note" ) ,
149- AnnotationType :: Help =>write ! ( w, "help" ) ,
161+ annotation_type : & AnnotationType ,
162+ ) ->( & ' static str , StyleType ) {
163+ match annotation_type{
164+ AnnotationType :: Error =>( "error" , StyleType :: Error ) ,
165+ AnnotationType :: Warning =>( "warning" , StyleType :: Warning ) ,
166+ AnnotationType :: Info =>( "info" , StyleType :: Info ) ,
167+ AnnotationType :: Note =>( "note" , StyleType :: Note ) ,
168+ AnnotationType :: Help =>( "help" , StyleType :: Help ) ,
169+ AnnotationType :: None =>( "" , StyleType :: None ) ,
150170}
151171}
152172
@@ -155,10 +175,12 @@ impl<S: StyleTrait> Renderer<S> {
155175w : & mut impl std:: io:: Write ,
156176display_mark : & DisplayMark ,
157177) -> std:: io:: Result < ( ) > {
158- match display_mark. mark_type {
159- DisplayMarkType :: AnnotationStart =>write ! ( w, "/" ) ,
160- DisplayMarkType :: AnnotationThrough =>write ! ( w, "|" ) ,
161- }
178+ let ( _, style) =self . get_annotation_type_style ( & display_mark. annotation_type ) ;
179+ let ch =match display_mark. mark_type {
180+ DisplayMarkType :: AnnotationStart =>'/' ,
181+ DisplayMarkType :: AnnotationThrough =>'|' ,
182+ } ;
183+ S :: fmt ( w, ch, & [ style] )
162184}
163185}
164186