@@ -116,6 +116,8 @@ public class FormatString{
116116 private static bool hasConsoleBeenPrepared = false ;
117117 #endif
118118
119+ private static bool usesColors = true ;
120+
119121 public bool addFinalReset = true ;
120122
121123 public string content { get {
@@ -145,6 +147,13 @@ public int length{get{
145147
146148 private bool flagToBuild ;
147149
150+ static FormatString ( ) {
151+ string noColor = Environment . GetEnvironmentVariable ( "NO_COLOR" ) ;
152+ if ( noColor != null ) {
153+ usesColors = false ;
154+ }
155+ }
156+
148157 public FormatString ( ) {
149158 privateContent = new List < char > ( ) ;
150159 format = new List < CharFormat ? > ( ) ;
@@ -157,6 +166,9 @@ public FormatString(string s) : this(){
157166
158167 #if WINDOWS
159168 private static void PrepareConsole ( ) {
169+ if ( ! usesColors ) {
170+ return ;
171+ }
160172 var iStdOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
161173 var _ = GetConsoleMode ( iStdOut , out var outConsoleMode )
162174 && SetConsoleMode ( iStdOut , outConsoleMode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING ) ;
@@ -263,7 +275,7 @@ private void Build(){
263275 }
264276 }
265277
266- if ( lastFormat == null || ( current . foreground != lastFormat . foreground && current . foreground != null ) || current . foregroundReset == true ) {
278+ if ( ( lastFormat == null || ( current . foreground != lastFormat . foreground && current . foreground != null ) || current . foregroundReset == true ) && usesColors ) {
267279 if ( current . foregroundReset == true ) {
268280 formatChanges = true ;
269281 changes . Add ( "39" ) ;
@@ -278,7 +290,7 @@ private void Build(){
278290 }
279291 }
280292
281- if ( lastFormat == null || ( current . background != lastFormat . background && current . background != null ) || current . backgroundReset == true ) {
293+ if ( ( lastFormat == null || ( current . background != lastFormat . background && current . background != null ) || current . backgroundReset == true ) && usesColors ) {
282294 if ( current . backgroundReset == true ) {
283295 formatChanges = true ;
284296 changes . Add ( "49" ) ;
0 commit comments