Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
234d opened this issue Oct 23, 2018 · 10 comments · Fixed by #392
Closed

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

234d opened this issue Oct 23, 2018 · 10 comments · Fixed by #392
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@234d
Copy link

234d commented Oct 23, 2018

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

@sharkdp
Copy link
Owner

sharkdp commented Oct 23, 2018

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 sharkdp added help wanted Extra attention is needed question Further information is requested labels Oct 23, 2018
@234d
Copy link
Author

234d commented Oct 24, 2018

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.

@sharkdp
Copy link
Owner

sharkdp commented Oct 24, 2018

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 added a commit that referenced this issue Oct 31, 2018
Allows the `width` argument to `--terminal-width` to be an offset
instead of an absolute number. Examples:

    --terminal-width=80   # Set output width to 80 characters
    --terminal-width=-2   # Set output width to actual_width - 2

closes #376
sharkdp added a commit that referenced this issue Oct 31, 2018
Allows the `width` argument to `--terminal-width` to be an offset
instead of an absolute number. Examples:

    --terminal-width=80   # Set output width to 80 characters
    --terminal-width=-2   # Set output width to actual_width - 2

closes #376
@234d
Copy link
Author

234d commented Oct 31, 2018

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

@sharkdp
Copy link
Owner

sharkdp commented Nov 11, 2018

This is now available in bat 0.9.

@melissaboiko
Copy link

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.

@sharkdp
Copy link
Owner

sharkdp commented Aug 2, 2021

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

@melissaboiko
Copy link

melissaboiko commented Aug 3, 2021

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.

@sharkdp
Copy link
Owner

sharkdp commented Aug 7, 2021

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 😄. But maybe it's a bit too much magic and would come with new problems.

@varenc
Copy link

varenc commented Feb 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants