@@ -594,20 +594,35 @@ static void replace(std::string& source, const std::unordered_map<std::string, s
594594 }
595595}
596596
597- static void replaceColors (std::string& source) {
598- // TODO: colors are not applied when either stdout or stderr is not a TTY because we resolve them before the stream usage
599- static const std::unordered_map<std::string, std::string> substitutionMap =
600- {
601- {" {reset}" , ::toString (Color::Reset)},
602- {" {bold}" , ::toString (Color::Bold)},
603- {" {dim}" , ::toString (Color::Dim)},
604- {" {red}" , ::toString (Color::FgRed)},
605- {" {green}" , ::toString (Color::FgGreen)},
606- {" {blue}" , ::toString (Color::FgBlue)},
607- {" {magenta}" , ::toString (Color::FgMagenta)},
608- {" {default}" , ::toString (Color::FgDefault)},
609- };
610- replace (source, substitutionMap);
597+ static void replaceColors (std::string& source, bool enableColors) {
598+ if (enableColors) {
599+ static const std::unordered_map<std::string, std::string> substitutionMap =
600+ {
601+ {" {reset}" , ::toString (Color::Reset)},
602+ {" {bold}" , ::toString (Color::Bold)},
603+ {" {dim}" , ::toString (Color::Dim)},
604+ {" {red}" , ::toString (Color::FgRed)},
605+ {" {green}" , ::toString (Color::FgGreen)},
606+ {" {blue}" , ::toString (Color::FgBlue)},
607+ {" {magenta}" , ::toString (Color::FgMagenta)},
608+ {" {default}" , ::toString (Color::FgDefault)},
609+ };
610+ replace (source, substitutionMap);
611+ }
612+ else {
613+ static const std::unordered_map<std::string, std::string> substitutionMap =
614+ {
615+ {" {reset}" , " " },
616+ {" {bold}" , " " },
617+ {" {dim}" , " " },
618+ {" {red}" , " " },
619+ {" {green}" , " " },
620+ {" {blue}" , " " },
621+ {" {magenta}" , " " },
622+ {" {default}" , " " },
623+ };
624+ replace (source, substitutionMap);
625+ }
611626}
612627
613628std::string ErrorMessage::toString (bool verbose, const std::string &templateFormat, const std::string &templateLocation) const
@@ -913,16 +928,16 @@ std::string replaceStr(std::string s, const std::string &from, const std::string
913928 return s;
914929}
915930
916- void substituteTemplateFormatStatic (std::string& templateFormat)
931+ void substituteTemplateFormatStatic (std::string& templateFormat, bool enableColors )
917932{
918933 replaceSpecialChars (templateFormat);
919- replaceColors (templateFormat);
934+ replaceColors (templateFormat, enableColors );
920935}
921936
922- void substituteTemplateLocationStatic (std::string& templateLocation)
937+ void substituteTemplateLocationStatic (std::string& templateLocation, bool enableColors )
923938{
924939 replaceSpecialChars (templateLocation);
925- replaceColors (templateLocation);
940+ replaceColors (templateLocation, enableColors );
926941}
927942
928943std::string getClassification (const std::string &guideline, ReportType reportType) {
0 commit comments