@@ -318,6 +318,20 @@ enum Destination {
318
318
Raw ( Box < Write + Send > ) ,
319
319
}
320
320
321
+ /// Do not use this for messages that end in `\n` – use `println_maybe_styled` instead. See
322
+ /// `EmitterWriter::print_maybe_styled` for details.
323
+ macro_rules! print_maybe_styled {
324
+ ( $writer: expr, $style: expr, $( $arg: tt) * ) => {
325
+ $writer. print_maybe_styled( format_args!( $( $arg) * ) , $style, false )
326
+ }
327
+ }
328
+
329
+ macro_rules! println_maybe_styled {
330
+ ( $writer: expr, $style: expr, $( $arg: tt) * ) => {
331
+ $writer. print_maybe_styled( format_args!( $( $arg) * ) , $style, true )
332
+ }
333
+ }
334
+
321
335
impl EmitterWriter {
322
336
pub fn stderr ( color_config : ColorConfig ,
323
337
registry : Option < diagnostics:: registry:: Registry > ) -> EmitterWriter {
@@ -346,8 +360,9 @@ impl EmitterWriter {
346
360
}
347
361
348
362
fn print_maybe_styled ( & mut self ,
349
- msg : & str ,
350
- color : term:: attr:: Attr ) -> io:: Result < ( ) > {
363
+ args : fmt:: Arguments ,
364
+ color : term:: attr:: Attr ,
365
+ print_newline_at_end : bool ) -> io:: Result < ( ) > {
351
366
match self . dst {
352
367
Terminal ( ref mut t) => {
353
368
try!( t. attr ( color) ) ;
@@ -364,17 +379,22 @@ impl EmitterWriter {
364
379
// once, which still leaves the opportunity for interleaved output
365
380
// to be miscolored. We assume this is rare enough that we don't
366
381
// have to worry about it.
367
- if msg. ends_with ( "\n " ) {
368
- try!( t. write_all ( msg[ ..msg. len ( ) -1 ] . as_bytes ( ) ) ) ;
369
- try!( t. reset ( ) ) ;
370
- try!( t. write_all ( b"\n " ) ) ;
382
+ try!( t. write_fmt ( args) ) ;
383
+ try!( t. reset ( ) ) ;
384
+ if print_newline_at_end {
385
+ t. write_all ( b"\n " )
386
+ } else {
387
+ Ok ( ( ) )
388
+ }
389
+ }
390
+ Raw ( ref mut w) => {
391
+ try!( w. write_fmt ( args) ) ;
392
+ if print_newline_at_end {
393
+ w. write_all ( b"\n " )
371
394
} else {
372
- try!( t. write_all ( msg. as_bytes ( ) ) ) ;
373
- try!( t. reset ( ) ) ;
395
+ Ok ( ( ) )
374
396
}
375
- Ok ( ( ) )
376
397
}
377
- Raw ( ref mut w) => w. write_all ( msg. as_bytes ( ) ) ,
378
398
}
379
399
}
380
400
@@ -384,15 +404,14 @@ impl EmitterWriter {
384
404
try!( write ! ( & mut self . dst, "{} " , topic) ) ;
385
405
}
386
406
387
- try!( self . print_maybe_styled ( & format ! ( "{}: " , lvl. to_string( ) ) ,
388
- term:: attr:: ForegroundColor ( lvl. color ( ) ) ) ) ;
389
- try!( self . print_maybe_styled ( & format ! ( "{}" , msg) ,
390
- term:: attr:: Bold ) ) ;
407
+ try!( print_maybe_styled ! ( self , term:: attr:: ForegroundColor ( lvl. color( ) ) ,
408
+ "{}: " , lvl. to_string( ) ) ) ;
409
+ try!( print_maybe_styled ! ( self , term:: attr:: Bold , "{}" , msg) ) ;
391
410
392
411
match code {
393
412
Some ( code) => {
394
413
let style = term:: attr:: ForegroundColor ( term:: color:: BRIGHT_MAGENTA ) ;
395
- try!( self . print_maybe_styled ( & format ! ( " [{}]" , code. clone( ) ) , style ) ) ;
414
+ try!( print_maybe_styled ! ( self , style , " [{}]" , code. clone( ) ) ) ;
396
415
}
397
416
None => ( )
398
417
}
@@ -623,8 +642,8 @@ impl EmitterWriter {
623
642
s. pop ( ) ;
624
643
}
625
644
626
- try!( self . print_maybe_styled ( & format ! ( "{} \n " , s ) ,
627
- term :: attr :: ForegroundColor ( lvl . color ( ) ) ) ) ;
645
+ try!( println_maybe_styled ! ( self , term :: attr :: ForegroundColor ( lvl . color ( ) ) ,
646
+ "{}" , s ) ) ;
628
647
}
629
648
}
630
649
Ok ( ( ) )
@@ -696,9 +715,8 @@ impl EmitterWriter {
696
715
}
697
716
}
698
717
s. push ( '^' ) ;
699
- s. push ( '\n' ) ;
700
- self . print_maybe_styled ( & s[ ..] ,
701
- term:: attr:: ForegroundColor ( lvl. color ( ) ) )
718
+ println_maybe_styled ! ( self , term:: attr:: ForegroundColor ( lvl. color( ) ) ,
719
+ "{}" , s)
702
720
}
703
721
704
722
fn print_macro_backtrace ( & mut self ,
0 commit comments