Colorized output for the Rego print function!
Support "all" the ANSI color codes by their name:
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
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.
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.
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.
print(colorize("foo bar baz", {"fg": "cyan", "bg": "red", "reset": false}]))
print("this too!")
print(reset(""))
Return the provided string with a different foreground color used for each letter of the string.
print(rainbow("Double rainbows!!"))
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.
- 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.