From 2cbb73a9c1ee3e16557aa40759ea6d8eec8abcfb Mon Sep 17 00:00:00 2001 From: jcamiel Date: Mon, 8 Aug 2022 14:04:13 +0200 Subject: [PATCH] Support NO_COLOR env variable (https://no-color.org) --- packages/hurl/src/cli/options.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/hurl/src/cli/options.rs b/packages/hurl/src/cli/options.rs index e98f818d12b..791e2413764 100644 --- a/packages/hurl/src/cli/options.rs +++ b/packages/hurl/src/cli/options.rs @@ -17,6 +17,7 @@ */ use std::collections::HashMap; +use std::env; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::{Path, PathBuf}; @@ -440,14 +441,20 @@ pub fn parse_options(matches: &ArgMatches) -> Result { }) } +/// Returns true if Hurl output uses ANSI code and false otherwise. pub fn output_color(matches: &ArgMatches) -> bool { if has_flag(matches, "color") { - true - } else if has_flag(matches, "no_color") { - false - } else { - atty::is(Stream::Stdout) + return true; + } + if has_flag(matches, "no_color") { + return false; + } + if let Ok(v) = env::var("NO_COLOR") { + if !v.is_empty() { + return false; + } } + atty::is(Stream::Stdout) } fn to_entry(matches: &ArgMatches) -> Result, CliError> {