2
0

Disable colors when output is not a terminal.

This commit is contained in:
Maarten Billemont 2014-11-23 13:33:09 -05:00
parent 2b8498f569
commit 37ec21f5be

View File

@ -11,6 +11,7 @@
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <alg/sha256.h>
@ -209,35 +210,46 @@ static int putvar(int c) {
}
const char *Identicon(const char *userName, const char *masterPassword) {
const char *left[] = { "", "", "", "" };
const char *right[] = { "", "", "", "" };
const char *leftArm[] = { "", "", "", "" };
const char *rightArm[] = { "", "", "", "" };
const char *body[] = { "", "", "", "", "", "" };
const char *accessory[] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
uint8_t identiconSeed[32];
HMAC_SHA256_Buf(masterPassword, strlen(masterPassword), userName, strlen(userName), identiconSeed);
bool useColor = 0;
#ifdef COLOR
setupterm(NULL, 2, NULL);
initputvar();
tputs(tparm(tgetstr("AF", NULL), identiconSeed[4] % 7 + 1), 1, putvar);
char red[strlen(putvarc)];
strcpy(red, putvarc);
tputs(tgetstr("me", NULL), 1, putvar);
char reset[strlen(putvarc)];
strcpy(reset, putvarc);
#else
const char *red = "", *reset = "";
if (isatty(STDERR_FILENO))
useColor = 1;
#endif
uint8_t colorIdentifier = identiconSeed[4] % 7 + 1;
char *colorString, *resetString;
if (useColor) {
setupterm(NULL, STDERR_FILENO, NULL);
initputvar();
tputs(tparm(tgetstr("AF", NULL), colorIdentifier), 1, putvar);
colorString = calloc(strlen(putvarc), sizeof(char));
strcpy(colorString, putvarc);
tputs(tgetstr("me", NULL), 1, putvar);
resetString = calloc(strlen(putvarc), sizeof(char));
strcpy(resetString, putvarc);
} else {
colorString = calloc(1, sizeof(char));
resetString = calloc(1, sizeof(char));
}
char *identicon = (char *)calloc(20, sizeof(char));
sprintf(identicon, "%s%s%s%s%s%s",
red,
left[identiconSeed[0] % (sizeof(left) / sizeof(left[0]))],
colorString,
leftArm[identiconSeed[0] % (sizeof(leftArm) / sizeof(leftArm[0]))],
body[identiconSeed[1] % (sizeof(body) / sizeof(body[0]))],
right[identiconSeed[2] % (sizeof(right) / sizeof(right[0]))],
rightArm[identiconSeed[2] % (sizeof(rightArm) / sizeof(rightArm[0]))],
accessory[identiconSeed[3] % (sizeof(accessory) / sizeof(accessory[0]))],
reset);
resetString);
free(colorString);
free(resetString);
return identicon;
}