From f0988a8a83142786295136d2a029c9eeb0f7d54e Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Sat, 29 Apr 2023 15:42:42 -0400 Subject: [PATCH] Start implementing -e - extensions --- include/arg.h | 1 + src/lib/arg.c | 33 +++++++++++++++++++-------------- src/main.c | 20 +++++++++++++++----- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/include/arg.h b/include/arg.h index 6782ef5..a1864c7 100644 --- a/include/arg.h +++ b/include/arg.h @@ -14,6 +14,7 @@ struct config { bool literal; bool pager; char *name; + char *extension; bool has_read_stdin; }; diff --git a/src/lib/arg.c b/src/lib/arg.c index 3634831..030d453 100644 --- a/src/lib/arg.c +++ b/src/lib/arg.c @@ -1,13 +1,13 @@ #include #include -#include #include +#include #include "arg.h" #include "util.h" -#define LAT_SHORT_ARGS "cltbrpn:Vh" -#define LAT_USAGE "usage: lat [-cltbrpnVh] [file...]" +#define LAT_SHORT_ARGS "cltbrpn:e:Vh" +#define LAT_USAGE "usage: lat [-cltbrpneVh] [file...]" struct config conf; @@ -16,17 +16,19 @@ void help(void) { "embellishments\n\n"); printf("%s\n\n", LAT_USAGE); - printf( - "options:\n" - "\t-c\t\t toggle color\n" - "\t-l\t\t toggle line numbers\n" - "\t-t\t\t toggle file info headers\n" - "\t-b\t\t set binary mode, -b forces binary and -bb forces NOT binary\n" - "\t-r\t\t print everything (headers, line numbers, etc.) to stdout (or equivalent)\n" - "\t-p\t\t print file with the pager (uses less)\n" - "\t-n \t\t manually set the name of the file in the title\n" - "\t-V\t\t show program version\n" - "\t-h\t\t display this help text\n\n"); + printf("options:\n" + "\t-c toggle color\n" + "\t-l toggle line numbers\n" + "\t-t toggle file info headers\n" + "\t-b set binary mode, -b forces binary and -bb forces NOT " + "binary\n" + "\t-r print everything (headers, line numbers, etc.) to " + "stdout (or equivalent)\n" + "\t-p print file with the pager (uses less)\n" + "\t-n manually set the name of the file shown in the title\n" + "\t-e link extension to lat\n" + "\t-V show program version\n" + "\t-h display this help text\n\n"); printf("environment:\n" "\tNO_COLOR, see https://no-color.org/\n"); } @@ -69,6 +71,9 @@ int parseargs(int argc, char *argv[]) { case 'n': conf.name = optarg; break; + case 'e': + conf.extension = optarg; + break; case 'V': version(); exit(EXIT_SUCCESS); diff --git a/src/main.c b/src/main.c index 4afabc8..781ab41 100644 --- a/src/main.c +++ b/src/main.c @@ -83,11 +83,14 @@ void run(FILE *fp, char *filename, bool tty) { int linecount = 1; for (int i = 0; i < f.lc; i++) { if (conf.lines) { - char *padding = linepad(linecount, f.lc); - fprintf(st, "%s%s%d│%s ", c.grey, padding, i + 1, c.reset); + { // line numbers + 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); fprintf(st, "\n"); - free(padding); linecount++; } else { fprintf(st, "%s\n", f.lines[i].buf); @@ -97,7 +100,8 @@ void run(FILE *fp, char *filename, bool tty) { } else { fwrite(f.buf, 1, f.buflen, st); fflush(st); - fwrite("\n", 1, 1, err); + if (tty) + fwrite("\n", 1, 1, err); } free(f.buf); free(f.lines); @@ -127,6 +131,7 @@ void initconf(void) { conf.lines = true; conf.name = NULL; + conf.extension = NULL; } void clearstdin(void) { @@ -150,8 +155,13 @@ int main(int argc, char *argv[]) { 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) { - int offset = parseargs(argc, argv); tty = tty || conf.literal;