ajalt / mordant

Multiplatform text styling for Kotlin command-line applications
https://ajalt.github.io/mordant/
Apache License 2.0
957 stars 34 forks source link

Not working with gradle output? #104

Closed dkim19375 closed 1 year ago

dkim19375 commented 1 year ago

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 1 year ago

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

sschuberth commented 6 months ago

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)
    }
}