Start implementing -e

- extensions
This commit is contained in:
Shav Kinderlehrer 2023-04-29 15:42:42 -04:00
parent 0f9a68c578
commit e4f517c020
3 changed files with 35 additions and 19 deletions

View File

@ -14,6 +14,7 @@ struct config {
bool literal; bool literal;
bool pager; bool pager;
char *name; char *name;
char *extension;
bool has_read_stdin; bool has_read_stdin;
}; };

View File

@ -1,13 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <stdnoreturn.h> #include <stdnoreturn.h>
#include <unistd.h>
#include "arg.h" #include "arg.h"
#include "util.h" #include "util.h"
#define LAT_SHORT_ARGS "cltbrpn:Vh" #define LAT_SHORT_ARGS "cltbrpn:e:Vh"
#define LAT_USAGE "usage: lat [-cltbrpnVh] [file...]" #define LAT_USAGE "usage: lat [-cltbrpneVh] [file...]"
struct config conf; struct config conf;
@ -16,17 +16,19 @@ void help(void) {
"embellishments\n\n"); "embellishments\n\n");
printf("%s\n\n", LAT_USAGE); printf("%s\n\n", LAT_USAGE);
printf( printf("options:\n"
"options:\n" "\t-c toggle color\n"
"\t-c\t\t toggle color\n" "\t-l toggle line numbers\n"
"\t-l\t\t toggle line numbers\n" "\t-t toggle file info headers\n"
"\t-t\t\t toggle file info headers\n" "\t-b set binary mode, -b forces binary and -bb forces NOT "
"\t-b\t\t set binary mode, -b forces binary and -bb forces NOT binary\n" "binary\n"
"\t-r\t\t print everything (headers, line numbers, etc.) to stdout (or equivalent)\n" "\t-r print everything (headers, line numbers, etc.) to "
"\t-p\t\t print file with the pager (uses less)\n" "stdout (or equivalent)\n"
"\t-n <name>\t\t manually set the name of the file in the title\n" "\t-p print file with the pager (uses less)\n"
"\t-V\t\t show program version\n" "\t-n <name> manually set the name of the file shown in the title\n"
"\t-h\t\t display this help text\n\n"); "\t-e <program> link extension to lat\n"
"\t-V show program version\n"
"\t-h display this help text\n\n");
printf("environment:\n" printf("environment:\n"
"\tNO_COLOR, see https://no-color.org/\n"); "\tNO_COLOR, see https://no-color.org/\n");
} }
@ -69,6 +71,9 @@ int parseargs(int argc, char *argv[]) {
case 'n': case 'n':
conf.name = optarg; conf.name = optarg;
break; break;
case 'e':
conf.extension = optarg;
break;
case 'V': case 'V':
version(); version();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -83,11 +83,14 @@ void run(FILE *fp, char *filename, bool tty) {
int linecount = 1; 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); { // line numbers
fprintf(st, "%s%s%d│%s ", c.grey, padding, i + 1, c.reset); char *padding = linepad(linecount, f.lc);
fprintf(err, "%s%s%d│%s ", c.grey, padding, i + 1, c.reset);
free(padding);
}
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);
linecount++; linecount++;
} else { } else {
fprintf(st, "%s\n", f.lines[i].buf); fprintf(st, "%s\n", f.lines[i].buf);
@ -97,7 +100,8 @@ void run(FILE *fp, char *filename, bool tty) {
} else { } else {
fwrite(f.buf, 1, f.buflen, st); fwrite(f.buf, 1, f.buflen, st);
fflush(st); fflush(st);
fwrite("\n", 1, 1, err); if (tty)
fwrite("\n", 1, 1, err);
} }
free(f.buf); free(f.buf);
free(f.lines); free(f.lines);
@ -127,6 +131,7 @@ void initconf(void) {
conf.lines = true; conf.lines = true;
conf.name = NULL; conf.name = NULL;
conf.extension = NULL;
} }
void clearstdin(void) { void clearstdin(void) {
@ -150,8 +155,13 @@ int main(int argc, char *argv[]) {
bool tty = isatty(STDOUT_FILENO); bool tty = isatty(STDOUT_FILENO);
int offset = parseargs(argc, argv);
conf.headers = conf.headers && tty;
printf("set extension to '%s'\n", conf.extension);
if (argc > 1) { if (argc > 1) {
int offset = parseargs(argc, argv);
tty = tty || conf.literal; tty = tty || conf.literal;