@@ -16,6 +16,9 @@ use std::iter;
16
16
use std:: path:: Path ;
17
17
use std:: sync:: Arc ;
18
18
19
+ use anstream:: ColorChoice ;
20
+ use anstream:: stream:: { AsLockedWrite , RawStream } ;
21
+ use anstyle:: { AnsiColor , Effects } ;
19
22
use derive_setters:: Setters ;
20
23
use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
21
24
use rustc_data_structures:: sync:: { DynSend , IntoDynSyncSend } ;
@@ -25,7 +28,6 @@ use rustc_lint_defs::pluralize;
25
28
use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
26
29
use rustc_span:: source_map:: SourceMap ;
27
30
use rustc_span:: { FileLines , FileName , SourceFile , Span , char_width, str_width} ;
28
- use termcolor:: { Buffer , BufferWriter , Color , ColorChoice , ColorSpec , StandardStream , WriteColor } ;
29
31
use tracing:: { debug, instrument, trace, warn} ;
30
32
31
33
use crate :: registry:: Registry ;
@@ -1701,7 +1703,6 @@ impl HumanEmitter {
1701
1703
} else {
1702
1704
col_sep_before_no_show_source = true ;
1703
1705
}
1704
-
1705
1706
// print out the span location and spacer before we print the annotated source
1706
1707
// to do this, we need to know if this span will be primary
1707
1708
let is_primary = primary_lo. file . name == annotated_file. file . name ;
@@ -3127,7 +3128,6 @@ impl FileWithAnnotatedLines {
3127
3128
multiline_depth : 0 ,
3128
3129
} ) ;
3129
3130
}
3130
-
3131
3131
let mut output = vec ! [ ] ;
3132
3132
let mut multiline_annotations = vec ! [ ] ;
3133
3133
@@ -3361,7 +3361,7 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
3361
3361
( '\u{2069}' , "�" ) ,
3362
3362
] ;
3363
3363
3364
- fn normalize_whitespace ( s : & str ) -> String {
3364
+ pub ( crate ) fn normalize_whitespace ( s : & str ) -> String {
3365
3365
const {
3366
3366
let mut i = 1 ;
3367
3367
while i < OUTPUT_REPLACEMENTS . len ( ) {
@@ -3406,7 +3406,26 @@ fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
3406
3406
)
3407
3407
}
3408
3408
3409
- fn emit_to_destination (
3409
+ pub trait WriteColor : io:: Write {
3410
+ fn supports_color ( & self ) -> bool ;
3411
+ }
3412
+
3413
+ impl < S : RawStream + AsLockedWrite > WriteColor for anstream:: AutoStream < S > {
3414
+ fn supports_color ( & self ) -> bool {
3415
+ match self . current_choice ( ) {
3416
+ ColorChoice :: Always | ColorChoice :: AlwaysAnsi | ColorChoice :: Auto => true ,
3417
+ ColorChoice :: Never => false ,
3418
+ }
3419
+ }
3420
+ }
3421
+
3422
+ impl WriteColor for std:: io:: Sink {
3423
+ fn supports_color ( & self ) -> bool {
3424
+ false
3425
+ }
3426
+ }
3427
+
3428
+ pub ( crate ) fn emit_to_destination (
3410
3429
rendered_buffer : & [ Vec < StyledString > ] ,
3411
3430
lvl : & Level ,
3412
3431
dst : & mut Destination ,
@@ -3429,10 +3448,8 @@ fn emit_to_destination(
3429
3448
let _buffer_lock = lock:: acquire_global_lock ( "rustc_errors" ) ;
3430
3449
for ( pos, line) in rendered_buffer. iter ( ) . enumerate ( ) {
3431
3450
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 ( ) ?;
3451
+ let style = part. style . anstyle ( * lvl) ;
3452
+ write ! ( dst, "{style}{}{style:#}" , part. text) ?;
3436
3453
}
3437
3454
if !short_message && ( !lvl. is_failure_note ( ) || pos != rendered_buffer. len ( ) - 1 ) {
3438
3455
writeln ! ( dst) ?;
@@ -3445,8 +3462,8 @@ fn emit_to_destination(
3445
3462
pub type Destination = Box < dyn WriteColor + Send > ;
3446
3463
3447
3464
struct Buffy {
3448
- buffer_writer : BufferWriter ,
3449
- buffer : Buffer ,
3465
+ buffer_writer : anstream :: Stderr ,
3466
+ buffer : Vec < u8 > ,
3450
3467
}
3451
3468
3452
3469
impl Write for Buffy {
@@ -3455,7 +3472,7 @@ impl Write for Buffy {
3455
3472
}
3456
3473
3457
3474
fn flush ( & mut self ) -> io:: Result < ( ) > {
3458
- self . buffer_writer . print ( & self . buffer ) ?;
3475
+ self . buffer_writer . write_all ( & self . buffer ) ?;
3459
3476
self . buffer . clear ( ) ;
3460
3477
Ok ( ( ) )
3461
3478
}
@@ -3472,15 +3489,11 @@ impl Drop for Buffy {
3472
3489
3473
3490
impl WriteColor for Buffy {
3474
3491
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 ( )
3492
+ self . buffer_writer . current_choice ( ) ;
3493
+ match self . buffer_writer . current_choice ( ) {
3494
+ ColorChoice :: Always | ColorChoice :: AlwaysAnsi | ColorChoice :: Auto => true ,
3495
+ ColorChoice :: Never => false ,
3496
+ }
3484
3497
}
3485
3498
}
3486
3499
@@ -3492,61 +3505,44 @@ pub fn stderr_destination(color: ColorConfig) -> Destination {
3492
3505
//
3493
3506
// On non-Windows we rely on the atomicity of `write` to ensure errors
3494
3507
// don't get all jumbled up.
3508
+ let buffer_writer = anstream:: Stderr :: new ( std:: io:: stderr ( ) , choice) ;
3495
3509
if cfg ! ( windows) {
3496
- Box :: new ( StandardStream :: stderr ( choice ) )
3510
+ Box :: new ( buffer_writer )
3497
3511
} else {
3498
- let buffer_writer = BufferWriter :: stderr ( choice) ;
3499
- let buffer = buffer_writer. buffer ( ) ;
3512
+ let buffer = Vec :: new ( ) ;
3500
3513
Box :: new ( Buffy { buffer_writer, buffer } )
3501
3514
}
3502
3515
}
3503
3516
3504
3517
/// On Windows, BRIGHT_BLUE is hard to read on black. Use cyan instead.
3505
3518
///
3506
3519
/// See #36178.
3507
- const BRIGHT_BLUE : Color = if cfg ! ( windows) { Color :: Cyan } else { Color :: Blue } ;
3520
+ const BRIGHT_BLUE : anstyle:: Style = if cfg ! ( windows) {
3521
+ AnsiColor :: BrightCyan . on_default ( )
3522
+ } else {
3523
+ AnsiColor :: BrightBlue . on_default ( )
3524
+ } ;
3508
3525
3509
3526
impl Style {
3510
- fn color_spec ( & self , lvl : Level ) -> ColorSpec {
3511
- let mut spec = ColorSpec :: new ( ) ;
3527
+ pub ( crate ) fn anstyle ( & self , lvl : Level ) -> anstyle:: Style {
3512
3528
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 ) ) ;
3529
+ Style :: Addition => AnsiColor :: BrightGreen . on_default ( ) ,
3530
+ Style :: Removal => AnsiColor :: BrightRed . on_default ( ) ,
3531
+ Style :: LineAndColumn => anstyle:: Style :: new ( ) ,
3532
+ Style :: LineNumber => BRIGHT_BLUE . effects ( Effects :: BOLD ) ,
3533
+ Style :: Quotation => anstyle:: Style :: new ( ) ,
3534
+ Style :: MainHeaderMsg => if cfg ! ( windows) {
3535
+ AnsiColor :: BrightWhite . on_default ( )
3536
+ } else {
3537
+ anstyle:: Style :: new ( )
3547
3538
}
3539
+ . effects ( Effects :: BOLD ) ,
3540
+ Style :: UnderlinePrimary | Style :: LabelPrimary => lvl. color ( ) . effects ( Effects :: BOLD ) ,
3541
+ Style :: UnderlineSecondary | Style :: LabelSecondary => BRIGHT_BLUE . effects ( Effects :: BOLD ) ,
3542
+ Style :: HeaderMsg | Style :: NoStyle => anstyle:: Style :: new ( ) ,
3543
+ Style :: Level ( lvl) => lvl. color ( ) . effects ( Effects :: BOLD ) ,
3544
+ Style :: Highlight => AnsiColor :: Magenta . on_default ( ) . effects ( Effects :: BOLD ) ,
3548
3545
}
3549
- spec
3550
3546
}
3551
3547
}
3552
3548
0 commit comments