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 <string.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <alg/sha256.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 *Identicon(const char *userName, const char *masterPassword) {
const char *left[] = { "", "", "", "" }; const char *leftArm[] = { "", "", "", "" };
const char *right[] = { "", "", "", "" }; const char *rightArm[] = { "", "", "", "" };
const char *body[] = { "", "", "", "", "", "" }; const char *body[] = { "", "", "", "", "", "" };
const char *accessory[] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }; const char *accessory[] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
uint8_t identiconSeed[32]; uint8_t identiconSeed[32];
HMAC_SHA256_Buf(masterPassword, strlen(masterPassword), userName, strlen(userName), identiconSeed); HMAC_SHA256_Buf(masterPassword, strlen(masterPassword), userName, strlen(userName), identiconSeed);
bool useColor = 0;
#ifdef COLOR #ifdef COLOR
setupterm(NULL, 2, NULL); if (isatty(STDERR_FILENO))
initputvar(); useColor = 1;
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 = "";
#endif #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)); char *identicon = (char *)calloc(20, sizeof(char));
sprintf(identicon, "%s%s%s%s%s%s", sprintf(identicon, "%s%s%s%s%s%s",
red, colorString,
left[identiconSeed[0] % (sizeof(left) / sizeof(left[0]))], leftArm[identiconSeed[0] % (sizeof(leftArm) / sizeof(leftArm[0]))],
body[identiconSeed[1] % (sizeof(body) / sizeof(body[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]))], accessory[identiconSeed[3] % (sizeof(accessory) / sizeof(accessory[0]))],
reset); resetString);
free(colorString);
free(resetString);
return identicon; return identicon;
} }