@@ -36,8 +36,8 @@ use std::io::prelude::*;
3636use std:: io:: { self , IsTerminal } ;
3737use std:: iter;
3838use std:: path:: Path ;
39- use termcolor:: { Ansi , BufferWriter , ColorChoice , ColorSpec , StandardStream } ;
40- use termcolor:: { Buffer , Color , WriteColor } ;
39+ use termcolor:: { Ansi , Buffer , BufferWriter , ColorChoice , ColorSpec , StandardStream } ;
40+ use termcolor:: { Color , WriteColor } ;
4141
4242/// Default column width, used in tests and when terminal dimensions cannot be determined.
4343const DEFAULT_COLUMN_WIDTH : usize = 140 ;
@@ -2604,15 +2604,44 @@ fn emit_to_destination(
26042604
26052605pub enum Destination {
26062606 Terminal ( StandardStream ) ,
2607- Buffered ( BufferWriter ) ,
26082607 Raw ( Box < ( dyn WriteColor + Send ) > ) ,
26092608}
26102609
26112610pub enum WritableDst < ' a > {
2612- Buffered ( & ' a mut BufferWriter , Buffer ) ,
26132611 Raw ( & ' a mut ( dyn WriteColor + Send ) ) ,
26142612}
26152613
2614+ struct Buffy {
2615+ buffer_writer : BufferWriter ,
2616+ buffer : Buffer ,
2617+ }
2618+
2619+ impl Write for Buffy {
2620+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
2621+ self . buffer . write ( buf)
2622+ }
2623+
2624+ fn flush ( & mut self ) -> io:: Result < ( ) > {
2625+ self . buffer_writer . print ( & self . buffer ) ?;
2626+ self . buffer . clear ( ) ;
2627+ Ok ( ( ) )
2628+ }
2629+ }
2630+
2631+ impl WriteColor for Buffy {
2632+ fn supports_color ( & self ) -> bool {
2633+ self . buffer . supports_color ( )
2634+ }
2635+
2636+ fn set_color ( & mut self , spec : & ColorSpec ) -> io:: Result < ( ) > {
2637+ self . buffer . set_color ( spec)
2638+ }
2639+
2640+ fn reset ( & mut self ) -> io:: Result < ( ) > {
2641+ self . buffer . reset ( )
2642+ }
2643+ }
2644+
26162645impl Destination {
26172646 fn from_stderr ( color : ColorConfig ) -> Destination {
26182647 let choice = color. to_color_choice ( ) ;
@@ -2625,25 +2654,22 @@ impl Destination {
26252654 if cfg ! ( windows) {
26262655 Terminal ( StandardStream :: stderr ( choice) )
26272656 } else {
2628- Buffered ( BufferWriter :: stderr ( choice) )
2657+ let buffer_writer = BufferWriter :: stderr ( choice) ;
2658+ let buffer = buffer_writer. buffer ( ) ;
2659+ Raw ( Box :: new ( Buffy { buffer_writer, buffer } ) )
26292660 }
26302661 }
26312662
26322663 fn writable ( & mut self ) -> WritableDst < ' _ > {
26332664 match * self {
26342665 Destination :: Terminal ( ref mut t) => WritableDst :: Raw ( t) ,
2635- Destination :: Buffered ( ref mut t) => {
2636- let buf = t. buffer ( ) ;
2637- WritableDst :: Buffered ( t, buf)
2638- }
26392666 Destination :: Raw ( ref mut t) => WritableDst :: Raw ( t) ,
26402667 }
26412668 }
26422669
26432670 fn supports_color ( & self ) -> bool {
26442671 match * self {
26452672 Self :: Terminal ( ref stream) => stream. supports_color ( ) ,
2646- Self :: Buffered ( ref buffer) => buffer. buffer ( ) . supports_color ( ) ,
26472673 Self :: Raw ( ref writer) => writer. supports_color ( ) ,
26482674 }
26492675 }
@@ -2702,14 +2728,12 @@ impl<'a> WritableDst<'a> {
27022728
27032729 fn set_color ( & mut self , color : & ColorSpec ) -> io:: Result < ( ) > {
27042730 match * self {
2705- WritableDst :: Buffered ( _, ref mut t) => t. set_color ( color) ,
27062731 WritableDst :: Raw ( ref mut t) => t. set_color ( color) ,
27072732 }
27082733 }
27092734
27102735 fn reset ( & mut self ) -> io:: Result < ( ) > {
27112736 match * self {
2712- WritableDst :: Buffered ( _, ref mut t) => t. reset ( ) ,
27132737 WritableDst :: Raw ( ref mut t) => t. reset ( ) ,
27142738 }
27152739 }
@@ -2718,24 +2742,20 @@ impl<'a> WritableDst<'a> {
27182742impl < ' a > Write for WritableDst < ' a > {
27192743 fn write ( & mut self , bytes : & [ u8 ] ) -> io:: Result < usize > {
27202744 match * self {
2721- WritableDst :: Buffered ( _, ref mut buf) => buf. write ( bytes) ,
27222745 WritableDst :: Raw ( ref mut w) => w. write ( bytes) ,
27232746 }
27242747 }
27252748
27262749 fn flush ( & mut self ) -> io:: Result < ( ) > {
27272750 match * self {
2728- WritableDst :: Buffered ( _, ref mut buf) => buf. flush ( ) ,
27292751 WritableDst :: Raw ( ref mut w) => w. flush ( ) ,
27302752 }
27312753 }
27322754}
27332755
27342756impl < ' a > Drop for WritableDst < ' a > {
27352757 fn drop ( & mut self ) {
2736- if let WritableDst :: Buffered ( ref mut dst, ref mut buf) = self {
2737- drop ( dst. print ( buf) ) ;
2738- }
2758+ self . flush ( ) . unwrap ( )
27392759 }
27402760}
27412761
0 commit comments