-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from StefansM/feature-colors
Allow terminal colors to be changed either via `initc`, or using direct colors.
- Loading branch information
Showing
10 changed files
with
808 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <config.h> | ||
|
||
#include <assert.h> | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "err.h" | ||
|
||
// Maximum size of an error message. Any messages longer than this are just | ||
// truncated: I don't want to handle errors within errors. | ||
#define ERR_BUF_SZ 256 | ||
|
||
// Buffer containing an error message | ||
static char ERR_BUF[ERR_BUF_SZ]; | ||
// Offset into ERR_BUF: new messages should be appended after this. | ||
static size_t ERR_OFFSET = 0; | ||
|
||
/** These correspond to the numbers PIPES_C_ERRORS multipled by -1. */ | ||
static const char *PIPES_C_ERROR_STRINGS[] = { | ||
"Success. If you see this an error message, please raise an issue!", | ||
|
||
// Parameter 1: Detailed message indicating ncurses error | ||
"An error was encountered in ncurses: %s.", | ||
|
||
// Parameter 1: Number of colors in the palette | ||
// Parameter 2: Number of available colors. | ||
"Too many colors for the terminal to handle were given in the palette." | ||
" %d colors were specified, but only %d are available.", | ||
|
||
"A color palette was supplied, but the terminal cannot redefine colors.", | ||
"A color palette was supplied, but the terminal cannot use color.", | ||
"Error allocating memory.", | ||
"Terminal did not reply to query.", | ||
"Buffer (%d items) too small: tried to insert %d items.", | ||
"Error encountered calling standard library function: %s", | ||
"An error occurred when calling iconv." | ||
}; | ||
|
||
static void err_provenance(const char *file, int line, const char *function); | ||
|
||
/// Write prefix containing file name, line and function. | ||
static void err_provenance(const char *file, int line, const char *function) { | ||
ERR_OFFSET += snprintf(ERR_BUF + ERR_OFFSET, | ||
ERR_BUF_SZ - ERR_OFFSET, | ||
"[%s:%d (%s)] ", file, line, function); | ||
} | ||
|
||
|
||
/** Store message in buffer. */ | ||
void set_error_(const char *file, int line, const char *function, | ||
cpipes_errno err_num, ...) { | ||
assert(ERR_ICONV_ERROR <= err_num && err_num <= ERR_CURSES_ERR | ||
/* Error number is within the correct range */); | ||
|
||
err_provenance(file, line, function); | ||
|
||
va_list extra_args; | ||
va_start(extra_args, err_num); | ||
|
||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wformat-nonliteral" | ||
ERR_OFFSET += vsnprintf( | ||
ERR_BUF + ERR_OFFSET, ERR_BUF_SZ - ERR_OFFSET, | ||
PIPES_C_ERROR_STRINGS[-err_num], extra_args); | ||
#pragma GCC diagnostic pop | ||
va_end(extra_args); | ||
|
||
// Always terminate with a nul, even if snprintf didn't allow anything to | ||
// actually be appended. | ||
ERR_BUF[ERR_BUF_SZ - 1] = '\0'; | ||
} | ||
|
||
#ifdef HAVE_FUNC_ATTRIBUTE_FORMAT | ||
__attribute__((__format__ (__printf__, 4, 5))) | ||
#endif | ||
void add_error_info_(const char *file, int line, const char *function, | ||
const char *fmt, ...) { | ||
ERR_OFFSET += snprintf( | ||
ERR_BUF + ERR_OFFSET, | ||
ERR_BUF_SZ - ERR_OFFSET, | ||
"%s", "\n"); | ||
err_provenance(file, line, function); | ||
|
||
va_list extra_args; | ||
va_start(extra_args, fmt); | ||
ERR_OFFSET += vsnprintf( | ||
ERR_BUF + ERR_OFFSET, ERR_BUF_SZ - ERR_OFFSET, | ||
fmt, extra_args); | ||
va_end(extra_args); | ||
} | ||
|
||
/** Clear error buffer. */ | ||
void clear_error(void) { | ||
ERR_BUF[0] = '\0'; | ||
ERR_OFFSET = 0; | ||
} | ||
|
||
|
||
void print_error(void) { | ||
fprintf(stderr, "%s\n", ERR_BUF); | ||
} | ||
|
||
const char *string_error(void) { | ||
return ERR_BUF; | ||
} |
Oops, something went wrong.