Awesome
colorized
Colorized output for the Rego print function!
Colors
Support "all" the ANSI color codes by their name:
- black
- <span style="color:red">red</span>
- <span style="color:green">green</span>
- <span style="color:yellow">yellow</span>
- <span style="color:blue">blue</span>
- <span style="color:magenta">magenta</span>
- <span style="color:cyan">cyan</span>
- white
Functions
colorize(str, settings)
Accepts as the first argument the string to colorize, and as the second argument either string, an array, or an object.
string
- colorize the provided string using the value of argument as the foreground color.
Example
print(colorize("foo bar baz", "red"))
array
- colorize the provided string using the first value of the array as foreground color,
and the second value as the background color.
Example
print(colorize("foo bar baz", ["yellow", "blue"]))
object
- colorize the provided string using the "fg"
attribute of the object as foreground color,
and the "bg"
attribute as the background color. The optional "reset"
boolean attribute
may also be used to disable resetting the colors back to the defaults. This is useful when
e.g. printing multiple lines of output without having to repeat the calls to colorize.
NOTE: If disabled, ensure that the reset(str)
function is called manually, or else any
subsequent output will remain colorized, including the result of evaluation.
Example
print(colorize("foo bar baz", {"fg": "cyan", "bg": "red", "reset": false}]))
print("this too!")
print(reset(""))
rainbow(str)
Return the provided string with a different foreground color used for each letter of the string.
Example
print(rainbow("Double rainbows!!"))
reset(str)
Resets the colors to the default. Only necessary when reset was disabled in the first place, using the
object variant of colorize
. You'll most likely won't need this.
Caveats
- JSON (luckily!) does not support the escape sequences required for colorization, so this works only
for the
print
command - not values returned by OPA itself - This is only for fun. Don't use this for anything serious.