ColrC (kuh·lr·see, feels like heresy) is a C library for terminal colors/escape-codes on linux.
There is also a command-line tool (colr tool) based on ColrC.
It is designed to be flexible and easy to use. Colors can be specified using defined names (RED
, BLUE
, etc.), 256-colors (ext(36)
), RGB colors (rgb(0, 0, 55)
), hex colors (hex(s)
, hex("#ff0000")
), or known names ("aliceblue"
). These colors can be used with fore()
and back()
to set the foreground/background colors (fore(RED)
, back(WHITE)
). Styles are specified with their defined names (style(BRIGHT)
).
Strings can be joined, replaced, colorized, and justified using a few functions/macros. fore()
, back()
, and style()
are mostly optional and position doesn't matter.
Ownership in ColrC is easy to remember. Strings (char*
) are yours, everything else belongs to ColrC. If you create a ColrC object with one of the Colr*
macros to use inside of the colr*
macros (notice the casing), it will be released. The resulting strings that are returned from the colr*
macros will not be released. You must free()
those.
If you use colr_print
or colr_puts
you won't have to manage the resulting string either.
You must include colr.h and compile colr.c along with your program.
There are plenty of examples in the documentation, and on this page.
ColrC uses a couple glibc features, which may not be compatible with your system. Most linux distros are compatible.
The colr.h header defines _GNU_SOURCE
if it's not already defined (see man feature_test_macros
).
Be sure to include libm (the math library) when compiling:
The only two files that are needed to use ColrC are colr.h and colr.c.
Name | Description |
---|---|
colr.h | The interface to ColrC. |
colr.c | Where ColrC is implemented. This must be compiled/linked with your program. |
You can also create a shared library (libcolr.so
) for your system. Clone the repo and run the make target:
If you link the library (and libm
), you will only need to include the header (colr.h
):
There are several make
targets to help you build and install the library:
If you don't want to use a debian package, you can run the included installer. The installer is interactive, and will let you choose where to install the library based on GCC's library search path. It will not overwrite existing files without confirmation:
For a full listing see the docs, but here are the main features in ColrC:
Name | Purpose |
---|---|
colr | Generates a colorized string. |
Colr | Generates a colorized ColorText, for use with other functions/macros. |
colr_cat | Concatenates strings and ColrC objects into a string. |
Colr_cat | Concatenates strings and ColrC objects into a ColorResult, for use with other functions/macros. |
colr_join | Generates a string by joining strings/ColrC-objects by another string/ColrC-object. |
Colr_join | Generates a ColorResult by joining strings/ColrC-objects by another string/ColrC-object, for use with other functions/macros. |
When an allocated ColorArg/ColorText/ColorResult is used inside of a Colr/colr call it is automatically released. Strings produced by a Colr/colr call are managed by the user (you must free()
them).
I've included an example that showcases some of these:
For all examples, check the documentation. Here is a table of the most common usage examples:
There are examples for all of the main features in ColrC, and tools (like the snippet runner) you can play with if you clone the repo.
ColrC is the C
version of Colr (a python library) and it's less-flexible cousin Colr.sh. The programming styles vary because C
doesn't allow easy method chaining, and instead leans towards nested function calls.
This is an attempt to create a flexible and easy version for C
.
In the future there may be a shared library or a python extension based on ColrC, but for now I'm finishing out the basic features and testing.