Have you ever wondered how some command line programs add artistically horrid colors to textual output? Ever wonder why you might see an ls listing showing directories in yellow or soft-linked files in red on some machine terminals and not others. I love colors as much as anyone else does, so I’m curious too.
Well, back in the days before we had a style markup language for designers we had programmers design a style markup language for… programmers. Some OS’s may vary but basically the magic seeps from an environment variable called LSCOLORS and the ls command line flag --color or another environment variable called CLICOLOR.
Back in those arcane days we didn’t have any of these fancy pants style sheets so the best we could come up with was the following approach.
First, define a set of finite codes that indicate which ANSI colors are available.
Then define an indexing for types of file listings based foreground+background pairings
The result of which being something like
export LSCOLORS=dxfxcxdxbxegedabagacad
Pretty obvious and easy to read huh?
a Scala crayon boxThe other day I was playing around with Scala’s REPL and came across something new. By now I should hope we all know about Scala’s Predef object. Where else do you think all of those implicit conversions are coming from? Ohio? Referenced inside Predef is one neat little class that’s easy to look right past: Console. Console provides us with some wildly fun library methods like println and readLine. What fun right?
So what’s Console got to do with color? Everything.
One of the newer features of Scala’s REPL is method tab completion. I see some irb users in the back yawning so I’ll get to my point. One of the useful side effects of having tab completion is api discovery. Guess what I get if I crack open a Scala REPL and type
scala> Console.<tab>
BLACK BLACK_B BLINK BLUE BLUE_B BOLD CYAN
CYAN_B GREEN GREEN_B INVISIBLE MAGENTA MAGENTA_B RED
RED_B RESET REVERSED UNDERLINED WHITE WHITE_B YELLOW
YELLOW_B asInstanceOf err flush in isInstanceOf out
print printf println readBoolean readByte readChar readDouble
readFloat readInt readLine readLong readShort readf readf1
readf2 readf3 setErr setIn setOut toString withErr
withIn withOut
Hrm… What’s this?
What are all these values named after ANSI colors doing in Console’s pantry?
What you will find is that Console has provided functions values for colorizing… your console’s output. What can we do with this? Colorize your CLI’s interface and make it all pretty like. The convention is Console.{COLOR} for setting foreground colors and Console.{COLOR}_B for setting background colors. Pretty sweet.
So in your REPL you can type
scala> Console.MAGENTA_B
Which will turn your terminals background to a nice shade of magenta.
Then add a tasteful green foreground with
scala> Console.GREEN
You should see some thing like
scala> “colors” :: “are” :: “awesome” :: Nil mkString(” “)
But wait there’s more. There’s BLINK! Your remember <blink></blink> don’t you?
So does Scala
scala> Console.BLINK
and behold
scala> “colors” :: “are” :: “awesome” :: Nil mkString(” “)
Don’t worry. You can always revert these changes to console output with Console.RESET.
Optionally, if you are feeling like a coding ninja I’d recommend Console.INVISIBLE
These are the things I live for.