@@ -124,7 +124,6 @@ Currently, `termcolor` does not provide anything to do this for you.
124
124
// #[cfg(doctest)]
125
125
// doctest!("../README.md");
126
126
127
- use std:: env;
128
127
use std:: error;
129
128
use std:: fmt;
130
129
use std:: io:: { self , Write } ;
@@ -221,72 +220,26 @@ pub enum ColorChoice {
221
220
222
221
impl ColorChoice {
223
222
/// Returns true if we should attempt to write colored output.
224
- fn should_attempt_color ( & self ) -> bool {
223
+ fn should_attempt_color ( & self , stream : concolor_control :: Stream ) -> bool {
225
224
match * self {
226
225
ColorChoice :: Always => true ,
227
226
ColorChoice :: AlwaysAnsi => true ,
228
227
ColorChoice :: Never => false ,
229
- ColorChoice :: Auto => self . env_allows_color ( ) ,
228
+ ColorChoice :: Auto => concolor_control :: get ( stream ) . color ( ) ,
230
229
}
231
230
}
232
231
233
- #[ cfg( not( windows) ) ]
234
- fn env_allows_color ( & self ) -> bool {
235
- match env:: var_os ( "TERM" ) {
236
- // If TERM isn't set, then we are in a weird environment that
237
- // probably doesn't support colors.
238
- None => return false ,
239
- Some ( k) => {
240
- if k == "dumb" {
241
- return false ;
242
- }
243
- }
244
- }
245
- // If TERM != dumb, then the only way we don't allow colors at this
246
- // point is if NO_COLOR is set.
247
- if env:: var_os ( "NO_COLOR" ) . is_some ( ) {
248
- return false ;
249
- }
250
- true
251
- }
252
-
253
- #[ cfg( windows) ]
254
- fn env_allows_color ( & self ) -> bool {
255
- // On Windows, if TERM isn't set, then we shouldn't automatically
256
- // assume that colors aren't allowed. This is unlike Unix environments
257
- // where TERM is more rigorously set.
258
- if let Some ( k) = env:: var_os ( "TERM" ) {
259
- if k == "dumb" {
260
- return false ;
261
- }
262
- }
263
- // If TERM != dumb, then the only way we don't allow colors at this
264
- // point is if NO_COLOR is set.
265
- if env:: var_os ( "NO_COLOR" ) . is_some ( ) {
266
- return false ;
267
- }
268
- true
269
- }
270
-
271
232
/// Returns true if this choice should forcefully use ANSI color codes.
272
233
///
273
234
/// It's possible that ANSI is still the correct choice even if this
274
235
/// returns false.
275
236
#[ cfg( windows) ]
276
- fn should_ansi ( & self ) -> bool {
237
+ fn should_ansi ( & self , stream : concolor_control :: Stream ) -> bool {
277
238
match * self {
278
239
ColorChoice :: Always => false ,
279
240
ColorChoice :: AlwaysAnsi => true ,
280
241
ColorChoice :: Never => false ,
281
- ColorChoice :: Auto => {
282
- match env:: var ( "TERM" ) {
283
- Err ( _) => false ,
284
- // cygwin doesn't seem to support ANSI escape sequences
285
- // and instead has its own variety. However, the Windows
286
- // console API may be available.
287
- Ok ( k) => k != "dumb" && k != "cygwin" ,
288
- }
289
- }
242
+ ColorChoice :: Auto => concolor_control:: get ( stream) . ansi_color ( ) ,
290
243
}
291
244
}
292
245
}
@@ -295,13 +248,29 @@ impl ColorChoice {
295
248
/// separate types, which makes it difficult to abstract over them. We use
296
249
/// some simple internal enum types to work around this.
297
250
251
+ #[ derive( Copy , Clone ) ]
298
252
enum StandardStreamType {
299
253
Stdout ,
300
254
Stderr ,
301
255
StdoutBuffered ,
302
256
StderrBuffered ,
303
257
}
304
258
259
+ impl StandardStreamType {
260
+ fn to_concolor ( self ) -> concolor_control:: Stream {
261
+ match self {
262
+ StandardStreamType :: Stdout => concolor_control:: Stream :: Stdout ,
263
+ StandardStreamType :: Stderr => concolor_control:: Stream :: Stderr ,
264
+ StandardStreamType :: StdoutBuffered => {
265
+ concolor_control:: Stream :: Stdout
266
+ }
267
+ StandardStreamType :: StderrBuffered => {
268
+ concolor_control:: Stream :: Stderr
269
+ }
270
+ }
271
+ }
272
+ }
273
+
305
274
enum IoStandardStream {
306
275
Stdout ( io:: Stdout ) ,
307
276
Stderr ( io:: Stderr ) ,
@@ -558,7 +527,7 @@ impl WriterInner<IoStandardStream> {
558
527
sty : StandardStreamType ,
559
528
choice : ColorChoice ,
560
529
) -> WriterInner < IoStandardStream > {
561
- if choice. should_attempt_color ( ) {
530
+ if choice. should_attempt_color ( sty . to_concolor ( ) ) {
562
531
WriterInner :: Ansi ( Ansi ( IoStandardStream :: new ( sty) ) )
563
532
} else {
564
533
WriterInner :: NoColor ( NoColor ( IoStandardStream :: new ( sty) ) )
@@ -581,12 +550,8 @@ impl WriterInner<IoStandardStream> {
581
550
StandardStreamType :: StdoutBuffered => wincon:: Console :: stdout ( ) ,
582
551
StandardStreamType :: StderrBuffered => wincon:: Console :: stderr ( ) ,
583
552
} ;
584
- let is_console_virtual = con
585
- . as_mut ( )
586
- . map ( |con| con. set_virtual_terminal_processing ( true ) . is_ok ( ) )
587
- . unwrap_or ( false ) ;
588
- if choice. should_attempt_color ( ) {
589
- if choice. should_ansi ( ) || is_console_virtual {
553
+ if choice. should_attempt_color ( sty. to_concolor ( ) ) {
554
+ if choice. should_ansi ( sty. to_concolor ( ) ) {
590
555
WriterInner :: Ansi ( Ansi ( IoStandardStream :: new ( sty) ) )
591
556
} else if let Ok ( console) = con {
592
557
WriterInner :: Windows {
@@ -1033,7 +998,7 @@ impl Buffer {
1033
998
/// Create a new buffer with the given color settings.
1034
999
#[ cfg( not( windows) ) ]
1035
1000
fn new ( choice : ColorChoice ) -> Buffer {
1036
- if choice. should_attempt_color ( ) {
1001
+ if choice. should_attempt_color ( concolor_control :: Stream :: Either ) {
1037
1002
Buffer :: ansi ( )
1038
1003
} else {
1039
1004
Buffer :: no_color ( )
@@ -1049,8 +1014,9 @@ impl Buffer {
1049
1014
/// sequences are used instead.
1050
1015
#[ cfg( windows) ]
1051
1016
fn new ( choice : ColorChoice , console : bool ) -> Buffer {
1052
- if choice. should_attempt_color ( ) {
1053
- if !console || choice. should_ansi ( ) {
1017
+ if choice. should_attempt_color ( concolor_control:: Stream :: Either ) {
1018
+ if !console || choice. should_ansi ( concolor_control:: Stream :: Either )
1019
+ {
1054
1020
Buffer :: ansi ( )
1055
1021
} else {
1056
1022
Buffer :: console ( )
0 commit comments