|
| 1 | +# Textus Coloris |
| 2 | + |
| 3 | +Simple "library" for doing colourised console output. |
| 4 | + |
| 5 | +# API |
| 6 | + |
| 7 | +There are five functions |
| 8 | + |
| 9 | +```C |
| 10 | +void tc_set_colors(const struct tc_coloris *colors); |
| 11 | +``` |
| 12 | +
|
| 13 | +This is to set the colour map. |
| 14 | +
|
| 15 | +```C |
| 16 | +int tc_print(FILE *fp, const char *fmt, ...); |
| 17 | +int tc_printv(FILE *fp, const char *fmt, va_list args); |
| 18 | +``` |
| 19 | + |
| 20 | +These print a colourised output to the given output stream. These are |
| 21 | +analogous to the fprintf(3) & vfprintf(3) function. |
| 22 | + |
| 23 | +```C |
| 24 | +char *tc_cstring(const char *fmt, ...); |
| 25 | +char *tc_cstringv(const char *fmt, va_list args); |
| 26 | +``` |
| 27 | +
|
| 28 | +These return a pointer to a colourised string. You should free(2) this |
| 29 | +pointer, NULL will be returned on error. These are sort of analogous to the |
| 30 | +asprintf(3) & vasprintf(3) functions. |
| 31 | +
|
| 32 | +# Use |
| 33 | +
|
| 34 | +Seeing as this is really a bit too simple to make into an actual _DSO_, it is |
| 35 | +instead presented as a header-only library. |
| 36 | +
|
| 37 | +This is contained under _header-only/_. |
| 38 | +
|
| 39 | +There is a simple test program to show basic usage. Essentially just copy |
| 40 | +_textus\_coloris.h_ into your project and |
| 41 | +
|
| 42 | +```C |
| 43 | +#define TEXTUS_COLORIS_IMPL |
| 44 | +#include "textus_coloris.h" |
| 45 | +``` |
| 46 | + |
| 47 | +in **one** of your .c files. If you want to use these functions from any other |
| 48 | +source files then just do |
| 49 | + |
| 50 | +```C |
| 51 | +#include "textus_coloris.h" |
| 52 | +``` |
| 53 | + |
| 54 | +in them. |
| 55 | + |
| 56 | +Define a colour map |
| 57 | + |
| 58 | +```C |
| 59 | +static const struct tc_coloris colors[] = { |
| 60 | + { "RED", "\e[38;5;160m" }, |
| 61 | + { "GREEN", "\e[38;5;40m" }, |
| 62 | + { "BLUE", "\e[38;5;75m" }, |
| 63 | + |
| 64 | + { "BOLD", "\e[1m" }, |
| 65 | + { "RST", "\e[0m" }, |
| 66 | + |
| 67 | + {} |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +then set it with |
| 72 | + |
| 73 | +```C |
| 74 | +tc_set_colors(colors); |
| 75 | +``` |
| 76 | +
|
| 77 | +Then you can do stuff like |
| 78 | +
|
| 79 | +```C |
| 80 | +tc_print(stdout, "Hello World! #BOLD##GREEN#%s#RST#\n", "Hello World!"); |
| 81 | +``` |
| 82 | + |
| 83 | +## split-out |
| 84 | + |
| 85 | +There is also a version with the core code split out into a _.c_ file. You |
| 86 | +can just copy _textus\_coloris.[ch]_ into your project and build the .c file |
| 87 | +as you do the rest. Then it's like the above but you don't |
| 88 | +_#define TEXTUS\_COLORIS\_IMPL_ |
| 89 | + |
| 90 | +Otherwise the functionality is the same. |
| 91 | + |
| 92 | +## Examples |
| 93 | + |
| 94 | +There are examples of usage under _header-only/_ & _split-out/_ |
| 95 | + |
| 96 | +# NO\_COLOR |
| 97 | + |
| 98 | +This obeys the [NO\_COLOR](https://no-color.org/) environment variable. |
| 99 | + |
| 100 | +# Thread safety |
| 101 | + |
| 102 | +This should be thread safe, the colour map pointer is stored in thread local |
| 103 | +storage so you should be able to set per thread colour maps. |
| 104 | + |
| 105 | +# License |
| 106 | + |
| 107 | +This licensed under under the MIT license. |
| 108 | + |
| 109 | +See *LICENSE* in the repository root for details. |
| 110 | + |
| 111 | +# Contributing |
| 112 | + |
| 113 | +See *CodingStyle.md* & *Contributing.md* |
0 commit comments