- --binary
This commit is contained in:
Shav Kinderlehrer 2023-04-17 21:07:24 -04:00
parent f853dabe7f
commit 2abab7c9e8
3 changed files with 26 additions and 9 deletions

View File

@ -2,13 +2,14 @@
#define ARG_H #define ARG_H
#include <stdbool.h> #include <stdbool.h>
#define LAT_VERSION "0.7.3" #define LAT_VERSION "0.8.0"
struct config { struct config {
bool stdin; bool stdin;
bool process; bool process;
bool color; bool color;
bool lines; bool lines;
bool force_binary;
bool has_read_stdin; bool has_read_stdin;
}; };

View File

@ -4,15 +4,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define LAT_USAGE "usage: lat [-cnVh] [files..]" #define LAT_USAGE "usage: lat [-cnbVh] [files..]"
void help(void) { void help(void) {
printf("%s\n", LAT_USAGE); printf("%s\n", LAT_USAGE);
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-b, --binary\t toggle whether to force the data to be treated as "
"binary or not\n"
"\t-V, --version\t show program version\n" "\t-V, --version\t show program version\n"
"\t-h, --help\t display this help text\n"); "\t-h, --help\t display this help text\n");
printf("");
} }
void version(void) { void version(void) {
@ -37,6 +40,11 @@ void parselongarg(char *arg) {
return; return;
} }
if (strcmp(arg, "--binary") == 0) {
conf.force_binary = !conf.force_binary;
return;
}
if (strcmp(arg, "--help") == 0) { if (strcmp(arg, "--help") == 0) {
help(); help();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -63,14 +71,17 @@ void parseshortarg(char *arg) {
case 'n': case 'n':
conf.lines = !conf.lines; conf.lines = !conf.lines;
break; break;
case 'h': case 'b':
help(); conf.force_binary = !conf.force_binary;
exit(EXIT_SUCCESS);
break; break;
case 'V': case 'V':
version(); version();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'h':
help();
exit(EXIT_SUCCESS);
break;
default: { default: {
char *str = malloc(2); char *str = malloc(2);
str[0] = c; str[0] = c;

View File

@ -23,6 +23,10 @@ void run(FILE *fp, char *filename, bool tty) {
struct filedata f; struct filedata f;
f = readfile(fp, conf.stdin); f = readfile(fp, conf.stdin);
if (conf.force_binary) {
f.binary = !f.binary;
}
if (tty) { if (tty) {
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),
@ -35,18 +39,18 @@ void run(FILE *fp, char *filename, bool tty) {
} }
if (conf.process) { if (conf.process) {
int linecount = 0; int linecount = 1;
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);
printf("%s%s%d:%s %s\n", grey, padding, i + 1, reset, f.lines[i].buf); printf("%s%s%d:%s ", grey, padding, i + 1, reset);
fwrite(f.lines[i].buf, 1, f.lines[i].len, stdout);
printf("\n");
free(padding); free(padding);
linecount++; linecount++;
} else { } else {
printf("%s\n", f.lines[i].buf); printf("%s\n", f.lines[i].buf);
} }
free(f.lines[i].buf); free(f.lines[i].buf);
} }
} else { } else {
@ -66,6 +70,7 @@ 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.process = true; conf.process = true;
conf.color = true; conf.color = true;
conf.lines = true; conf.lines = true;