Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using colors in Scala Console

I was hoping that would be as straight forward as

Console.out.println( "Test " + Console.RED + " RED " + Console.RESET )

but nothing happens, instead I see the control characters (e.g. "\033[31m"). I tried both from within sbt (sbt run) and normal IDEA run configuration run...

like image 421
0__ Avatar asked Mar 19 '12 15:03

0__


3 Answers

It works if your console supports color. I just tried it on a linux system via putty and got the expected result.

Edit: If you're looking to do this via windows you can use Ansicon to provide coloring in your standard command prompt.

like image 176
coltfred Avatar answered Nov 13 '22 09:11

coltfred


Yup, the above works just fine. Also I've found that it's pretty neat to wrap those up as methods ".red" on String, like in this library https://github.com/ktoso/scala-rainbow

Lots of Ruby gems out there that do this, but not so much for Scala yet... :-)

like image 6
Konrad 'ktoso' Malawski Avatar answered Nov 13 '22 08:11

Konrad 'ktoso' Malawski


from Scala Standard Library:

import scala.io.AnsiColor._
println(s"${REVERSED}${BOLD}My name ist ${GREEN}Green!${RESET} default font")

works in IntelliJ console too

like image 2
Hartmut Pfarr Avatar answered Nov 13 '22 09:11

Hartmut Pfarr