bat icon indicating copy to clipboard operation
bat copied to clipboard

Width is wrong when using less as pager with -J option

Open 234d opened this issue 8 years ago • 10 comments

The output from bat is one column to wide when less adds a status column to the left edge.

Setup touch file export BAT_PAGER

Compare BAT_PAGER="less -R" bat file

To BAT_PAGER="less -JR" bat file

234d avatar Oct 23 '18 15:10 234d

That is unfortunate, but I think it will be really difficult to handle this in bat. We currently rely on having the whole terminal width for our output. If less adds 2 columns for the status bar (-J) or line numbers (`-N?), .. we have less space effectively.

Several ideas/workarounds come to my mind:

  • bat has a hidden --terminal-width option that you can use to explicitly set the width of the terminal. If less is adding two columns, you can subtract two for bat in order to make it work:
    BAT_PAGER="less -RJ" bat --terminal-width=$((COLUMNS - 2)) file
    
    Unfortunately, you cannot add this to bats config file or use an alias, since $COLUMNS needs to be evaluated by the shell. A shell function would work, though.
  • You could turn of bats grid and also turn off character-level wrapping. This way, the output wouldn't be so bad because line wraps would be handled by less:
    BAT_PAGER="less -RJ" bat --style="numbers,changes,header" --wrap=never file
    
  • Maybe it would be possible to wrap less in a shell script that reports a terminal size of $COLUMNS - 2 to bat.

sharkdp avatar Oct 23 '18 19:10 sharkdp

I figured it wouldn't be an easy problem to fix but wanted to bring it up if others encounter it. And if there maybe was a workaround, which you presented a few. Thanks for that!

What about adding a "--terminal-width-offset" option? That way the offset could be adapted to what version of less is currently in use. Maybe it's not worth the clutter in the code since there are acceptable workarounds.

234d avatar Oct 24 '18 15:10 234d

What about adding a "--terminal-width-offset" option?

I thought about that, too. We could maybe just support something like --terminal-width=+5 or --terminal-width=-2? I don't think that would be very hard to implement.

sharkdp avatar Oct 24 '18 21:10 sharkdp

Oh, forgot to leave a reply. The ability to set an offset is awesome! Thanks for a great tool and all the feedback!

234d avatar Oct 31 '18 21:10 234d

This is now available in bat 0.9.

sharkdp avatar Nov 11 '18 13:11 sharkdp

this is a simple issue but can be a bit tricky to track, took me forever to understand what was wrong here:

Bildschirmfoto vom 2021-07-31 21-20-56

I solved it by adding --pager=less -+J on ~/.config/bat/config . This overrides the $LESS envvar. Since bat --generate-config-file already suggests some less(1) options, I think it could be a nice idea to have this as a default? (in fact the other options were already in my $LESS, my bat --pager now consists of less -+J.

melissaboiko avatar Jul 31 '21 19:07 melissaboiko

@melissaboiko I don't quite understand. #392 proposes another solution to this problem. And I don't know what -+J does?

sharkdp avatar Aug 02 '21 20:08 sharkdp

Funny less(1) syntax but -+J disables the -J (or --+status-column also works), overriding the $LESS environment variable .

The J option is the one causing trouble, so if somebody has -J on their $LESS, but "less -+J" on the --pager, the latter overrides the first, and less(1) doesn't mess up the columns when called from bat.

Of course, the offset option works too, and has the advantage of keeping up the less(1) status column if the person wants that inside bat.

Currently when you generate a sample config file there are these lines:


# Uncomment the following line if you are using less version >= 551 and want to
# enable mouse scrolling support in `bat` when running inside tmux. This might
# disable text selection, unless you press shift.
#--pager="less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse"

I think similar comments could be helpful here, like

# Uncomment the following line if you use the less(1) -J/--status-column option
# (either on --pager or via $LESS), to allow space for the status column:
# --terminal-width=-2
#
# (Or disable the less(1) status column in bat by adding --+status-column to --pager).

It's a bit dirty coupling but conceivably we could even peek into --pager/$PAGER/$LESS and if less -J / less --status-column is detected, do terminal width -2 by default… I mean if you're into that kind of thing.

melissaboiko avatar Aug 03 '21 09:08 melissaboiko

I think similar comments could be helpful here, like […]

That sounds great!

It's a bit dirty coupling but conceivably we could even peek into --pager/$PAGER/$LESS and if less -J / less --status-column is detected, do terminal width -2 by default… I mean if you're into that kind of thing.

I kind of like this :smile:. But maybe it's a bit too much magic and would come with new problems.

sharkdp avatar Aug 07 '21 17:08 sharkdp

I also just struggled with this issue. Took quite a while to debug. When less -J... is used as the pager for bat is the behavior always wrong? Suggestion: It could be worth having bat output a warning message to stderr when it detects 1 character column misalignment and and less -J...is being used as the pager. Though not sure if that's possible.

Once I finally figured out the problem and found this issue it was pretty easy to fix it in my bat config thanks to the handy --terminal-width="-1" option mentioned here.

varenc avatar Feb 13 '23 04:02 varenc