ajalt/mordant

Not working with gradle output?

dkim19375 opened this issue · 2 comments

Regular ANSI color codes work fine, but not ones from Mordant:

import com.github.ajalt.mordant.rendering.TextColors.red
import com.github.ajalt.mordant.terminal.Terminal

fun main() {
    val t = Terminal()
    t.println(red("This isn't red!"))
    println("\u001B[31mThis is red\u001B[0m")
}

gradle run output:
image

However, java -jar BuiltJar.jar works fine

I also have tried to put org.gradle.console=rich in my gradle.properties, but it doesn't seem to do anything

ajalt commented

By default, Mordant disables colors and animations when stdout is redirected so that it doesn't write ANSI codes to files. Since Gradle captures the output, it causes colors to be disabled.

If you want, you can force colors with Terminal(AnsiLevel.TRUECOLOR, interactive=true).

What I do is avoid the run task, and instead use installDist then run the binary directly. Here's an example of that

Cross-posting from here, I'm now solving this on a Gradle task level:

tasks.named<JavaExec>("run") {
    System.getenv("TERM")?.also {
        val mode = it.substringAfter('-', "16color")
        environment("FORCE_COLOR" to mode)
    }

    System.getenv("COLORTERM")?.also {
        environment("FORCE_COLOR" to it)
    }
}