Compare commits

...

19 Commits

Author SHA1 Message Date
f386be73b3 Backseat -e
Will do later if determined to be worth it
2023-05-18 10:02:21 -04:00
feed927d1c Revert extension 2023-05-18 10:01:14 -04:00
08abee1958 Prep for new extension system 2023-05-18 08:51:12 -04:00
e5398d744f Revert extension 2023-05-04 09:59:52 -04:00
8cbe2307f7 Add extension exec scaffolding 2023-05-03 09:22:02 -04:00
a7fec62499 Update README
- More correct
2023-05-02 09:38:25 -04:00
ac3f697cc4 Cleanup 2023-05-01 18:47:37 -04:00
74b880aa94 Fix typo 2023-05-01 18:43:20 -04:00
d8265220e9 Update README 2023-05-01 18:39:27 -04:00
f0988a8a83 Start implementing -e
- extensions
2023-05-01 18:13:13 -04:00
216682498d Refactor codebase 2023-05-01 18:13:13 -04:00
d5c3259225 Fix memory leak and NULL-check error 2023-05-01 18:07:53 -04:00
7e24ad39b1 Cleanup
- Cleanup unneeded headers
- Fix helptext typos/bugs
- add noreturn to die(), and equiv
2023-04-26 18:05:13 -04:00
77c2d0ea4a Bump version 2023-04-22 15:37:13 -04:00
d1bbadfbd6 Update README
- QOL for *.tape files
2023-04-22 15:33:08 -04:00
69cf5566cf Move to getopt
- everything is single-letter now
2023-04-20 20:31:21 -04:00
ea97af4f12 Socials 2023-04-20 13:45:57 -04:00
07c1fe51fb Create c-cpp.yml 2023-04-20 13:35:55 -04:00
6096052962 Add social 2023-04-20 10:33:28 -04:00
22 changed files with 640 additions and 171 deletions

21
.github/workflows/c-cpp.yml vendored Normal file
View File

@ -0,0 +1,21 @@
name: Build
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: prep
run: make prep
- name: make
run: make
- name: test
run: ./build/lat -V

1
.gitignore vendored
View File

@ -131,3 +131,4 @@ $RECYCLE.BIN/
# End of https://www.toptal.com/developers/gitignore/api/c,macos,windows,linux,c
build/

23
LICENSE Normal file
View File

@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View File

@ -6,7 +6,11 @@ ODIR:=obj
BINDIR:=build
CC:=cc
CFLAGS:=-I$(IDIR) -Wall -Wextra -pedantic -Ofast
# OMG SO FAST (see https://www.shlomifish.org/humour/by-others/funroll-loops/Gentoo-is-Rice.html)
# CFLAGS:=-I$(IDIR) -Wall -Wextra -pedantic -Ofast -faggressive-loop-optimizations -funroll-all-loops -march=native
# For a stable experience
CFLAGS:=-I$(IDIR) -Wall -Wextra -pedantic -O2 -march=native
LIB:=
DEPS:=$($(IDIR)/%.h)
@ -22,7 +26,6 @@ $(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(LIB)
$(NAME): $(OBJ)
@echo "--don't forget to set $(NAME)'s version--"
$(CC) -o $(BINDIR)/$@ $^ $(CFLAGS) $(LIB)
.PHONY: prep

135
README.md Normal file
View File

@ -0,0 +1,135 @@
# lat
> lat | lazy cat - a cat clone with some quality-of-life embellishments
[![Build](https://github.com/secondary-smiles/lat/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/secondary-smiles/lat/actions/workflows/c-cpp.yml)
![lat demo](social/render/lat_demo.gif)
## About
`lat` is a solution to a problem that doesn't exist. It's the awkward middle child in-between `cat` and `bat`. It was created because the author didn't want to have to configure `bat` just to get decent file printing, but wanted a little more control than `cat` offered.
`lat` does not expect to be used. `lat` expects to be forgotten and shunted into a corner because it does not belong in the slightest.
`lat` is fast. Here are the hyperfine results comparing `cat`, `lat`, and `bat` on a `lat`'s `main.c` source file:
```text
hyperfine "./build/lat src/main.c" "cat src/main.c" "bat src/main.c" -N --warmup 200
Benchmark 1: ./build/lat src/main.c
Time (mean ± σ): 0.2 ms ± 0.0 ms [User: 0.1 ms, System: 0.0 ms]
Range (min … max): 0.1 ms … 0.7 ms 19651 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
Benchmark 2: cat src/main.c
Time (mean ± σ): 0.2 ms ± 0.0 ms [User: 0.2 ms, System: 0.0 ms]
Range (min … max): 0.2 ms … 0.4 ms 13717 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
Benchmark 3: bat src/main.c
Time (mean ± σ): 1.4 ms ± 0.7 ms [User: 1.1 ms, System: 0.3 ms]
Range (min … max): 1.1 ms … 6.5 ms 1057 runs
Warning: The first benchmarking run for this command was significantly slower than the rest (2.8 ms). This could be caused by (filesystem) caches that were not filled until after the first run. You are already using the '--warmup' option which helps to fill these caches before the actual benchmark. You can either try to increase the warmup count further or re-run this benchmark on a quiet system in case it was a random outlier. Alternatively, consider using the '--prepare' option to clear the caches before each timing run.
Summary
'./build/lat src/main.c' ran
1.29 ± 0.28 times faster than 'cat src/main.c'
9.14 ± 4.56 times faster than 'bat src/main.c'
```
~~as you can see, the extra features do take their toll on performance, but overall, `lat` is generally nearly on-par with `cat`, and *far* faster than `bat`.~~
**As of v0.12.2 `lat` is the new fastest of the three!** (on my machine and with reasonably sized files)
## Install
`lat` is not yet at a stable version (`v1.0.0`), so it is not on any package managers.
```bash
git clone https://github.com/secondary-smiles/lat.git
cd lat
make prep
make
./build/lat -V
# now add lat to your $PATH/do whatever you want with the binary
```
## Use
`lat` can, for the most part, be a direct drop in for `cat`. However it really shines when embedded into another program. For example, in `fzf`, `lat` makes an excellent viewer with the command `fzf --command lat -r {}`.
![Using lat in fzf](social/render/fzf_embed.gif)
Soon, I plan to add the ability to live-write to `lat`. That means that `lat` can become yet another output stream.
For example, this should be possible in a future version of `lat`
```c
int main(void) {
FILE *st = popen("lat -r", "w");
if (st == NULL)
exit(1);
fprintf(st, "look ma, i'm formatted!")
pclose(st);
}
```
> UPDATE: this feature is possible, however the data is only printed upon `pclose`.
### Features and Flags
#### `-c` color
Completely disables or enables all colored output from `lat`.
#### `-l` line numbers
Print numbers for each line of the file.
#### `-t` file title
Shows or hides formatted file data headers.
#### `-b` binary mode
By default, `lat` will attempt to detect if the file is printable or not. If the file isn't (e.g. you ran `lat file.pdf`) then lat will enter *`binary mode`*. In *`binary mode`*, `lat` will skip a lot of processing in favor of speed.
You can force *`binary mode`* to be `on` or `off` with the flags `-b` and `-bb` respectively.
#### `-r` raw output
`lat` is smart enough to print all non-file characters to a separate filestream. That way, the output of `lat` can be used to con**cat**enate files, the way it was originally meant to be (with `>` or `|`).
However, sometimes you want those extra symbols.
`-r` prints everything out to the primary filestream (usually `stdout`)
#### `-p` pager
`lat` comes with out-of-the-box support for paging in `less`. If you don't want your file messing up your terminal, just `-p` it.
### `-n` name
`lat` allows you to customize the name of the file shown. This can be useful in demonstrations or when `lat` is embedded in another program.
#### If there is a feature you'd like to see, feel free to make an issue (1x points). If you're feeling especially savvy, make a PR with the feature (10x points).
## Helptext
> `lat -h`
```text
lat | lazy cat - a cat clone with some quality-of-life embellishments
usage: lat [-cltbrpneVh] [file...]
options:
-c toggle color
-l toggle line numbers
-t toggle file info headers
-b set binary mode, -b forces binary and -bb forces NOT binary
-r print everything (headers, line numbers, etc.) to stdout (or equivalent)
-p print file with the pager (uses less)
-n <name> manually set the name of the file shown in the title
-e <program> link extension to lat
-V show program version
-h display this help text
environment:
NO_COLOR, see https://no-color.org/
```

View File

@ -2,10 +2,10 @@
#define ARG_H
#include <stdbool.h>
#define LAT_VERSION "0.11.1"
#define LAT_VERSION "0.12.4"
struct config {
bool stdin;
bool isstdin;
bool process;
bool color;
bool lines;
@ -13,6 +13,8 @@ struct config {
int force_binary;
bool literal;
bool pager;
char *name;
char *extension;
bool has_read_stdin;
};

54
social/binary.tape Normal file
View File

@ -0,0 +1,54 @@
Require lat
Output social/render/binary.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat src/main.c"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -b src/main.c"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat ./build/lat"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -bb ./build/lat"
Sleep 2s
Enter
Sleep 5s

31
social/color.tape Normal file
View File

@ -0,0 +1,31 @@
Require lat
Output social/render/color.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat src/main.c"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -c src/main.c"
Sleep 2s
Enter
Sleep 5s

41
social/fzf_embed.tape Normal file
View File

@ -0,0 +1,41 @@
Require lat
Output social/render/fzf_embed.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "fzf --preview 'lat -r {}'"
Sleep 1s
Enter
Sleep 2s
Type "main.c"
Sleep 2s
Backspace 7
Sleep 1s
Type "types.h"
Sleep 1s
Backspace 8
Sleep 2s
Type "file.c"
Sleep 3s
Backspace 7
Sleep 2s
Up@3s 15
Sleep 3s
Enter
Sleep 2s

32
social/headers.tape Normal file
View File

@ -0,0 +1,32 @@
Require lat
Output social/render/headers.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat src/main.c"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -t src/main.c"
Sleep 2s
Enter
Sleep 5s

24
social/lat_demo.tape Normal file
View File

@ -0,0 +1,24 @@
Require lat
Output social/render/lat_demo.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 2000
Set TypingSpeed 0.2
Type "lat Makefile"
Sleep 2s
Enter
Sleep 4s
Type "lat -h"
Sleep 2s
Enter
Sleep 4s

30
social/line_numbers.tape Normal file
View File

@ -0,0 +1,30 @@
Require lat
Output social/render/line_numbers.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat src/main.c"
Sleep 2s
Enter
Sleep 4s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -n src/main.c"
Sleep 2s
Enter
Sleep 4s

46
social/literal.tape Normal file
View File

@ -0,0 +1,46 @@
Require lat
Output social/render/literal.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat src/main.c > example.c"
Sleep 2s
Enter
Sleep 5s
Type "cat example.c"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -r src/main.c > example.c"
Sleep 2s
Enter
Sleep 3s
Type "cat example.c"
Sleep 2s
Enter
Sleep 5s
Hide
Type "rm example.c"
Enter

37
social/name.tape Normal file
View File

@ -0,0 +1,37 @@
Require lat
Output social/render/name.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat"
Sleep 2s
Enter
Type "hello, world!"
Enter
Sleep 3s
Ctrl+d
Sleep 3s
Type "lat -n "custom name" src/main.c"
Sleep 2s
Enter
Type "hello, world!"
Enter
Sleep 3s
Ctrl+d
Sleep 4s

40
social/pager.tape Normal file
View File

@ -0,0 +1,40 @@
Require lat
Output social/render/pager.gif
Set Theme "Gruvbox Dark"
Set WindowBar Rings
Set BorderRadius 10
Set Margin 10
Set FontSize 32
Set Width 2400
Set Height 1200
Set TypingSpeed 0.2
Type "lat src/main.c"
Sleep 2s
Enter
Sleep 5s
Type "clear"
Sleep 1s
Enter
Sleep 2s
Type "lat -p src/main.c"
Sleep 2s
Enter
Sleep 5s
Down 100
Sleep 3s
Type "q"
Sleep 4s

BIN
social/render/fzf_embed.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

BIN
social/render/lat_demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 662 KiB

View File

@ -1,11 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdnoreturn.h>
#include <unistd.h>
#include "arg.h"
#include "util.h"
#define LAT_USAGE "usage: lat [-cntblpVh] [file...]"
#define LAT_SHORT_ARGS "cltbrpn:e:Vh"
#define LAT_USAGE "usage: lat [-cltbrpneVh] [file...]"
struct config conf;
void help(void) {
printf("lat | lazy cat - a cat clone with some quality-of-life "
@ -13,113 +17,40 @@ void help(void) {
printf("%s\n\n", LAT_USAGE);
printf("options:\n"
"\t-c, --color\t toggle color\n"
"\t-n, --lines\t toggle line numbers\n"
"\t-t, --headers\t toggle file info headers\n"
"\t-b, --binary\t toggle binary mode, -b forces binary and -bb forces "
"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-V, --version\t show program version\n"
"\t-h, --help\t display this help text (--help shows additional "
"info)\n\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> NONFUNCTIONAL (will be added soon) 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\n");
}
void examples(void) {
printf(
"examples:\n"
"\tlat file1\n\t\t print the contents of file1 with the default "
"formatting\n"
"\tlat - file1\n\t\t read from stdin (the '-' flag reads from "
"stdin) "
"and then print the contents of stdin and file1\n"
"\tlat -nc file1 file2\n\t\t print the contents of file1 and "
"file2 "
"without printing line numbers or colors\n"
"\tlat --binary file.txt\n\t\t force file.txt to be treated as a binary "
"file\n"
"\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"
"\tcurl example.com | lat\n\t\t pipe the results of 'curl example.com' "
"into lat\n"
"\tfzf --preview 'lat -l {}'\n\t\t use lat as the file viewer in fzf\n"
);
"\tNO_COLOR, see https://no-color.org/\n");
}
void version(void) {
printf("lat - v%s built %s at %s\n", LAT_VERSION, __DATE__, __TIME__);
}
struct config conf;
void argerr(char *r, char *arg) {
printf("lat: %s '%s'\n\n", r, arg);
printf("%s\n", LAT_USAGE);
printf("run '--help' for more information\n");
noreturn void argerr(void) {
printf("\n%s\n", LAT_USAGE);
printf("run '-h' for more information\n");
exit(EXIT_FAILURE);
}
void parselongarg(char *arg) {
if (strcmp(arg, "--color") == 0) {
conf.color = !conf.color;
return;
}
if (strcmp(arg, "--lines") == 0) {
conf.lines = !conf.lines;
return;
}
if (strcmp(arg, "--headers") == 0) {
conf.headers = !conf.headers;
return;
}
if (strcmp(arg, "--binary") == 0) {
if (conf.force_binary < 0)
conf.force_binary = 1;
else
conf.force_binary = !conf.force_binary;
return;
}
if (strcmp(arg, "--literal") == 0) {
conf.literal = !conf.literal;
return;
}
if (strcmp(arg, "--pager") == 0) {
conf.pager = !conf.pager;
return;
}
if (strcmp(arg, "--help") == 0) {
help();
examples();
exit(EXIT_SUCCESS);
return;
}
if (strcmp(arg, "--version") == 0) {
version();
exit(EXIT_SUCCESS);
return;
}
argerr("unrecognized arg", arg);
}
void parseshortarg(char *arg) {
size_t i = 1;
while (arg[i] != '\0') {
char c = arg[i];
switch (c) {
int parseargs(int argc, char *argv[]) {
char opt;
while ((opt = getopt(argc, argv, LAT_SHORT_ARGS)) != -1) {
switch (opt) {
case 'c':
conf.color = !conf.color;
break;
case 'n':
case 'l':
conf.lines = !conf.lines;
break;
case 't':
@ -131,12 +62,18 @@ void parseshortarg(char *arg) {
else
conf.force_binary = !conf.force_binary;
break;
case 'l':
case 'r':
conf.literal = !conf.literal;
break;
case 'p':
conf.pager = !conf.pager;
break;
case 'n':
conf.name = optarg;
break;
case 'e':
conf.extension = optarg;
break;
case 'V':
version();
exit(EXIT_SUCCESS);
@ -145,39 +82,10 @@ void parseshortarg(char *arg) {
help();
exit(EXIT_SUCCESS);
break;
default: {
char *str = malloc(2);
str[0] = c;
str[1] = '\0';
argerr("unrecognized flag", str);
free(str);
default:
argerr();
break;
}
}
i++;
}
}
int parseargs(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; i++) { // offset for argv[0]
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '\0') {
return i;
}
if (arg[0] == '-') {
if (arg[1] == '-') {
parselongarg(arg);
continue;
}
parseshortarg(arg);
} else {
return i;
}
}
return i;
return optind;
}

View File

@ -4,6 +4,7 @@
#include "types.h"
#include "util.h"
#include "arg.h"
bool isbinary(struct filedata *f) {
@ -22,7 +23,7 @@ bool isbinary(struct filedata *f) {
}
}
struct filedata readfile(FILE *fp, bool isstdin) {
struct filedata readfile(FILE *fp) {
struct filedata f;
f.lc = 0;
@ -32,7 +33,7 @@ struct filedata readfile(FILE *fp, bool isstdin) {
f.buf = NULL;
f.lines = NULL;
if (isstdin) {
if (conf.isstdin) {
size_t bufsize = 1024;
f.buf = malloc(bufsize);
if (f.buf == NULL)
@ -42,7 +43,6 @@ struct filedata readfile(FILE *fp, bool isstdin) {
while (fread(&c, 1, 1, fp) > 0) {
if (f.buflen == bufsize - 1) {
bufsize *= 2;
char *new_buf = realloc(f.buf, bufsize);
if (new_buf == NULL)
die("realloc");

View File

@ -1,12 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "types.h"
#include "util.h"
void appendline(struct filedata *f, char *data, size_t len) {
f->lines = realloc(f->lines, sizeof(struct line) * (f->lc + 1));
if (f->lines == NULL)
die("realloc");
size_t loc = f->lc;
@ -20,7 +23,7 @@ void loadlines(struct filedata *f) {
f->lc = 0;
size_t offset = 0;
size_t linelen = 4096;
size_t linelen = 1024;
char *line = malloc(linelen);
if (line == NULL)
die("malloc");
@ -28,10 +31,11 @@ void loadlines(struct filedata *f) {
for (size_t i = 0; i < f->buflen; i++) {
char c = f->buf[i];
if (c == '\n') {
if (offset < linelen) { // shrink
if (offset >= 1 && offset < linelen) { // shrink to fit
char *new_line = realloc(line, offset);
if (new_line == NULL)
die("realloc");
line = new_line;
}
@ -59,7 +63,7 @@ void loadlines(struct filedata *f) {
}
// capture last line
if (offset < linelen) { // shrink
if (offset >= 1 && offset < linelen) { // shrink
char *new_line = realloc(line, offset);
if (new_line == NULL)
die("realloc");

View File

@ -1,8 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdnoreturn.h>
void die(const char *message) {
noreturn void die(const char *message) {
perror(message);
exit(EXIT_FAILURE);
}

View File

@ -1,7 +1,5 @@
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "arg.h"
@ -18,13 +16,45 @@
FILE *st;
FILE *err;
struct colors {
char *invert_t;
char *grey;
char *reset;
};
struct colors c;
void initcolor(void) {
c.invert_t = conf.color ? INVERT_T : "";
c.grey = conf.color ? GREY : "";
c.reset = conf.color ? RESET : "";
}
void printheadertop(char *filename, bool binary) {
char *name = conf.name == NULL ? filename : conf.name;
char *addon = binary ? "<binary>" : "";
if (conf.isstdin && !conf.pager)
fprintf(err, "\x1b[2K\r%s%s%s%s\n", c.invert_t, name, addon, c.reset);
else
fprintf(err, "%s%s%s%s\n", c.invert_t, name, addon, c.reset);
}
void printheaderbottom(size_t buflen) {
float rounded;
char *format = formatbytes(buflen, &rounded);
fprintf(err, "%s%.2f %s%s\n", c.invert_t, rounded, format, c.reset);
}
void run(FILE *fp, char *filename, bool tty) {
const char *invert_t = conf.color ? INVERT_T : "";
const char *grey = conf.color ? GREY : "";
const char *reset = conf.color ? RESET : "";
initcolor();
struct filedata f;
f = readfile(fp, conf.stdin);
f = readfile(fp, conf.isstdin);
if (conf.extension != NULL) {
// TODO
}
if (conf.pager) {
st = popen("less", "w");
@ -45,14 +75,10 @@ void run(FILE *fp, char *filename, bool tty) {
}
if (conf.headers) {
char *addon = f.binary ? "<binary>" : "";
if (conf.stdin && !conf.pager)
fprintf(err, "\x1b[2K\r%s%s%s%s\n", invert_t, basename(filename), addon,
reset);
else
fprintf(err, "%s%s%s%s\n", invert_t, basename(filename), addon, reset);
printheadertop(filename, f.binary);
}
// any/all processing to be done
conf.process = (tty && !f.binary);
if (conf.process) { // file display processing
loadlines(&f);
@ -62,11 +88,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 ", grey, padding, i + 1, 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);
@ -76,18 +105,17 @@ 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);
fflush(st); // prevent timing inconsistencies between st and err
fflush(err);
if (conf.headers) {
float rounded;
char *format = formatbytes(f.buflen, &rounded);
fprintf(err, "%s%.2f %s%s\n", invert_t, rounded, format, reset);
printheaderbottom(f.buflen);
}
if (conf.pager) {
@ -97,14 +125,18 @@ void run(FILE *fp, char *filename, bool tty) {
void initconf(void) {
conf.force_binary = -1;
conf.stdin = false;
conf.isstdin = false;
conf.has_read_stdin = false;
conf.pager = false;
conf.literal = false;
conf.process = true;
conf.headers = true;
conf.color = true;
conf.lines = true;
conf.name = NULL;
conf.extension = NULL;
}
void clearstdin(void) {
@ -128,40 +160,45 @@ int main(int argc, char *argv[]) {
bool tty = isatty(STDOUT_FILENO);
int offset = parseargs(argc, argv);
conf.headers = conf.headers && tty;
if (argc > 1) {
int offset = parseargs(argc, argv);
tty = tty || conf.literal;
conf.headers = conf.headers && tty; // tty still overrides user
conf.pager = conf.pager && tty;
for (int i = offset; i < argc; i++) {
if (*argv[i] == '-') {
if (conf.has_read_stdin)
clearstdin();
conf.has_read_stdin = true;
conf.stdin = true;
conf.isstdin = true;
run(stdin, "stdin", tty);
if (tty && (i + 1 != argc)) {
fprintf(err, "\n"); // separate concurrent files in tty
}
continue;
}
conf.stdin = false;
conf.isstdin = false;
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL)
die(argv[i]);
run(fp, argv[i], tty);
fclose(fp);
if (tty && (i + 1 != argc)) {
printf("offset: %d argc: %d\n", i, argc);
fprintf(err, "\n"); // separate concurrent files in tty
}
}
if (offset == argc) {
conf.stdin = true;
conf.isstdin = true;
run(stdin, "stdin", tty);
}
} else {
conf.stdin = true;
conf.isstdin = true;
run(stdin, "stdin", tty); // for piped-input or repl-like behavior
}