Prevent fork nightmare with PAGER=batcat (#2235)

* Added rsamuelklatchko's changes

* Added some comments and deleted redundant code

* Ran cargo fmt

* Update src/pager.rs

Co-authored-by: Martin Nordholts <enselic@gmail.com>

* Added bugfix to changelog

* src/pager.rs nitpick: arg0 -> s

I forgot to comment on this name so I figured I'd just push a commit to
take care of it.

Co-authored-by: Martin Nordholts <enselic@gmail.com>
This commit is contained in:
John Higgins 2022-08-12 05:27:47 -07:00 committed by GitHub
parent c4d9d7561b
commit 7b2e0ece55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -6,6 +6,8 @@
## Bugfixes
- Prevent fork nightmare with `PAGER=batcat`. See #2235 (@johnmatthiggins)
## Other
- Relaxed glibc requirements on amd64, see #2106 and #2194 (@sharkdp)

View File

@ -40,15 +40,23 @@ impl PagerKind {
fn from_bin(bin: &str) -> PagerKind {
use std::path::Path;
match Path::new(bin)
.file_stem()
.map(|s| s.to_string_lossy())
.as_deref()
{
Some("bat") => PagerKind::Bat,
// Set to `less` by default on most Linux distros.
let pager_bin = Path::new(bin).file_stem();
// The name of the current running binary. Normally `bat` but sometimes
// `batcat` for compatibility reasons.
let current_bin = env::args_os().next();
// Check if the current running binary is set to be our pager.
let is_current_bin_pager = current_bin
.map(|s| Path::new(&s).file_stem() == pager_bin)
.unwrap_or(false);
match pager_bin.map(|s| s.to_string_lossy()).as_deref() {
Some("less") => PagerKind::Less,
Some("more") => PagerKind::More,
Some("most") => PagerKind::Most,
_ if is_current_bin_pager => PagerKind::Bat,
_ => PagerKind::Unknown,
}
}