- --literal
- fix outputting format for less and piped output (finally)
This commit is contained in:
Shav Kinderlehrer 2023-04-19 22:30:10 -04:00
parent c713312b22
commit 83929dd8aa
4 changed files with 31 additions and 16 deletions

View File

@ -2,7 +2,7 @@
#define ARG_H #define ARG_H
#include <stdbool.h> #include <stdbool.h>
#define LAT_VERSION "0.10.2" #define LAT_VERSION "0.11.0"
struct config { struct config {
bool stdin; bool stdin;
@ -11,6 +11,7 @@ struct config {
bool lines; bool lines;
bool headers; bool headers;
int force_binary; int force_binary;
bool literal;
bool pager; bool pager;
bool has_read_stdin; bool has_read_stdin;
}; };

View File

@ -5,7 +5,7 @@
#include "arg.h" #include "arg.h"
#include "util.h" #include "util.h"
#define LAT_USAGE "usage: lat [-cntbpVh] [file...]" #define LAT_USAGE "usage: lat [-cntblpVh] [file...]"
void help(void) { void help(void) {
printf("lat | lazy cat - a cat clone with some quality-of-life " printf("lat | lazy cat - a cat clone with some quality-of-life "
@ -18,6 +18,7 @@ void help(void) {
"\t-t, --headers\t toggle file info headers\n" "\t-t, --headers\t toggle file info headers\n"
"\t-b, --binary\t toggle binary mode, -b forces binary and -bb forces " "\t-b, --binary\t toggle binary mode, -b forces binary and -bb forces "
"NOT binary\n" "NOT binary\n"
"\t-l, --literal\t print everything to stdout (or equivalent)\n"
"\t-p, --pager\t print file with the pager (uses less)\n" "\t-p, --pager\t print file with the pager (uses less)\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 (--help shows additional " "\t-h, --help\t display this help text (--help shows additional "
@ -42,7 +43,9 @@ void examples(void) {
"\tlat -bb --pager file.txt\n\t\t force file.txt to NOT be treated " "\tlat -bb --pager file.txt\n\t\t force file.txt to NOT be treated "
"as a binary file and print it in the pager\n" "as a binary file and print it in the pager\n"
"\tcurl example.com | lat\n\t\t pipe the results of 'curl example.com' " "\tcurl example.com | lat\n\t\t pipe the results of 'curl example.com' "
"into lat\n"); "into lat\n"
"\tfzf --preview 'lat -l {}'\n\t\t use lat as the file viewer in fzf\n"
);
} }
void version(void) { void version(void) {
@ -82,6 +85,10 @@ void parselongarg(char *arg) {
return; return;
} }
if (strcmp(arg, "--literal") == 0) {
conf.literal = !conf.literal;
}
if (strcmp(arg, "--pager") == 0) { if (strcmp(arg, "--pager") == 0) {
conf.pager = !conf.pager; conf.pager = !conf.pager;
} }
@ -122,6 +129,9 @@ void parseshortarg(char *arg) {
else else
conf.force_binary = !conf.force_binary; conf.force_binary = !conf.force_binary;
break; break;
case 'l':
conf.literal = !conf.literal;
break;
case 'p': case 'p':
conf.pager = !conf.pager; conf.pager = !conf.pager;
break; break;

View File

@ -34,6 +34,10 @@ void run(FILE *fp, char *filename, bool tty) {
die("popen 'less'"); die("popen 'less'");
} }
if (conf.literal) {
err = st;
}
if (conf.force_binary > 0) { if (conf.force_binary > 0) {
f.binary = true; f.binary = true;
} else if (conf.force_binary == 0) { } else if (conf.force_binary == 0) {
@ -42,11 +46,11 @@ void run(FILE *fp, char *filename, bool tty) {
if (conf.headers) { if (conf.headers) {
char *addon = f.binary ? "<binary>" : ""; char *addon = f.binary ? "<binary>" : "";
if (conf.pager) if (conf.stdin && !conf.pager)
fprintf(err, "%s%s%s%s\r\n", invert_t, basename(filename), addon, reset); fprintf(err, "\x1b[2K\r%s%s%s%s\n", invert_t, basename(filename), addon,
else
fprintf(err, "\x1b[2K\r%s%s%s%s\r\n", invert_t, basename(filename), addon,
reset); reset);
else
fprintf(err, "%s%s%s%s\n", invert_t, basename(filename), addon, reset);
} }
conf.process = (tty && !f.binary); conf.process = (tty && !f.binary);
@ -83,14 +87,11 @@ void run(FILE *fp, char *filename, bool tty) {
float rounded; float rounded;
char *format = formatbytes(f.buflen, &rounded); char *format = formatbytes(f.buflen, &rounded);
fprintf(err, "%s%.2f %s%s\r\n", invert_t, rounded, format, reset); fprintf(err, "%s%.2f %s%s\n", invert_t, rounded, format, reset);
} }
if (conf.pager) { if (conf.pager) {
pclose(st); // err is already the same as st pclose(st); // err is already the same as st
st = stdout;
err = stderr;
} }
} }
@ -99,6 +100,7 @@ void initconf(void) {
conf.stdin = false; conf.stdin = false;
conf.has_read_stdin = false; conf.has_read_stdin = false;
conf.pager = false; conf.pager = false;
conf.literal = false;
conf.process = true; conf.process = true;
conf.headers = true; conf.headers = true;
conf.color = true; conf.color = true;
@ -125,8 +127,10 @@ int main(int argc, char *argv[]) {
} }
bool tty = isatty(STDOUT_FILENO); bool tty = isatty(STDOUT_FILENO);
if (argc > 1) { if (argc > 1) {
int offset = parseargs(argc, argv); int offset = parseargs(argc, argv);
tty = tty || conf.literal;
conf.headers = conf.headers && tty; // tty still overrides user conf.headers = conf.headers && tty; // tty still overrides user
conf.pager = conf.pager && tty; conf.pager = conf.pager && tty;
@ -148,7 +152,7 @@ int main(int argc, char *argv[]) {
fclose(fp); fclose(fp);
if (tty && (i + 1 != argc)) { if (tty && (i + 1 != argc)) {
printf("offset: %d argc: %d\n", i, argc); printf("offset: %d argc: %d\n", i, argc);
fprintf(err, "\r\n"); // separate concurrent files in tty fprintf(err, "\n"); // separate concurrent files in tty
} }
} }