Skip to content

Commit

Permalink
Merge pull request #703 from non/non/nocolor
Browse files Browse the repository at this point in the history
Honor the NO_COLOR environment variable.
  • Loading branch information
valencik committed Sep 22, 2023
2 parents 0b86b0b + 2e50667 commit c9ffef2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ public class Ansi {
private static final String LIGHT_MAGENTA = "\u001B[95m";
private static final String LIGHT_CYAN = "\u001B[96m";

private static final boolean noColor = shouldDisableColor();

private static final boolean shouldDisableColor() {
String noColorEnv = System.getenv("NO_COLOR");
return noColorEnv != null && noColorEnv.length() > 0;
}

public static String c(String s, String colorSequence) {
if (colorSequence == null) return s;
if (colorSequence == null || noColor) return s;
else return colorSequence + s + NORMAL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ final class JUnitReporter(
}
val canHighlight = !PlatformCompat.isNative
new StringBuilder()
.append(AnsiColors.Reset)
.append(AnsiColors.use(AnsiColors.Reset))
.append(
if (!canHighlight) ""
else if (highlight) AnsiColors.Bold
else AnsiColors.DarkGrey
else if (highlight) AnsiColors.use(AnsiColors.Bold)
else AnsiColors.use(AnsiColors.DarkGrey)
)
.append(" at ")
.append(settings.decodeName(e.getClassName + '.' + e.getMethodName))
Expand All @@ -259,7 +259,7 @@ final class JUnitReporter(
}
)
.append(')')
.append(AnsiColors.Reset)
.append(AnsiColors.use(AnsiColors.Reset))
.toString()
}
private def formatTime(elapsedMillis: Double): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ object AnsiColors {
val GREEN = "\u001B[32m"
val DarkGrey = "\u001B[90m"

val noColor: Boolean =
Option(System.getenv("NO_COLOR")).exists(_ != "")

def use(colorSequence: String): String =
if (noColor) "" else colorSequence

def c(s: String, colorSequence: String): String =
if (colorSequence == null) s
if (colorSequence == null || noColor) s
else colorSequence + s + Reset

def filterAnsi(s: String): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class Lines extends Serializable {
.append(format(location.line - 1))
.append(slice(0))
.append('\n')
.append(AnsiColors.Reversed)
.append(AnsiColors.use(AnsiColors.Reversed))
.append(format(location.line))
.append(slice(1))
.append(AnsiColors.Reset)
.append(AnsiColors.use(AnsiColors.Reset))
if (slice.length >= 3)
out
.append('\n')
Expand Down
8 changes: 5 additions & 3 deletions munit/shared/src/main/scala/munit/internal/difflib/Diff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ class Diff(val obtained: String, val expected: String) extends Serializable {

private def appendDiffOnlyReport(sb: StringBuilder): Unit = {
header("Diff", sb)
sb.append(
s" (${AnsiColors.LightRed}- obtained${AnsiColors.Reset}, ${AnsiColors.LightGreen}+ expected${AnsiColors.Reset})"
).append("\n")
val red = AnsiColors.use(AnsiColors.LightRed)
val reset = AnsiColors.use(AnsiColors.Reset)
val green = AnsiColors.use(AnsiColors.LightGreen)
sb.append(s" (${red}- obtained${reset}, ${green}+ expected${reset})")
sb.append("\n")
sb.append(unifiedDiff)
}

Expand Down

0 comments on commit c9ffef2

Please sign in to comment.