191 points · by Olshansky · 1y ago · 95 comments
Like rg, it's one of those "rewrite it in rust" projects that turned out to actually be quite well thought through.
The reason “rewrite in Rust” gets a lot of hate is because it’s often a poorer replica in that it supports a subset of features and sometimes so littered with unsafe blocks that it’s barely any safer than their C counterparts.
Ripgrep and bat are the exceptions in that they’re modernisations in every sense of the term. They’re safer, have more modern features, and better defaults. Even if you don’t give a crap about memory safety, there’s a reason to use ripgrep and bat.
Personally I really don’t see the benefit in rewriting stuff unless you’re bringing other modernisations to the table too. But I suspect for some people, it’s more of an exercise to learn Rust than it is an ambition to displace a particular coreutil.
However what I truly don't understand is using a different license. For something so fundamental, please just let that be the same.
But I think there are a lot more great examples. I have used pandas and I like polars. I have used latex but I like typst. People are creating generally valuable tools that bring something new to the table. More competition and diversity is rarely a bad thing.
That's exactly what's needed in many cases tho: someone to drop most of the legacy features.
I'd say that rewriting anything in any language (even in the same language) would remove large amounts of cruft, and add long-missing neat things that are easier to add when you build from scratch, and with a good understanding which the original authors lacked. Often it also can afford using a better architecture, see rg vs grep: grep has many brilliant technical solutions, but making it multithreaded would be a major rewrite anyway.
Edit, with cURL it's OK… 200.
That makes no sense.
bat: A cat(1) Clone with Wings - https://news.ycombinator.com/item?id=33382307 - Oct 2022 (2 comments)
bat, a cat(1) clone with syntax highlighting, Git integration written in Rust - https://news.ycombinator.com/item?id=24850244 - Oct 2020 (6 comments)
Bat: A cat(1) clone with wings - https://news.ycombinator.com/item?id=17887819 - Aug 2018 (12 comments)
Bat: A cat(1) clone with wings - https://news.ycombinator.com/item?id=17849535 - Aug 2018 (1 comment)
Bat: cat(1) clone with syntax highlighting and Git integration - https://news.ycombinator.com/item?id=16968755 - May 2018 (1 comment)
I just downloaded it on MacOS and when I ran it the first time it took a really long time on a one-line JSON which disappointed me but then any subsequent run on anything was fast. I'd completely forgotten about MacOS doing that thing on first run.
Uses the pager for large files automatically etc. Very nice.
If you like that, you can also make less behave like cat if the file fits in one screen. You just need to add F to the LESS environment variable. It's very convenient because so many things depend on less.
https://www.man7.org/linux/man-pages/man1/less.1.html#:~:tex...
works fine for me.
I guess if all you did was read the headline of the post you could assume your alias does all the same things as bat.
> That shows non-printable characters like bat does?
cat does actually support that via the flags with -v (you can also use -t and -e to view tab and line endings too)
> That allows you to concatenate and page multiple files at once like bat does?
cat is literally called “cat” because it’s intended purpose is concatenation.
It’s not a pager though the GPs example did pipe to less anyway.
> That supports the --line-range option like bat does?
‘tail’ and ‘head’ would be muscle memory to a lot of people and not that different in terms of number of keystrokes.
But I do take your point that it’s nice to have that built into your pager.
> You can pipe the output of tail -f through your alias?
I couldn’t see why not. tail -f isn’t doing anything weird with the fd.
———
I’m not arguing against using bat though. I have it aliased to cat on my own machines, so I clearly and would recommend bat. But I do think some people might be surprised how far you can get with coreutils if bat wasn’t available
cat's behaviour and bat's behaviour is different, though.
>cat a.txt b.txt
It was a dark and stormy night.
Once upon a time.
>bat a.txt b.txt
───────┬──────────────────────────────────────────────────────
│ File: a.txt
───────┼──────────────────────────────────────────────────────
1 │ It was a dark and stormy night.
───────┴──────────────────────────────────────────────────────
───────┬──────────────────────────────────────────────────────
│ File: b.txt
───────┼──────────────────────────────────────────────────────
1 │ Once upon a time.
───────┴──────────────────────────────────────────────────────
This difference becomes more useful once we have a more meaningful example: >cat *.py
(thousands of lines of output)
>bat -r :5 -H 2 --style full *.py
───────┬──────────────────────────────────────────────────────
│ File: __init__.py <EMPTY>
│ Size: 0 B
───────┴──────────────────────────────────────────────────────
───────┬──────────────────────────────────────────────────────
│ File: editor.py
│ Size: 2.4 KB
───────┼──────────────────────────────────────────────────────
1 │ import collections
2 │ import contextlib
3 │ import glob
4 │ import io
5 │ import os.path
───────┴──────────────────────────────────────────────────────
It's hard to imagine many people have the muscle memory for the combination of cat, head, and whatever else you need to add headers with the filename and file size, call out empty files, highlight the second line, show line numbers, do syntax formatting, and wrap to the terminal width (head doesn't do this).I also use the fzf previewer with --range-limited pretty frequently.
Your solution would be ok with an alias as well, so thanks. Might try it just so I dont need yet another program lying around
Whether this proves or disproves anything in re syntax highlighting utilities I leave as an exercise.
cat .bashrc | highlight --force -O xterm256 | less -SRNI
everything is green, except line numbers are black (which comes from less), but bat .bashrc
shows actual syntax highlighting.so apparently, no, it doesn't work.
to be fair, this is how it works:
cat .bashrc | highlight --syntax shellscript -O ansi | less -R
to avoid getting caught by the useless use of cat police, this does too: highlight --syntax shellscript -O ansi .bashrc | less -R
however, i have to tell it which syntax to usebut to its credit, highlight even has support for pike, which bat doesn't (yet) (fixed that for myself, at least)
so overall, bat wins.
so really bat and hightlight are equal, and it's not just a useless use of cat, but using cat here actually breaks the syntax detection. and it does so in bat too, obviously.
so this means highlight almost wins because it has pike support already, whereas for bat i had to add it, except that it turns out that if highlight can't detect the syntax it produces nothing, and you need --force to fix that, and if it is given multiple files as arguments it writes the output to files too, which is practically never what i want so i need to fix that with --stdout.
bat it is.
This will live in my .bashrc for a long time:
cat() {
if [[ -t 1 ]]; then
command cat "$@" | highlight --force -O xterm256
else
# plain cat to pipe into other things
command cat "$@"
fi
}i'd change the third line so you can actually get syntax highlighting:
command highlight --stdout --force -O xterm256 "$@" highlight --force -O xterm256 < whatever | less -SRNI* https://freshports.org/textproc/highlight/
* https://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/textproc/hi...