- --pager
This commit is contained in:
Shav Kinderlehrer 2023-04-19 10:46:17 -04:00
parent e586f29e6d
commit 23ede20d7c
3 changed files with 56 additions and 20 deletions

View File

@ -2,7 +2,7 @@
#define ARG_H #define ARG_H
#include <stdbool.h> #include <stdbool.h>
#define LAT_VERSION "0.9.3" #define LAT_VERSION "0.10.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 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 [-cntbVh] [file...]" #define LAT_USAGE "usage: lat [-cntbpVh] [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,23 +18,27 @@ void help(void) {
"\t-t, --headers\t toggle whether to print file headers or not\n" "\t-t, --headers\t toggle whether to print file headers or not\n"
"\t-b, --binary\t toggle whether to force the data to be treated as " "\t-b, --binary\t toggle whether to force the data to be treated as "
"binary or not. see examples\n" "binary or not. see examples\n"
"\t-p, --pager\t print file a pager (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\n\n"); "\t-h, --help\t display this help text (--help shows additional info)\n\n");
printf("environment:\n" printf("environment:\n"
"\tNO_COLOR, see https://no-color.org/\n\n"); "\tNO_COLOR, see https://no-color.org/\n\n");
}
void examples(void) {
printf( printf(
"examples:\n" "examples:\n"
"\tlat file1\n\t\t print the content of file1 witht default formatting\n" "\tlat file1\n\t\t print the content of file1 witht default formatting\n"
"\tlat - file1\n\t\t read from stdin (the '-' character reads from " "\tlat - file1\n\t\t read from stdin (the '-' character reads from "
"stdin) " "stdin) "
"and then print the contents of stdin and file1\n" "and then print the contents of stdin and file1\n"
"\tlat --lines --color file1 file2\n\t\t print the contents of file1 and " "\tlat -nc file1 file2\n\t\t print the contents of file1 and "
"file2 " "file2 "
"without printing line numbers or colors\n" "without printing line numbers or colors\n"
"\tlat --binary file.txt\n\t\t force file.txt to be treated as a binary " "\tlat --binary file.txt\n\t\t force file.txt to be treated as a binary "
"file\n" "file\n"
"\tlat -bb file.txt\n\t\t force file.txt to NOT be treated as a binary " "\tlat -bb --pager file.txt\n\t\t force file.txt to NOT be treated "
"file\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");
} }
@ -76,8 +80,13 @@ void parselongarg(char *arg) {
return; return;
} }
if (strcmp(arg, "--pager") == 0) {
conf.pager = !conf.pager;
}
if (strcmp(arg, "--help") == 0) { if (strcmp(arg, "--help") == 0) {
help(); help();
examples();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
return; return;
} }
@ -111,6 +120,9 @@ void parseshortarg(char *arg) {
else else
conf.force_binary = !conf.force_binary; conf.force_binary = !conf.force_binary;
break; break;
case 'p':
conf.pager = !conf.pager;
break;
case 'V': case 'V':
version(); version();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -15,6 +15,9 @@
#define GREY "\x1b[90m" #define GREY "\x1b[90m"
#define RESET "\x1b[0m" #define RESET "\x1b[0m"
FILE *st;
FILE *err;
void run(FILE *fp, char *filename, bool tty) { void run(FILE *fp, char *filename, bool tty) {
const char *invert_t = conf.color ? INVERT_T : ""; const char *invert_t = conf.color ? INVERT_T : "";
const char *grey = conf.color ? GREY : ""; const char *grey = conf.color ? GREY : "";
@ -23,18 +26,24 @@ void run(FILE *fp, char *filename, bool tty) {
struct filedata f; struct filedata f;
f = readfile(fp, conf.stdin); f = readfile(fp, conf.stdin);
if (conf.pager) {
st = popen("less", "w");
err = st;
if (st == NULL)
die("popen 'less'");
}
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) {
f.binary = false; f.binary = false;
} }
conf.headers = conf.headers && tty; // tty still overrides user
if (conf.headers) { if (conf.headers) {
char *addon = f.binary ? "<binary>" : ""; char *addon = f.binary ? "<binary>" : "";
fprintf(stderr, "\r\x1b[2K%s%s%s%s\r\n", invert_t, basename(filename), fprintf(err, "\x1b[2K\r%s%s%s%s\r\n", invert_t, basename(filename), addon,
addon, reset); reset);
} }
conf.process = (tty && !f.binary); conf.process = (tty && !f.binary);
@ -47,35 +56,44 @@ void run(FILE *fp, char *filename, bool tty) {
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); char *padding = linepad(linecount, f.lc);
printf("%s%s%d:%s ", grey, padding, i + 1, reset); fprintf(st, "%s%s%d│%s ", grey, padding, i + 1, reset);
fwrite(f.lines[i].buf, 1, f.lines[i].len, stdout); fwrite(f.lines[i].buf, 1, f.lines[i].len, st);
printf("\n"); fprintf(st, "\n");
free(padding); free(padding);
linecount++; linecount++;
} else { } else {
printf("%s\n", f.lines[i].buf); fprintf(st, "%s\n", f.lines[i].buf);
} }
free(f.lines[i].buf); free(f.lines[i].buf);
} }
} else { } else {
fwrite(f.buf, 1, f.buflen, stdout); fwrite(f.buf, 1, f.buflen, st);
fwrite("\n", 1, 1, err);
} }
free(f.buf); free(f.buf);
fflush(stdout); // prevent timing inconsistencies between stdout and stderr fflush(st); // prevent timing inconsistencies between st and err
if (conf.headers) { if (conf.headers) {
float rounded; float rounded;
char *format = formatbytes(f.buflen, &rounded); char *format = formatbytes(f.buflen, &rounded);
fprintf(stderr, "\r%s%.2f %s%s\r\n", invert_t, rounded, format, reset); fprintf(err, "%s%.2f %s%s\r\n", invert_t, rounded, format, reset);
}
if (conf.pager) {
pclose(st); // err is already the same as st
st = stdout;
err = stderr;
} }
} }
void initconf(void) { void initconf(void) {
conf.stdin = false;
conf.force_binary = -1; conf.force_binary = -1;
conf.stdin = false;
conf.has_read_stdin = false; conf.has_read_stdin = false;
conf.pager = false;
conf.process = true; conf.process = true;
conf.headers = true; conf.headers = true;
conf.color = true; conf.color = true;
@ -91,6 +109,9 @@ void clearstdin(void) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
initconf(); initconf();
st = stdout;
err = stderr;
// init no_color first so that args take priority // init no_color first so that args take priority
char *no_color = getenv("NO_COLOR"); char *no_color = getenv("NO_COLOR");
@ -101,6 +122,9 @@ 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);
conf.headers = conf.headers && tty; // tty still overrides user
conf.pager = conf.pager && tty;
for (int i = offset; i < argc; i++) { for (int i = offset; i < argc; i++) {
if (*argv[i] == '-') { if (*argv[i] == '-') {
if (conf.has_read_stdin) if (conf.has_read_stdin)
@ -117,9 +141,8 @@ int main(int argc, char *argv[]) {
die(argv[i]); die(argv[i]);
run(fp, argv[i], tty); run(fp, argv[i], tty);
fclose(fp); fclose(fp);
if (tty && (i + 1 != argc)) { if (tty && (i + 1 != argc)) {
fprintf(stderr, "\r\n"); // separate concurrent files in tty fprintf(err, "\r\n"); // separate concurrent files in tty
} }
} }