@@ -16,6 +16,8 @@ use std::iter;
16
16
use std:: path:: Path ;
17
17
use std:: sync:: Arc ;
18
18
19
+ use anstream:: { AutoStream , ColorChoice } ;
20
+ use anstyle:: { AnsiColor , Effects } ;
19
21
use derive_setters:: Setters ;
20
22
use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
21
23
use rustc_data_structures:: sync:: { DynSend , IntoDynSyncSend } ;
@@ -25,7 +27,6 @@ use rustc_lint_defs::pluralize;
25
27
use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
26
28
use rustc_span:: source_map:: SourceMap ;
27
29
use rustc_span:: { FileLines , FileName , SourceFile , Span , char_width, str_width} ;
28
- use termcolor:: { Buffer , BufferWriter , Color , ColorChoice , ColorSpec , StandardStream , WriteColor } ;
29
30
use tracing:: { debug, instrument, trace, warn} ;
30
31
31
32
use crate :: registry:: Registry ;
@@ -525,10 +526,6 @@ impl Emitter for HumanEmitter {
525
526
!self . short_message
526
527
}
527
528
528
- fn supports_color ( & self ) -> bool {
529
- self . dst . supports_color ( )
530
- }
531
-
532
529
fn translator ( & self ) -> & Translator {
533
530
& self . translator
534
531
}
@@ -1701,7 +1698,6 @@ impl HumanEmitter {
1701
1698
} else {
1702
1699
col_sep_before_no_show_source = true ;
1703
1700
}
1704
-
1705
1701
// print out the span location and spacer before we print the annotated source
1706
1702
// to do this, we need to know if this span will be primary
1707
1703
let is_primary = primary_lo. file . name == annotated_file. file . name ;
@@ -3127,7 +3123,6 @@ impl FileWithAnnotatedLines {
3127
3123
multiline_depth : 0 ,
3128
3124
} ) ;
3129
3125
}
3130
-
3131
3126
let mut output = vec ! [ ] ;
3132
3127
let mut multiline_annotations = vec ! [ ] ;
3133
3128
@@ -3361,7 +3356,7 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
3361
3356
( '\u{2069}' , "�" ) ,
3362
3357
] ;
3363
3358
3364
- fn normalize_whitespace ( s : & str ) -> String {
3359
+ pub ( crate ) fn normalize_whitespace ( s : & str ) -> String {
3365
3360
const {
3366
3361
let mut i = 1 ;
3367
3362
while i < OUTPUT_REPLACEMENTS . len ( ) {
@@ -3406,7 +3401,7 @@ fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
3406
3401
)
3407
3402
}
3408
3403
3409
- fn emit_to_destination (
3404
+ pub ( crate ) fn emit_to_destination (
3410
3405
rendered_buffer : & [ Vec < StyledString > ] ,
3411
3406
lvl : & Level ,
3412
3407
dst : & mut Destination ,
@@ -3429,10 +3424,8 @@ fn emit_to_destination(
3429
3424
let _buffer_lock = lock:: acquire_global_lock ( "rustc_errors" ) ;
3430
3425
for ( pos, line) in rendered_buffer. iter ( ) . enumerate ( ) {
3431
3426
for part in line {
3432
- let style = part. style . color_spec ( * lvl) ;
3433
- dst. set_color ( & style) ?;
3434
- write ! ( dst, "{}" , part. text) ?;
3435
- dst. reset ( ) ?;
3427
+ let style = part. style . anstyle ( * lvl) ;
3428
+ write ! ( dst, "{style}{}{style:#}" , part. text) ?;
3436
3429
}
3437
3430
if !short_message && ( !lvl. is_failure_note ( ) || pos != rendered_buffer. len ( ) - 1 ) {
3438
3431
writeln ! ( dst) ?;
@@ -3442,11 +3435,11 @@ fn emit_to_destination(
3442
3435
Ok ( ( ) )
3443
3436
}
3444
3437
3445
- pub type Destination = Box < dyn WriteColor + Send > ;
3438
+ pub type Destination = AutoStream < Box < dyn Write + Send > > ;
3446
3439
3447
3440
struct Buffy {
3448
- buffer_writer : BufferWriter ,
3449
- buffer : Buffer ,
3441
+ buffer_writer : std :: io :: Stderr ,
3442
+ buffer : Vec < u8 > ,
3450
3443
}
3451
3444
3452
3445
impl Write for Buffy {
@@ -3455,7 +3448,7 @@ impl Write for Buffy {
3455
3448
}
3456
3449
3457
3450
fn flush ( & mut self ) -> io:: Result < ( ) > {
3458
- self . buffer_writer . print ( & self . buffer ) ?;
3451
+ self . buffer_writer . write_all ( & self . buffer ) ?;
3459
3452
self . buffer . clear ( ) ;
3460
3453
Ok ( ( ) )
3461
3454
}
@@ -3470,83 +3463,59 @@ impl Drop for Buffy {
3470
3463
}
3471
3464
}
3472
3465
3473
- impl WriteColor for Buffy {
3474
- fn supports_color ( & self ) -> bool {
3475
- self . buffer . supports_color ( )
3476
- }
3477
-
3478
- fn set_color ( & mut self , spec : & ColorSpec ) -> io:: Result < ( ) > {
3479
- self . buffer . set_color ( spec)
3480
- }
3481
-
3482
- fn reset ( & mut self ) -> io:: Result < ( ) > {
3483
- self . buffer . reset ( )
3484
- }
3485
- }
3486
-
3487
3466
pub fn stderr_destination ( color : ColorConfig ) -> Destination {
3467
+ let buffer_writer = std:: io:: stderr ( ) ;
3488
3468
let choice = color. to_color_choice ( ) ;
3469
+ // We need to resolve `ColorChoice::Auto` before `Box`ing since
3470
+ // `ColorChoice::Auto` on `dyn Write` will always resolve to `Never`
3471
+ let choice = if matches ! ( choice, ColorChoice :: Auto ) {
3472
+ AutoStream :: choice ( & buffer_writer)
3473
+ } else {
3474
+ choice
3475
+ } ;
3489
3476
// On Windows we'll be performing global synchronization on the entire
3490
3477
// system for emitting rustc errors, so there's no need to buffer
3491
3478
// anything.
3492
3479
//
3493
3480
// On non-Windows we rely on the atomicity of `write` to ensure errors
3494
3481
// don't get all jumbled up.
3495
3482
if cfg ! ( windows) {
3496
- Box :: new ( StandardStream :: stderr ( choice ) )
3483
+ AutoStream :: new ( Box :: new ( buffer_writer ) , choice )
3497
3484
} else {
3498
- let buffer_writer = BufferWriter :: stderr ( choice) ;
3499
- let buffer = buffer_writer. buffer ( ) ;
3500
- Box :: new ( Buffy { buffer_writer, buffer } )
3485
+ let buffer = Vec :: new ( ) ;
3486
+ AutoStream :: new ( Box :: new ( Buffy { buffer_writer, buffer } ) , choice)
3501
3487
}
3502
3488
}
3503
3489
3504
3490
/// On Windows, BRIGHT_BLUE is hard to read on black. Use cyan instead.
3505
3491
///
3506
3492
/// See #36178.
3507
- const BRIGHT_BLUE : Color = if cfg ! ( windows) { Color :: Cyan } else { Color :: Blue } ;
3493
+ const BRIGHT_BLUE : anstyle:: Style = if cfg ! ( windows) {
3494
+ AnsiColor :: BrightCyan . on_default ( )
3495
+ } else {
3496
+ AnsiColor :: BrightBlue . on_default ( )
3497
+ } ;
3508
3498
3509
3499
impl Style {
3510
- fn color_spec ( & self , lvl : Level ) -> ColorSpec {
3511
- let mut spec = ColorSpec :: new ( ) ;
3500
+ pub ( crate ) fn anstyle ( & self , lvl : Level ) -> anstyle:: Style {
3512
3501
match self {
3513
- Style :: Addition => {
3514
- spec. set_fg ( Some ( Color :: Green ) ) . set_intense ( true ) ;
3515
- }
3516
- Style :: Removal => {
3517
- spec. set_fg ( Some ( Color :: Red ) ) . set_intense ( true ) ;
3518
- }
3519
- Style :: LineAndColumn => { }
3520
- Style :: LineNumber => {
3521
- spec. set_bold ( true ) ;
3522
- spec. set_intense ( true ) ;
3523
- spec. set_fg ( Some ( BRIGHT_BLUE ) ) ;
3524
- }
3525
- Style :: Quotation => { }
3526
- Style :: MainHeaderMsg => {
3527
- spec. set_bold ( true ) ;
3528
- if cfg ! ( windows) {
3529
- spec. set_intense ( true ) . set_fg ( Some ( Color :: White ) ) ;
3530
- }
3531
- }
3532
- Style :: UnderlinePrimary | Style :: LabelPrimary => {
3533
- spec = lvl. color ( ) ;
3534
- spec. set_bold ( true ) ;
3535
- }
3536
- Style :: UnderlineSecondary | Style :: LabelSecondary => {
3537
- spec. set_bold ( true ) . set_intense ( true ) ;
3538
- spec. set_fg ( Some ( BRIGHT_BLUE ) ) ;
3539
- }
3540
- Style :: HeaderMsg | Style :: NoStyle => { }
3541
- Style :: Level ( lvl) => {
3542
- spec = lvl. color ( ) ;
3543
- spec. set_bold ( true ) ;
3544
- }
3545
- Style :: Highlight => {
3546
- spec. set_bold ( true ) . set_fg ( Some ( Color :: Magenta ) ) ;
3502
+ Style :: Addition => AnsiColor :: BrightGreen . on_default ( ) ,
3503
+ Style :: Removal => AnsiColor :: BrightRed . on_default ( ) ,
3504
+ Style :: LineAndColumn => anstyle:: Style :: new ( ) ,
3505
+ Style :: LineNumber => BRIGHT_BLUE . effects ( Effects :: BOLD ) ,
3506
+ Style :: Quotation => anstyle:: Style :: new ( ) ,
3507
+ Style :: MainHeaderMsg => if cfg ! ( windows) {
3508
+ AnsiColor :: BrightWhite . on_default ( )
3509
+ } else {
3510
+ anstyle:: Style :: new ( )
3547
3511
}
3512
+ . effects ( Effects :: BOLD ) ,
3513
+ Style :: UnderlinePrimary | Style :: LabelPrimary => lvl. color ( ) . effects ( Effects :: BOLD ) ,
3514
+ Style :: UnderlineSecondary | Style :: LabelSecondary => BRIGHT_BLUE . effects ( Effects :: BOLD ) ,
3515
+ Style :: HeaderMsg | Style :: NoStyle => anstyle:: Style :: new ( ) ,
3516
+ Style :: Level ( lvl) => lvl. color ( ) . effects ( Effects :: BOLD ) ,
3517
+ Style :: Highlight => AnsiColor :: Magenta . on_default ( ) . effects ( Effects :: BOLD ) ,
3548
3518
}
3549
- spec
3550
3519
}
3551
3520
}
3552
3521
0 commit comments