Define three functions, respectively used for: * printing a new color marker before text to colorize * printing end color marker * wrapping the two previous functions into a substitute to ds_put_format(), with colors
Signed-off-by: Quentin Monnet <quentin.mon...@6wind.com> --- lib/dynamic-string.c | 32 ++++++++++++++++++++++++++++++++ lib/dynamic-string.h | 7 +++++++ 2 files changed, 39 insertions(+) diff --git a/lib/dynamic-string.c b/lib/dynamic-string.c index a6c8f6c763e1..93f0f92987db 100644 --- a/lib/dynamic-string.c +++ b/lib/dynamic-string.c @@ -453,3 +453,35 @@ ds_chomp(struct ds *ds, int c) ds->string[--ds->length] = '\0'; } } + +/* Colors: start a colorized text attribute on stdout using the SGR_START + * format; the attribute is specified by SGR_SEQ. */ +void +ds_put_color_start(struct ds* string, char const *sgr_seq, + int const color_option) +{ + if (!color_option || !sgr_seq) { + return; + } + ds_put_format(string, sgr_start, sgr_seq); +} + +/* Restore the normal text attribute using the SGR_END string. */ +void +ds_put_color_end(struct ds* string, int const color_option) +{ + if (!color_option) { + return; + } + ds_put_format(string, "%s", sgr_end); +} + +/* Wrapper: enclose string s between SGR_SEQ and SGR_END. */ +void +ds_put_color(struct ds* string, char const *s, char const *sgr_seq, + int const color_option) +{ + ds_put_color_start(string, sgr_seq, color_option); + ds_put_cstr(string, s); + ds_put_color_end(string, color_option); +} diff --git a/lib/dynamic-string.h b/lib/dynamic-string.h index f1e0a368af32..2f928a5c8f86 100644 --- a/lib/dynamic-string.h +++ b/lib/dynamic-string.h @@ -23,6 +23,7 @@ #include <stdint.h> #include <stdio.h> #include <time.h> +#include "colors.h" #include "compiler.h" /* A "dynamic string", that is, a buffer that can be used to construct a @@ -74,6 +75,12 @@ void ds_swap(struct ds *, struct ds *); int ds_last(const struct ds *); void ds_chomp(struct ds *, int c); +void ds_put_color_start(struct ds* string, char const *sgr_seq, + int const color_option); +void ds_put_color_end(struct ds* string, + int const color_option); +void ds_put_color(struct ds* string, char const *s, char const *sgr_seq, + int const color_option); /* Inline functions. */ -- 1.9.1 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev