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 pager;
char *name;
char *extension;
bool has_read_stdin;
};

View File

@ -1,13 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdnoreturn.h>
#include <unistd.h>
#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 <name>\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 <name> manually set the name of the file shown in the title\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"
"\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);

View File

@ -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;