bat sould use pager name from PAGER environment variable, and only use less if PAGER is unset.
Good idea!
How would we handle command line arguments to the pager? Currently, we pass --RAW-CONTROL-CHARS, --no-init and (optionally) --quit-if-one-screen. We cannot expect other pagers to have the same command line options.
I guess the only option would be to pass no command-line arguments (can/should command-line arguments be specified in $PAGER?) if $PAGER is set. Also, we should check if $PAGER == "less" and fall back to using the normal command-line aguments.
I think, support should be added for each pager in bat, and fall back to no arguments if pager isn't supported.
For example, the slit pager parses the 16 base colors by default (there is a feature request for xterm-256 colors) , and don't launch the curses interface if output fits in one screen, only prints it.
bat should add a command line argument to customize the pager first.
Only after that, we can discuss whether to support the environment variable PAGER.
And, we can also support a different environment variable, like BAT_PAGER.
it is necessary, because sometimes we just want to specify pager for bat.
@moonfruit PAGER seems to be supported by a lot of programs (man, git, mysql, .. to name a few). I think it's perfectly fine to support PAGER first and add overrides later, if we feel we need to.
@sharkdp You are right about that many programs support PAGER, but most of them also support another way to specific pager (command line argument like man and mysqlor configuration file like git).
If I want to use a different pager for bat other than system pager, I cannot do it in case of that bat only support PAGER environment variable.
git also has the GIT_PAGER environment variable. I think this few lines of code shouldn't be a big problem.
I think a reasonable detection would follow:
BAT_PAGER if presentPAGER if present (optionally: skip this if --no-detect-pager command-line flag present)less with bat-optimised flagsIn either case that we use an environment variable, it would be convenient to support including flags for the pager (e.g., my personal PAGER is less --no-init --quit-if-one-screen --chop-long-lines which works well with man and other well-behaved posix built-ins, and it would be inconvenient if bat did not support flags)
I think we could try to support command-line arguments by splitting the contents of $PAGER by whitespace. This would, however, fail for more elaborate commands like
PAGER="my-pager --foo='complex argument'"
For those looking for a temporary alternative, I'm using an alias as follows. In my case, I wanted to remove the "-X" flag to less so that the contents of a long file are not kept on my screen after I quit the pager, but you can replace the less --tabs 4 -RFX part with the pager and pager arguments of your choice.
if command -v bat >/dev/null ; then
BAT_PATH=$(which bat)
function bat() {
if [ -t 1 ] ; then
$BAT_PATH --color always --style full --paging never $* \
| less --tabs 4 -RF
else
$BAT_PATH $*
fi
}
fi
I have implemented a first version of this in #192. It'd be great if someone could review this.
Released in bat 0.5.
I am running 0.6.0 and I am still not able to scroll in iTerm. Is there specific flag or something that I need to set?
I'm using the default Terminal app in Mac, and the suggestion from @achalddave in the above comment didn't work for me.
I modified the bash function to use RS instead of RF, and was able to get mouse scrolling to work:
if command -v bat >/dev/null ; then
BAT_PATH=$(which bat)
function bat() {
if [ -t 1 ] ; then
$BAT_PATH --color always --style full --paging never $* \
| less --tabs 4 -RS
else
$BAT_PATH $*
fi
}
fi
You can trick bat into passing no arguments to the pager (particularly less) by setting BAT_PAGER="less --". This trick works well even for small files as an altscreen will be created.
How would we handle command line arguments to the pager? Currently, we pass
--RAW-CONTROL-CHARS,--no-initand (optionally)--quit-if-one-screen. We cannot expect other pagers to have the same command line options.
less respects the environment variable LESS, so you could set that (if it's not already set) to -RXF and just execute $PAGER. That's what git does.
This issue is quite old. In the meantime we have a pager handling which is quite robust, I believe.
We actually do interpret the command-line arguments in PAGER, but we make sure to always enable -R:
https://github.com/sharkdp/bat/blob/e9210c0f6c390ccffe72a5461c57b8348ceb4a48/src/output.rs#L31-L44
Most helpful comment
I think a reasonable detection would follow:
BAT_PAGERif presentPAGERif present (optionally: skip this if--no-detect-pagercommand-line flag present)lesswith bat-optimised flagsIn either case that we use an environment variable, it would be convenient to support including flags for the pager (e.g., my personal
PAGERisless --no-init --quit-if-one-screen --chop-long-lineswhich works well withmanand other well-behaved posix built-ins, and it would be inconvenient if bat did not support flags)