Optimize printing

This commit is contained in:
Shav Kinderlehrer 2023-04-10 20:49:43 -04:00
parent 0e59c5f27c
commit 87f3769355

View File

@ -12,7 +12,7 @@
#define GREY "\x1b[90m" #define GREY "\x1b[90m"
#define RESET "\x1b[0m" #define RESET "\x1b[0m"
int run(char *filename) { void run(char *filename) {
int tty = isatty(STDOUT_FILENO); int tty = isatty(STDOUT_FILENO);
FILE *fp = fopen(filename, "r+b"); FILE *fp = fopen(filename, "r+b");
@ -36,13 +36,12 @@ int run(char *filename) {
for (unsigned i = 0; i < f.len; i++) { for (unsigned i = 0; i < f.len; i++) {
c = f.buf[i]; c = f.buf[i];
if (pc == '\n' || i == 0) { if (tty && (pc == '\n' || i == 0)) {
f.lc++; f.lc++;
int padlen = lcpad - intlen(f.lc); int padlen = lcpad - intlen(f.lc);
char padding[padlen]; char padding[padlen];
memset(padding, ' ', padlen); memset(padding, ' ', padlen);
if (tty)
fprintf(stderr, "%s%s%d:%s ", GREY, padding, f.lc, RESET); fprintf(stderr, "%s%s%d:%s ", GREY, padding, f.lc, RESET);
} }
@ -50,13 +49,12 @@ int run(char *filename) {
printf("%c", c); printf("%c", c);
} }
if (tty) {
float rounded; float rounded;
char *format = formatBytes(f.len, &rounded); char *format = formatBytes(f.len, &rounded);
if (tty)
fprintf(stderr, "%s%.2f %s%s\r\n", INVERT_T, rounded, format, UINVERT_T); fprintf(stderr, "%s%.2f %s%s\r\n", INVERT_T, rounded, format, UINVERT_T);
}
return 0;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -66,8 +64,7 @@ int main(int argc, char *argv[]) {
} }
for (int i = 1; i < argc; i++) { // start at one to offset argv[0] for (int i = 1; i < argc; i++) { // start at one to offset argv[0]
if (run(argv[i]) != 0) run(argv[i]);
die("run");
if (i + 1 != argc) { if (i + 1 != argc) {
fprintf(stderr, "\r\n"); // separate concurrent files fprintf(stderr, "\r\n"); // separate concurrent files