Printing In Colors

Hi Everyone
I’m trying to print and output in colors,how do I do it in kotlin??
let say we want to print(“Hello World”) and it has to be in red.

1 Like

I don’t think there is an official way of doing it. But you can try to use ANSI escape codes to tell your console that a text should be colored (most terminals support it, including IntelliJs terminal & output).

// Everything after this is in red
val red = "\u001b[31m"

// Resets previous color codes
val reset = "\u001b[0m"

println(red + "Hello World!" + reset)

Pretty sure there are also 3rd party libraries that allow you to use colors.

See also: ANSI EscapeCode

1 Like

At least two that I’m aware of:

2 Likes

Thanks a lot man!it worked!
so as I see I have to know that specific color’s code to have an output on that one.
Really appreciate your help!

It would be nice if Kotlin has some simple extension functions to print text with colors.