https://github.com/anderseknert/colorized
Colorized output for the Rego print function!
https://github.com/anderseknert/colorized
colorize colors opa open-policy-agent print rego
Last synced: about 1 month ago
JSON representation
Colorized output for the Rego print function!
- Host: GitHub
- URL: https://github.com/anderseknert/colorized
- Owner: anderseknert
- License: apache-2.0
- Created: 2022-03-28T10:28:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-28T12:28:55.000Z (about 3 years ago)
- Last Synced: 2025-03-14T20:50:41.602Z (about 1 month ago)
- Topics: colorize, colors, opa, open-policy-agent, print, rego
- Language: Open Policy Agent
- Homepage:
- Size: 31.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-opa - Colorized - Colorized output for the OPA print function! (Fun and Quirky / Testing Blogs and Articles)
README
# colorized
Colorized output for the Rego
[print](https://blog.openpolicyagent.org/introducing-the-opa-print-function-809da6a13aee)
function!## Colors
Support "all" the [ANSI color codes](https://talyian.github.io/ansicolors/) 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
```rego
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
```rego
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
```rego
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
```rego
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.