gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
termcolor.h
Go to the documentation of this file.
1// libtermcolor: termcolor.h
2// Copyright (C) 2021 Jérémie Astor
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16/*
17 This is the header file for libtermcolor. It contains the documentation for
18 using the various functions in the exposed API. The doc folder contains
19 other information. Both places are vital for proper API usage, so reading
20 both places will facilitate adeptitude with libtermcolor.
21*/
22#pragma once
23
24#include <stdbool.h>
25#include <stdio.h>
26//#include "termcolor_export.h"
27
28#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__WIN32) && !defined(__CYGWIN__)) && defined (ENABLE_VIRTUAL_TERMINAL_PROCESSING)
29 #define TERMCOLOR_OS_WIN
30 #include <windows.h>
31#endif
32
33/* A type describing an termcolor error. These are the only values returned by
34 the `tcol_fprintf` and `tcol_printf` functions. You may assume any nonzero
35 value is an error and use `tcol_errorstr` to display a diagnostic. */
44
45/* Decodes the given termcolor representation of a color
46
47 Parameters:
48 - `dst`: The buffer to write the resulting ANSI escape code to
49 - `dstn`: The length of the destination buffer
50 - `len`: A pointer to a value which is initialized to the number of bytes
51 written to `dst`
52 - `rep`: The attributes applied to the text
53 - `foreground`: A foreground color, if any
54 - `background`: A background color, if any
55
56 Return Value:
57 - int: One of the values `enum term_color_error_t`
58
59 NOTE: This is private API exposed for developers. Those interested can read
60 the source (found in src/termcolor.c). The relevant parts are the enum
61 `_termcolor_internal_color` and the function `_termcolor_internal_lookup`.
62*/
63int _tcol_color_generate(char *dst, size_t dstn, size_t *len, int rep,
64 int foreground, int background);
65
66/* Returns a human-readable string describing the given libtermcolor error. */
67const char *tcol_errorstr(const enum term_color_error_t err);
68
69/* Parses a termcolor color string without the brackets.
70
71 Parameters:
72 - `dst`: The buffer to write the resulting ANSI escape code to
73 - `dstn`: The length of the destination buffer
74 - `color`: The buffer that the color is given in
75 - `k`: The length of the given color in the buffer
76 - `len`: A pointer to a value which is initialized to the number of bytes
77 written to `dst`
78
79 Return Value:
80 - `int`: One of the values `enum term_color_error_t`
81
82 NOTE: color is not null-terminated as its length is given by k which shall be
83 between 1 and 16.*/
84int tcol_color_parse(char *dst, size_t dstn, char color[16],
85 size_t k, size_t *len);
86
87/* By default libtermcolor will use color. However this behavior can be manually
88 overridden using this function.
89
90 Parameters
91 - `enable_color`: Whether to enable "colorization" of the format string */
92void tcol_override_color_checks(bool enable_color);
93
94/* Return the current state of colorization. */
95bool tcol_has_color(void);
96
97/* Printfs the colorized format string to the specified stream.
98
99 Parameters:
100 - `fmt`: The termcolor format string
101 - `...`: The variable argumentsto replace the format specifiers with
102
103 Return Value:
104 - `int`: One of the values `enum term_color_error_t` */
105int tcol_fprintf(FILE *stream, const char *fmt, ...);
106
107/* Printfs the colorized format string to the standard output.
108
109 Parameters:
110 - `stream`: The `FILE*` stream to printf to
111 - `fmt`: The termcolor format string
112 - `...`: The variable argumentsto replace the format specifiers with
113
114 Return Value:
115 - `int`: One of the values `enum term_color_error_t` */
116int tcol_printf(const char *fmt, ...);
117
118/* Snprintfs the colorized format string to the specified string stream.
119
120 Parameters:
121 - `buffer`: The `char*` stream to printf to
122 - `N`: The total size of `buffer`, including the null terminatr
123 - `fmt`: The termcolor format string
124 - `...`: The variable argumentsto replace the format specifiers with
125
126 Return Value:
127 - `int`: One of the values `enum term_color_error_t`*/
128int tcol_snprintf(char *buffer, size_t N, const char *fmt, ...);
char const char * fmt
Definition gwion_print.h:2
const char * tcol_errorstr(const enum term_color_error_t err)
Definition termcolor.c:32
int tcol_snprintf(char *buffer, size_t N, const char *fmt,...)
Definition termcolor.c:382
int tcol_fprintf(FILE *stream, const char *fmt,...)
Definition termcolor.c:365
int tcol_printf(const char *fmt,...)
Definition termcolor.c:373
int _tcol_color_generate(char *dst, size_t dstn, size_t *len, int rep, int foreground, int background)
Definition termcolor.c:76
void tcol_override_color_checks(bool enable_color)
Definition termcolor.c:40
int tcol_color_parse(char *dst, size_t dstn, char color[16], size_t k, size_t *len)
Definition termcolor.c:142
bool tcol_has_color(void)
Definition termcolor.c:36
term_color_error_t
Definition termcolor.h:36
@ TermColorErrorInvalidColor
Definition termcolor.h:40
@ TermColorErrorUnterminatedColor
Definition termcolor.h:41
@ TERM_COLOR_ERROR_COUNT
Definition termcolor.h:42
@ TermColorErrorAllocationFailed
Definition termcolor.h:38
@ TermColorErrorNone
Definition termcolor.h:37
@ TermColorErrorPrintingFailed
Definition termcolor.h:39