-
Notifications
You must be signed in to change notification settings - Fork 3
02 ANSI escape codes
Spells started as a tiny util library, which was concentrating on ANSI escape codes, which is why they are often used and treated as first class citizens. In fact, the bigger part of spells provides utilities for the terminal. Spells is thus rather useful at devtime instead of in production.
The core abstraction is called AnsiStyle
, defined in the AnsiModule. Every String can be converted into an AnsiStyle
like this:
scala> "style".toAnsiStyle
res0: spells.AnsiModule#AnsiStyle = stylesample
Notice how spells attempts to provide a sample of the given style.
scala> "\u001b[34m".toAnsiStyle
res0: spells.AnsiModule#AnsiStyle = Blue // printed in blue
The toAnsiStyle
factory understands human readable styles (in capital camel case) as well, like:
scala> "Red".toAnsiStyle
res0: spells.AnsiModule#AnsiStyle = Red // printed in red
scala> "Untouched".toAnsiStyle
res1: spells.AnsiModule#AnsiStyle = sample // printed in regular terminal color
The list of all ANSI escape codes can be found here. Spells provides a few codes out of the box:
scala> AnsiStyle.Green
res0: spells.AnsiModule#AnsiStyle = Green // printed in green
scala> AnsiStyle.Random
res1: spells.AnsiModule#AnsiStyle = Yellow // printed in yellow
scala> AnsiStyle.All // each style is printed in its respective color
res2: scala.Vector[spells.AnsiModule#AnsiStyle] = Vector(Black, Blue, ..., White, Yellow)
The defaults can be found in the reference.conf file and the Configuration wiki page shows how to override as well as retrieve them programmatically.