Skip to content

anderseknert/colorized

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

colorized

Colorized output for the Rego print function!

Colors

Support "all" the ANSI color codes by their name:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • 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"))

fg_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"]))

fg_y_bg_b


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(""))

advanced

rainbow(str)

Return the provided string with a different foreground color used for each letter of the string.

Example

print(rainbow("Double rainbows!!"))

rainbow

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.