sharkdp / bat

A cat(1) clone with wings.
Apache License 2.0
49.74k stars 1.25k forks source link

bat Incorrectly using pager on small files #2832

Open Hashino opened 10 months ago

Hashino commented 10 months ago

What steps will reproduce the bug?

  1. use bat on any file

What happens?

bat will always use the pager, regardless of file content.

https://github.com/sharkdp/bat/assets/5542633/478940e8-dbfa-4dd9-bd6c-67cb608b4629

What did you expect to happen instead?

for the text to be displayed in the terminal without the pager when the file content fitss, like it happens when i use --paging=never

How did you install bat?

sudo pacman -Syu bat


bat version and environment

~ > yay -Qi bat
Name            : bat
Version         : 0.24.0-1
Description     : Cat clone with syntax highlighting and git integration
Architecture    : x86_64
URL             : https://github.com/sharkdp/bat
Licenses        : APACHE  MIT
Groups          : None
Provides        : None
Depends On      : libgit2  oniguruma
Optional Deps   : None
Required By     : bat-extras
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 4.90 MiB
Packager        : Alexander F. Rødseth <xyproto@archlinux.org>
Build Date      : Sun 15 Oct 2023 12:57:12 PM -03
Install Date    : Fri 12 Jan 2024 03:53:27 PM -03
Install Reason  : Explicitly installed
Install Script  : No
Validated By    : Signature

~ > bat .config/bat/config --paging=never
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: .config/bat/config
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ # This is `bat`s configuration file. Each line either contains a comment or
   2   │ # a command-line option that you want to pass to `bat` by default. You can
   3   │ # run `bat --help` to get a list of all possible configuration options.
   4   │ 
   5   │ # Specify desired highlighting theme (e.g. "TwoDark"). Run `bat --list-themes`
   6   │ # for a list of all available themes
   7   │ --theme="Nord"
   8   │ 
   9   │ # Enable this to use italic text on the terminal. This is not supported on all
  10   │ # terminal emulators (like tmux, by default):
  11   │ #--italic-text=always
  12   │ 
  13   │ # Uncomment the following line to disable automatic paging:
  14   │ #--paging=never
  15   │ 
  16   │ # Uncomment the following line if you are using less version >= 551 and want to
  17   │ # enable mouse scrolling support in `bat` when running inside tmux. This might
  18   │ # disable text selection, unless you press shift.
  19   │ #--pager="less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse"
  20   │ 
  21   │ # Syntax mappings: map a certain filename pattern to a language.
  22   │ #   Example 1: use the C++ syntax for Arduino .ino files
  23   │ #   Example 2: Use ".gitignore"-style highlighting for ".ignore" files
  24   │ #--map-syntax "*.ino:C++"
  25   │ #--map-syntax ".ignore:Git Ignore"
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
~ > pfetch 
       /\         hashino@dacompsi
      /  \        os      Arch Linux
     /\   \       kernel  6.6.10-zen1-1-zen
    /      \      wm      awesome
   /   ,,   \     shell   fish
  /   |  |  -\    memory  3029M / 7845M
 /_-''    ''-_\
erhhung commented 9 months ago

I noticed this behavior only if the BAT_PAGER environment variable is defined.

$ echo $PAGER
less -Ln
$ echo $BAT_PAGER
less -Ln

# shows file using less
$ bat one-line-file.txt

# cats file to terminal
$ unset BAT_PAGER
$ bat one-line-file.txt
THIS IS A ONE-LINE FILE

However, if I alter the BAT_PAGER variable so that it includes the -F option for less according the bat README, ("The second option (-F) instructs less to exit immediately if the output size is smaller than the vertical size of the terminal. This is convenient for small files because you do not have to press q to quit the pager.") everything works as expected!

$ export BAT_PAGER='less -RFLn'

# cats file to terminal
$ bat one-line-file.txt
THIS IS A ONE-LINE FILE

If you want to use another pager app, you'll have to see if it supports an option similar to less -F.

eth-p commented 9 months ago

Love the look of your WM.

Regarding the issue, @erhhung is correct. Unless asked not to, bat will always pipe its output into the pager (which by default, is less). less will take up the full terminal and be interactive regardless of the number of lines except when --quit-if-one-screen is passed to it as a command-line argument.

The code behind it is a bit complicated, but in most cases, bat will automatically add that argument to less. Explicitly setting bat's pager may prevent that from happening in the following cases:

Without knowing more about how bat is configured in your environment, @Hashino, I can't tell you exactly why bat isn't giving --quit-if-one-screen to less by default. If you want to give us that info, you can paste the output of bat --diagnostics as a comment on this issue or edit it into the original comment :)