@@ -262,22 +262,26 @@ impl RunContext {
262262 . collect ( )
263263 }
264264
265- /// Returns `Some(true)` if ANSI escape codes are explicitly enabled, `Some(false)` if ANSI escape
266- /// codes are explicitly disabled, `None` otherwise. .
267- pub fn use_color_env_var ( & self ) -> Option < bool > {
268- // According to the NO_COLOR spec, any presence of the variable should disable color, but to
269- // maintain backward compatibility with code < 7.1.0, we check that the NO_COLOR env is at
270- // least not empty.
265+ /// Returns `Some(true)` if color is set through env, `Some(false)` if color is disable through env,
266+ /// `None` otherwise.
267+ pub fn color_env_var ( & self ) -> Option < bool > {
268+ self . get_env_var_bool ( HURL_COLOR )
269+ }
270+
271+ /// Returns `Some(true)` if no color is set through env, `Some(false)` if no color is disable through env,
272+ /// `None` otherwise.
273+ pub fn no_color_env_var ( & self ) -> Option < bool > {
271274 if let Some ( v) = self . env_vars . get ( "NO_COLOR" ) {
275+ // According to the NO_COLOR spec, any presence of the variable should disable color, but to
276+ // maintain backward compatibility with code < 7.1.0, we check that the NO_COLOR env is at
277+ // least not empty.
272278 if !v. is_empty ( ) {
273- Some ( false )
279+ Some ( true )
274280 } else {
275281 None
276282 }
277- } else if let Some ( v) = self . get_env_var_bool ( HURL_NO_COLOR ) {
278- Some ( !v)
279283 } else {
280- self . get_env_var_bool ( HURL_COLOR )
284+ self . get_env_var_bool ( HURL_NO_COLOR )
281285 }
282286 }
283287
@@ -367,28 +371,16 @@ mod tests {
367371
368372 let env_vars = HashMap :: from ( [ ( "A" . to_string ( ) , "B" . to_string ( ) ) ] ) ;
369373 let ctx = RunContext :: new ( env_vars, stdin_term, stdout_term, stderr_term) ;
370- assert ! ( ctx. use_color_env_var ( ) . is_none( ) ) ;
374+ assert ! ( ctx. color_env_var ( ) . is_none( ) ) ;
371375 }
372376
373377 #[ test]
374- fn context_has_env_var_color ( ) {
378+ fn context_has_color_env_var ( ) {
375379 let stdin_term = true ;
376380 let stdout_term = true ;
377381 let stderr_term = true ;
378382
379383 let data = [
380- ( "NO_COLOR" , "0" , Some ( false ) ) ,
381- ( "NO_COLOR" , "1" , Some ( false ) ) ,
382- ( "NO_COLOR" , "true" , Some ( false ) ) ,
383- ( "NO_COLOR" , "TRUE" , Some ( false ) ) ,
384- ( "NO_COLOR" , "false" , Some ( false ) ) ,
385- ( "NO_COLOR" , "FALSE" , Some ( false ) ) ,
386- ( "HURL_NO_COLOR" , "0" , Some ( true ) ) ,
387- ( "HURL_NO_COLOR" , "1" , Some ( false ) ) ,
388- ( "HURL_NO_COLOR" , "true" , Some ( false ) ) ,
389- ( "HURL_NO_COLOR" , "TRUE" , Some ( false ) ) ,
390- ( "HURL_NO_COLOR" , "false" , Some ( true ) ) ,
391- ( "HURL_NO_COLOR" , "FALSE" , Some ( true ) ) ,
392384 ( "HURL_COLOR" , "0" , Some ( false ) ) ,
393385 ( "HURL_COLOR" , "1" , Some ( true ) ) ,
394386 ( "HURL_COLOR" , "true" , Some ( true ) ) ,
@@ -401,7 +393,41 @@ mod tests {
401393 let env_vars = HashMap :: from ( [ ( name. to_string ( ) , value. to_string ( ) ) ] ) ;
402394 let ctx = RunContext :: new ( env_vars, stdin_term, stdout_term, stderr_term) ;
403395 assert_eq ! (
404- ctx. use_color_env_var( ) ,
396+ ctx. color_env_var( ) ,
397+ expected,
398+ "test env var {}={}" ,
399+ name,
400+ value
401+ ) ;
402+ }
403+ }
404+
405+ #[ test]
406+ fn context_has_no_color_env_var ( ) {
407+ let stdin_term = true ;
408+ let stdout_term = true ;
409+ let stderr_term = true ;
410+
411+ let data = [
412+ ( "NO_COLOR" , "0" , Some ( true ) ) ,
413+ ( "NO_COLOR" , "1" , Some ( true ) ) ,
414+ ( "NO_COLOR" , "true" , Some ( true ) ) ,
415+ ( "NO_COLOR" , "TRUE" , Some ( true ) ) ,
416+ ( "NO_COLOR" , "false" , Some ( true ) ) ,
417+ ( "NO_COLOR" , "FALSE" , Some ( true ) ) ,
418+ ( "HURL_NO_COLOR" , "0" , Some ( false ) ) ,
419+ ( "HURL_NO_COLOR" , "1" , Some ( true ) ) ,
420+ ( "HURL_NO_COLOR" , "true" , Some ( true ) ) ,
421+ ( "HURL_NO_COLOR" , "TRUE" , Some ( true ) ) ,
422+ ( "HURL_NO_COLOR" , "false" , Some ( false ) ) ,
423+ ( "HURL_NO_COLOR" , "FALSE" , Some ( false ) ) ,
424+ ] ;
425+
426+ for ( name, value, expected) in data {
427+ let env_vars = HashMap :: from ( [ ( name. to_string ( ) , value. to_string ( ) ) ] ) ;
428+ let ctx = RunContext :: new ( env_vars, stdin_term, stdout_term, stderr_term) ;
429+ assert_eq ! (
430+ ctx. no_color_env_var( ) ,
405431 expected,
406432 "test env var {}={}" ,
407433 name,
0 commit comments