Add NO_COLOR support

This commit is contained in:
Shav Kinderlehrer 2023-04-11 22:53:21 -04:00
parent 87d147d78e
commit a3cae79124

View File

@ -12,14 +12,21 @@
#define GREY "\x1b[90m"
#define RESET "\x1b[0m"
int color = 1;
void run(FILE *fp, char *filename, int tty) {
const char *invert_t = color ? INVERT_T : "";
const char *uinvert_t = color ? UINVERT_T : "";
const char *grey = color ? GREY : "";
const char *reset = color ? RESET : "";
struct filedata f;
f = readfile(fp);
if (tty) {
char *addon = f.binary ? " <binary>" : "";
fprintf(stderr, "\r%s%s%s%s\r\n", INVERT_T, basename(filename), addon,
UINVERT_T);
fprintf(stderr, "\r%s%s%s%s\r\n", invert_t, basename(filename), addon,
uinvert_t);
}
int lcpad = intlen(f.lc);
@ -39,8 +46,8 @@ void run(FILE *fp, char *filename, int tty) {
if (padlen)
memset(padding, ' ', padlen);
fprintf(stderr, "\r%s%s%d:%s ", GREY, padlen > 0 ? padding : "", f.lc,
RESET); // padlen < 1 causes undefined
fprintf(stderr, "\r%s%s%d:%s ", grey, padlen > 0 ? padding : "", f.lc,
reset); // padlen < 1 causes undefined
}
pc = c;
@ -53,11 +60,19 @@ void run(FILE *fp, char *filename, int tty) {
float rounded;
char *format = formatbytes(f.len, &rounded);
fprintf(stderr, "\n%s%.2f %s%s\r\n", INVERT_T, rounded, format, UINVERT_T);
char *cnewline = c == '\n' ? "" : "\n";
fprintf(stderr, "\r%s%s%.2f %s%s\r\n", cnewline, invert_t, rounded, format,
uinvert_t);
}
}
int main(int argc, char *argv[]) {
char *no_color = getenv("NO_COLOR");
if (no_color != NULL && no_color[0] != '\0') {
color = 0;
}
if (argc > 1) {
for (int i = 1; i < argc; i++) { // start at one to offset argv[0]
FILE *fp = fopen(argv[i], "rb");