Read file quick

This commit is contained in:
Shav Kinderlehrer 2023-04-14 22:42:55 -04:00
parent ca8bb9e1c3
commit 6eadc6dd06
3 changed files with 23 additions and 19 deletions

View File

@ -7,6 +7,7 @@ struct filedata {
size_t len;
int binary;
char *buf;
char *altbuf;
};
struct filedata readfile(FILE *fp);

View File

@ -25,7 +25,7 @@ struct filedata readfile(FILE *fp) {
die("fread");
}
f.lc = 1000;
f.lc = 10000;
// guess if printable
// from https://github.com/sharkdp/content_inspector/blob/master/src/lib.rs

View File

@ -30,28 +30,30 @@ void run(FILE *fp, char *filename, bool tty) {
int lcpad = intlen(f.lc);
f.lc = 0;
char pc = '\0';
char c;
for (size_t i = 0; i < f.len; i++) {
c = f.buf[i];
// f.lc = 0;
// char pc = '\0';
// char c;
// for (size_t i = 0; i < f.len; i++) {
// c = f.buf[i];
if ((conf.lines && tty && !f.binary) && (pc == '\n' || i == 0)) {
f.lc++;
// if ((conf.lines && tty && !f.binary) && (pc == '\n' || i == 0)) {
// f.lc++;
int padlen = lcpad - intlen(f.lc);
char padding[padlen];
// int padlen = lcpad - intlen(f.lc);
// char padding[padlen];
if (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
}
// fprintf(stderr, "\r%s%s%d:%s ", grey, padlen > 0 ? padding : "", f.lc,
// reset); // padlen < 1 causes undefined
// }
pc = c;
printf("%c", c);
}
// pc = c;
// printf("%c", c);
// }
printf("%s", f.buf);
fflush(stdout); // prevent timing inconsistencies between stdout and stderr
@ -59,7 +61,8 @@ void run(FILE *fp, char *filename, bool tty) {
float rounded;
char *format = formatbytes(f.len, &rounded);
char *cnewline = c == '\n' ? "" : "\n";
// char *cnewline = c == '\n' ? "" : "\n";
char *cnewline = "";
fprintf(stderr, "\r%s%s%.2f %s%s\r\n", cnewline, invert_t, rounded, format,
uinvert_t);
}