sharkdp/bat

Add a way to pass arguments to the pager via a command line flag

cohml opened this issue · 4 comments

Let's say you set a custom pager like this:

BAT_PAGER="less --tabs 4 -RF"

Then behind the scenes, those arguments will be passed into the specified pager program when paginating bat. This therefore "hardcodes" the "default" behavior of your pager when using bat.

But sometimes it's useful to one-off modify this default pager behavior. For example, to preload a search query like less +/some\ string.

Currently, I think the only way to do this would be as follows:

BAT_PAGER="${BAT_PAGER} +/some\ string" bat ...

But that is quite painful and verbose, not to mention potentially brittle since it depends on the user remembering the exact value of their BAT_PAGER.

So what I propose therefore is a way to pass extra args via a command line argument, e.g.

bat --pager-opts="+/some\ string" ...

Related issues:

This is already possible through the --pager option :)

The only caveat is that you need to have $LESS set to support ANSI colour codes by default (e.g. export LESS="-R"), or else you'll see the escape sequences instead of colors.

$ bat src/printer.rs --pager='less --version'
less 643 (PCRE2 regular expressions)
Copyright (C) 1984-2023  Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Home page: https://greenwoodsoftware.com/less

You can even use arguments with spaces by quoting them:

bat src/printer.rs --pager='less "+/&mut OutputHandle"'

I'm not too keen on the idea of adding a separate --pager-opts option for this, though. It's the same number of characters to type --pager-opts='...' as it is to type --pager='less ...', so we would be making bat more complex without saving any keystrokes for the user in doing so.

But to be clear, this

bat src/printer.rs --pager='less "+/&mut OutputHandle"'

is not the same as this

BAT_PAGER="${BAT_PAGER} +/&mut OutputHandle" bat src/printer.rs

The former overwrites whatever you've set for BAT_PAGER. The latter - which is the use case for which I propose --pager-opts (we could probably find a clearer name) - would append to it.

If most people set BAT_PAGER to something super simple like less -RF, then sure, the value add of --pager-opts isn't huge. But I personally pimp out my BAT_PAGER, and surely I'm not the only one:

echo $BAT_PAGER
less --chop-long-lines --RAW-CONTROL-CHARS --header=1 --jump-target=3 --line-num-width=0 --LONG-PROMPT --no-number-headers --shift=4 --status-column --use-color --color=HGk --color=NGk

Using --pager as you propose would completely wipe out all of that careful customization.

So adding a way to basically add extra args onto the current value of BAT_PAGER would be a nice addition IMHO. Because as things stand, bat kind of locks you into your fixed pager settings with its all-or-nothing approach.

That is quite the customization, wow.

Are those options specifically for bat, or are they general options that you use with less? If it's the latter, I think you can probably get away with setting them through the LESS environment variable and adding extra options with --pager.

If they're only for bat, I can see how --pager-opts would save you a ton of trouble. I would be happy to make a PR for this feature after I work through my backlog, but if you need a solution now I could probably write up a quick wrapper function/script to add the argument before it's actually supported by bat.

TL;DR: They're less options, but I would wholeheartedly support that PR at any point in time because I think it would be of wider utility. I'd open one myself if I only knew Rust 😭

That is quite the customization, wow.

Haha, thanks. I'm quite proud of it :) less is amazing. (As is bat!)

To your question, those are less-specific options. But to me, less IS bat and vice-versa. I only ever invoke bat, and page basically everything. bat is essentially my pager.

My case is perhaps a bit extreme, I don't know. But I still think that more generally a nice case could be made for some way to pass arguments directly to the pager program via the bat API. Especially for people like me who use bat for basically everything and a powerful pager like less, such an option would de facto grandfather in all kinds of nice functionality into the bat experience.

As for this proposal, ...

I think you can probably get away with setting them through the LESS environment variable and adding extra options with --pager.

... the drawback there is that it would clobber the less program itself. There are select instances where one might want to use their pager program without any options at all. BAT_PAGER is perfect because it provides a way to load up as many pager customizations as you want, allowing a customized pager UX through bat yet leaving the pager program itself unmodified for when it's needed.

Thanks for the quick replies by the way!