- add color arg
- add lines arg
This commit is contained in:
Shav Kinderlehrer 2023-04-12 23:46:53 -04:00
parent c760493f48
commit 58e0d435c3
6 changed files with 94 additions and 15 deletions

14
include/arg.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef ARG_H
#define ARG_H
#include <stdbool.h>
struct config {
bool color;
bool lines;
};
extern struct config conf;
int parseargs(int argc, char *argv[]);
#endif

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#ifndef FILE_H
#define FILE_H
#include <stdio.h>
struct filedata {
int lc;
size_t len;

View File

@ -1,5 +1,6 @@
#ifndef LIB_H
#define LIB_H
#include <stdbool.h>
#include <stddef.h>
void die(const char *message);

64
src/lib/arg.c Normal file
View File

@ -0,0 +1,64 @@
#include "arg.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct config conf;
void argerr(char *r, char *arg) {
fprintf(stderr, "lat: %s '%s'\n", r, arg);
exit(EXIT_FAILURE);
}
void parselongarg(char *arg) {
if (strcmp(arg, "--color") == 0) {
conf.color = !conf.color;
return;
}
if (strcmp(arg, "--lines") == 0) {
conf.lines = !conf.lines;
return;
}
argerr("unrecognized arg", arg);
}
void parseshortarg(char *arg) {
size_t i = 1;
while (arg[i] != '\0') {
char c = arg[i];
switch (c) {
case 'c':
conf.color = !conf.color;
break;
case 'n':
conf.lines = !conf.lines;
break;
default:
argerr("unrecognized flag", &c);
break;
}
i++;
}
}
int parseargs(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; i++) { // offset for argv[0]
char *arg = argv[i];
if (arg[0] == '-') {
if (arg[1] == '-') {
parselongarg(arg);
continue;
}
parseshortarg(arg);
} else {
return i;
}
}
return i;
}

View File

@ -4,7 +4,7 @@
void die(const char *message) {
perror(message);
exit(1);
exit(EXIT_FAILURE);
}
char *formatbytes(size_t bytes, float *rounded) {

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
#include "arg.h"
#include "file.h"
#include "util.h"
@ -12,12 +13,6 @@
#define GREY "\x1b[90m"
#define RESET "\x1b[0m"
struct config {
int color;
};
struct config conf;
void run(FILE *fp, char *filename, int tty) {
const char *invert_t = conf.color ? INVERT_T : "";
const char *uinvert_t = conf.color ? UINVERT_T : "";
@ -41,7 +36,7 @@ void run(FILE *fp, char *filename, int tty) {
for (size_t i = 0; i < f.len; i++) {
c = f.buf[i];
if (tty && !f.binary && (pc == '\n' || i == 0)) {
if ((conf.lines && tty && !f.binary) && (pc == '\n' || i == 0)) {
f.lc++;
int padlen = lcpad - intlen(f.lc);
@ -70,24 +65,29 @@ void run(FILE *fp, char *filename, int tty) {
}
}
void initconf(void) { conf.color = 1; }
void initconf(void) {
conf.color = true;
conf.lines = true;
}
int main(int argc, char *argv[]) {
initconf();
// init no_color first so that args take priority
char *no_color = getenv("NO_COLOR");
if (no_color != NULL && no_color[0] != '\0') {
conf.color = 0;
conf.color = false;
}
if (argc > 1) {
for (int i = 1; i < argc; i++) { // start at one to offset argv[0]
int offset = parseargs(argc, argv);
for (int i = offset; i < argc; i++) { // start at one to offset argv[0]
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL)
die(argv[i]);
int tty = isatty(STDOUT_FILENO);
bool tty = isatty(STDOUT_FILENO);
run(fp, argv[i], tty);
fclose(fp);
@ -99,5 +99,5 @@ int main(int argc, char *argv[]) {
run(stdin, "stdin", 1); // for piped-input or repl-like behavior
}
return 0;
return EXIT_SUCCESS;
}