mordant icon indicating copy to clipboard operation
mordant copied to clipboard

Nested color style is ignored if it applies to the first character of the string to be rendered

Open pedorro opened this issue 1 year ago • 2 comments

The following code block:

object Lezduit {
    @JvmStatic
    fun main(args: Array<String>) {
        val t = Terminal()
        val style = (bold + black + strikethrough)
        t.println(
            cyan("You ${(green on white)("can ${style("nest")} styles")} arbitrarily")
        )
        t.println(
            cyan("${red("Unless")} it's the first char in the string")
        )
    }
}

produces the following output: image

The first output line is copied directly from the docs. The second line demonstrates the issue. Note the the word 'Unless' is supposed to be red. But the red() text color directive is ignored since it is applied immediately after applying the cyan() color. Adding a space (or any other character) inside the string, in front of the red() color directive causes the word 'Unless' to be rendered in red.

The use-case for this is that we have standard 'error()', 'info()', & 'debug()' functions for our CLI, that apply a particular color style (e.g. 'error()' is red). Occasionally we'd like to apply a different color to a particular piece of the output. This works unless it is the first character of the output.


System: Ubuntu 22.04 Terminal: Terminator 2.1.1 & GNOME Terminal 3.44.0

pedorro avatar Dec 09 '24 19:12 pedorro

Nesting rules are complicated, and I'm sure there are some use cases where the current behavior is preferred. I agree that your example is valid though. In the mean time, you can work around this by joining the parts separately red("warning ") + cyan("message")

ajalt avatar Dec 09 '24 20:12 ajalt

sounds good - thx for the awesome CLI libs BTW. Both Clikt & Mordant kick a lot of ass :grin:

pedorro avatar Dec 10 '24 15:12 pedorro