Refactor codebase

This commit is contained in:
Shav Kinderlehrer 2023-04-29 15:08:36 -04:00
parent 1c90bf55b1
commit 0f9a68c578
3 changed files with 46 additions and 25 deletions

View File

@ -5,7 +5,7 @@
#define LAT_VERSION "0.12.2" #define LAT_VERSION "0.12.2"
struct config { struct config {
bool stdin; bool isstdin;
bool process; bool process;
bool color; bool color;
bool lines; bool lines;

View File

@ -4,6 +4,7 @@
#include "types.h" #include "types.h"
#include "util.h" #include "util.h"
#include "arg.h"
bool isbinary(struct filedata *f) { bool isbinary(struct filedata *f) {
@ -22,7 +23,7 @@ bool isbinary(struct filedata *f) {
} }
} }
struct filedata readfile(FILE *fp, bool isstdin) { struct filedata readfile(FILE *fp) {
struct filedata f; struct filedata f;
f.lc = 0; f.lc = 0;
@ -32,7 +33,7 @@ struct filedata readfile(FILE *fp, bool isstdin) {
f.buf = NULL; f.buf = NULL;
f.lines = NULL; f.lines = NULL;
if (isstdin) { if (conf.isstdin) {
size_t bufsize = 1024; size_t bufsize = 1024;
f.buf = malloc(bufsize); f.buf = malloc(bufsize);
if (f.buf == NULL) if (f.buf == NULL)

View File

@ -15,13 +15,41 @@
FILE *st; FILE *st;
FILE *err; FILE *err;
struct colors {
char *invert_t;
char *grey;
char *reset;
};
struct colors c;
void initcolor(void) {
c.invert_t = conf.color ? INVERT_T : "";
c.grey = conf.color ? GREY : "";
c.reset = conf.color ? RESET : "";
}
void printheadertop(char *filename, bool binary) {
char *name = conf.name == NULL ? filename : conf.name;
char *addon = binary ? "<binary>" : "";
if (conf.isstdin && !conf.pager)
fprintf(err, "\x1b[2K\r%s%s%s%s\n", c.invert_t, name, addon, c.reset);
else
fprintf(err, "%s%s%s%s\n", c.invert_t, name, addon, c.reset);
}
void printheaderbottom(size_t buflen) {
float rounded;
char *format = formatbytes(buflen, &rounded);
fprintf(err, "%s%.2f %s%s\n", c.invert_t, rounded, format, c.reset);
}
void run(FILE *fp, char *filename, bool tty) { void run(FILE *fp, char *filename, bool tty) {
const char *invert_t = conf.color ? INVERT_T : ""; initcolor();
const char *grey = conf.color ? GREY : "";
const char *reset = conf.color ? RESET : "";
struct filedata f; struct filedata f;
f = readfile(fp, conf.stdin); f = readfile(fp, conf.isstdin);
if (conf.pager) { if (conf.pager) {
st = popen("less", "w"); st = popen("less", "w");
@ -41,15 +69,11 @@ void run(FILE *fp, char *filename, bool tty) {
f.binary = false; f.binary = false;
} }
char *name = conf.name == NULL ? filename : conf.name;
if (conf.headers) { if (conf.headers) {
char *addon = f.binary ? "<binary>" : ""; printheadertop(filename, f.binary);
if (conf.stdin && !conf.pager)
fprintf(err, "\x1b[2K\r%s%s%s%s\n", invert_t, name, addon, reset);
else
fprintf(err, "%s%s%s%s\n", invert_t, name, addon, reset);
} }
// any/all processing to be done
conf.process = (tty && !f.binary); conf.process = (tty && !f.binary);
if (conf.process) { // file display processing if (conf.process) { // file display processing
loadlines(&f); loadlines(&f);
@ -60,7 +84,7 @@ void run(FILE *fp, char *filename, bool tty) {
for (int i = 0; i < f.lc; i++) { for (int i = 0; i < f.lc; i++) {
if (conf.lines) { if (conf.lines) {
char *padding = linepad(linecount, f.lc); char *padding = linepad(linecount, f.lc);
fprintf(st, "%s%s%d│%s ", grey, padding, i + 1, reset); fprintf(st, "%s%s%d│%s ", c.grey, padding, i + 1, c.reset);
fwrite(f.lines[i].buf, 1, f.lines[i].len, st); fwrite(f.lines[i].buf, 1, f.lines[i].len, st);
fprintf(st, "\n"); fprintf(st, "\n");
free(padding); free(padding);
@ -82,10 +106,7 @@ void run(FILE *fp, char *filename, bool tty) {
fflush(err); fflush(err);
if (conf.headers) { if (conf.headers) {
float rounded; printheaderbottom(f.buflen);
char *format = formatbytes(f.buflen, &rounded);
fprintf(err, "%s%.2f %s%s\n", invert_t, rounded, format, reset);
} }
if (conf.pager) { if (conf.pager) {
@ -95,7 +116,7 @@ void run(FILE *fp, char *filename, bool tty) {
void initconf(void) { void initconf(void) {
conf.force_binary = -1; conf.force_binary = -1;
conf.stdin = false; conf.isstdin = false;
conf.has_read_stdin = false; conf.has_read_stdin = false;
conf.pager = false; conf.pager = false;
conf.literal = false; conf.literal = false;
@ -131,16 +152,15 @@ int main(int argc, char *argv[]) {
if (argc > 1) { if (argc > 1) {
int offset = parseargs(argc, argv); int offset = parseargs(argc, argv);
tty = tty || conf.literal; tty = tty || conf.literal;
conf.headers = conf.headers && tty; // tty still overrides user
conf.pager = conf.pager && tty;
for (int i = offset; i < argc; i++) { for (int i = offset; i < argc; i++) {
if (*argv[i] == '-') { if (*argv[i] == '-') {
if (conf.has_read_stdin) if (conf.has_read_stdin)
clearstdin(); clearstdin();
conf.has_read_stdin = true; conf.has_read_stdin = true;
conf.stdin = true; conf.isstdin = true;
run(stdin, "stdin", tty); run(stdin, "stdin", tty);
if (tty && (i + 1 != argc)) { if (tty && (i + 1 != argc)) {
fprintf(err, "\n"); // separate concurrent files in tty fprintf(err, "\n"); // separate concurrent files in tty
@ -149,7 +169,7 @@ int main(int argc, char *argv[]) {
continue; continue;
} }
conf.stdin = false; conf.isstdin = false;
FILE *fp = fopen(argv[i], "rb"); FILE *fp = fopen(argv[i], "rb");
if (fp == NULL) if (fp == NULL)
die(argv[i]); die(argv[i]);
@ -161,11 +181,11 @@ int main(int argc, char *argv[]) {
} }
if (offset == argc) { if (offset == argc) {
conf.stdin = true; conf.isstdin = true;
run(stdin, "stdin", tty); run(stdin, "stdin", tty);
} }
} else { } else {
conf.stdin = true; conf.isstdin = true;
run(stdin, "stdin", tty); // for piped-input or repl-like behavior run(stdin, "stdin", tty); // for piped-input or repl-like behavior
} }