- --headers
This commit is contained in:
Shav Kinderlehrer 2023-04-17 22:04:03 -04:00
parent f8bdf764b2
commit b60ecd747b
3 changed files with 16 additions and 3 deletions

View File

@ -9,6 +9,7 @@ struct config {
bool process; bool process;
bool color; bool color;
bool lines; bool lines;
bool headers;
bool force_binary; bool force_binary;
bool has_read_stdin; bool has_read_stdin;
}; };

View File

@ -11,6 +11,7 @@ void help(void) {
printf("options:\n" printf("options:\n"
"\t-c, --color\t toggle whether to print color or not\n" "\t-c, --color\t toggle whether to print color or not\n"
"\t-n, --lines\t toggle whether to print line numbers or not\n" "\t-n, --lines\t toggle whether to print line numbers or not\n"
"\t-t, --headers\t toggle whether to print file headers or not\n"
"\t-b, --binary\t toggle whether to force the data to be treated as " "\t-b, --binary\t toggle whether to force the data to be treated as "
"binary or not\n" "binary or not\n"
"\t-V, --version\t show program version\n" "\t-V, --version\t show program version\n"
@ -40,6 +41,11 @@ void parselongarg(char *arg) {
return; return;
} }
if (strcmp(arg, "--headers")) {
conf.headers = !conf.headers;
return;
}
if (strcmp(arg, "--binary") == 0) { if (strcmp(arg, "--binary") == 0) {
conf.force_binary = !conf.force_binary; conf.force_binary = !conf.force_binary;
return; return;
@ -71,6 +77,9 @@ void parseshortarg(char *arg) {
case 'n': case 'n':
conf.lines = !conf.lines; conf.lines = !conf.lines;
break; break;
case 't':
conf.headers = !conf.headers;
break;
case 'b': case 'b':
conf.force_binary = !conf.force_binary; conf.force_binary = !conf.force_binary;
break; break;

View File

@ -27,7 +27,9 @@ void run(FILE *fp, char *filename, bool tty) {
f.binary = !f.binary; f.binary = !f.binary;
} }
if (tty) { conf.headers = conf.headers && tty; // tty still overrides user
if (conf.headers) {
char *addon = f.binary ? "<binary>" : ""; char *addon = f.binary ? "<binary>" : "";
fprintf(stderr, "\r\x1b[2K%s%s%s%s\r\n", invert_t, basename(filename), fprintf(stderr, "\r\x1b[2K%s%s%s%s\r\n", invert_t, basename(filename),
addon, reset); addon, reset);
@ -60,7 +62,7 @@ void run(FILE *fp, char *filename, bool tty) {
fflush(stdout); // prevent timing inconsistencies between stdout and stderr fflush(stdout); // prevent timing inconsistencies between stdout and stderr
if (tty) { if (conf.headers) {
float rounded; float rounded;
char *format = formatbytes(f.buflen, &rounded); char *format = formatbytes(f.buflen, &rounded);
@ -71,10 +73,11 @@ void run(FILE *fp, char *filename, bool tty) {
void initconf(void) { void initconf(void) {
conf.stdin = false; conf.stdin = false;
conf.force_binary = false; conf.force_binary = false;
conf.has_read_stdin = false;
conf.process = true; conf.process = true;
conf.headers = true;
conf.color = true; conf.color = true;
conf.lines = true; conf.lines = true;
conf.has_read_stdin = false;
} }
void clearstdin(void) { void clearstdin(void) {