Try to approximate if file is printeable

This commit is contained in:
Shav Kinderlehrer 2023-04-11 19:17:36 -04:00
parent da0287ec63
commit 9ffc001bf2
5 changed files with 37 additions and 14 deletions

View File

@ -5,6 +5,7 @@
struct filedata {
int lc;
size_t len;
int binary;
char *buf;
};

View File

@ -4,7 +4,7 @@
void die(const char *message);
char *formatBytes(size_t bytes, float *rounded);
char *formatbytes(size_t bytes, float *rounded);
int intlen(size_t i);
#endif

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
#include "util.h"
@ -9,9 +10,9 @@ struct filedata readfile(FILE *fp) {
f.lc = 0;
f.len = 0;
f.binary = 0;
size_t bufsize = 4;
f.buf = malloc(bufsize);
if (f.buf == NULL)
die("malloc");
@ -37,5 +38,19 @@ struct filedata readfile(FILE *fp) {
f.buf[f.len++] = c;
}
// guess if printable
// from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs
int testlen = 64;
char *testbuf[testlen];
memcpy(testbuf, f.buf, testlen);
char *result = memchr(testbuf, 0x00, testlen);
if (result) {
f.binary = 1;
} else {
f.binary = 0;
}
return f;
}

View File

@ -7,7 +7,7 @@ void die(const char *message) {
exit(1);
}
char *formatBytes(size_t bytes, float *rounded) {
char *formatbytes(size_t bytes, float *rounded) {
char *SIZES[] = {"bytes", "kB", "MB", "GB"};
size_t size = bytes;

View File

@ -12,14 +12,15 @@
#define GREY "\x1b[90m"
#define RESET "\x1b[0m"
void run(FILE *fp, char *filename) {
int tty = isatty(STDOUT_FILENO);
void run(FILE *fp, char *filename, int tty) {
struct filedata f;
f = readfile(fp);
if (tty)
fprintf(stderr, "\r%s%s%s\r\n", INVERT_T, basename(filename), UINVERT_T);
if (tty) {
char *addon = f.binary ? " <binary>" : "";
fprintf(stderr, "\n%s%s%s%s\r\n", INVERT_T, basename(filename), addon,
UINVERT_T);
}
int lcpad = intlen(f.lc);
@ -29,12 +30,14 @@ void run(FILE *fp, char *filename) {
for (size_t i = 0; i < f.len; i++) {
c = f.buf[i];
if (tty && (pc == '\n' || i == 0)) {
if (tty && !f.binary && (pc == '\n' || i == 0)) {
f.lc++;
int padlen = lcpad - intlen(f.lc);
char padding[padlen];
memset(padding, ' ', padlen);
if (padlen)
memset(padding, ' ', padlen);
fprintf(stderr, "\r%s%s%d:%s ", GREY, padlen > 0 ? padding : "", f.lc,
RESET); // padlen < 1 causes undefined
@ -44,11 +47,13 @@ void run(FILE *fp, char *filename) {
printf("%c", c);
}
fflush(stdout); // prevent timing inconsistencies between stdout and stderr
if (tty) {
float rounded;
char *format = formatBytes(f.len, &rounded);
char *format = formatbytes(f.len, &rounded);
fprintf(stderr, "\r%s%.2f %s%s\r\n", INVERT_T, rounded, format, UINVERT_T);
fprintf(stderr, "\n%s%.2f %s%s\r\n", INVERT_T, rounded, format, UINVERT_T);
}
}
@ -58,7 +63,9 @@ int main(int argc, char *argv[]) {
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL)
die(argv[i]);
run(fp, argv[i]);
int tty = isatty(STDOUT_FILENO);
run(fp, argv[i], tty);
fclose(fp);
if (i + 1 != argc) {
@ -66,7 +73,7 @@ int main(int argc, char *argv[]) {
}
}
} else {
run(stdin, "stdin"); // for piped-input or repl-like behavior
run(stdin, "stdin", 1); // for piped-input or repl-like behavior
}
return 0;