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

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

Open
pedorro opened this issue Dec 9, 2024 · 2 comments

Comments

@pedorro
Copy link

pedorro commented Dec 9, 2024

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

@ajalt
Copy link
Owner

ajalt commented Dec 9, 2024

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

@pedorro
Copy link
Author

pedorro commented Dec 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants