Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor the NO_COLOR environment variable. #703

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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