page icon indicating copy to clipboard operation
page copied to clipboard

bat syntax support does not match vim

Open arcnmx opened this issue 2 years ago • 2 comments

Problem

I set up page as my git pager:

:; git config core.pager 'page -O -t git'
:; git diff
[ 0000000376 | WARN  | dump in page ]
cannot spawn bat, use stdout: Os { code: 2, kind: NotFound, message: "No such file or directory" }
diff --git ...

Though it worked, I was confused about the warning, not knowing what it was or that it was a dependency that needed to be installed. After looking through the source, I tried installing it:

:; git diff
[bat error]: unknown syntax: 'git'

and now there's no output at all! One can't always assume that bat language names will match git filetypes.

Resolution

There appears to be no way to configure this behaviour. I could've set PAGE_LOG=error to silence all warnings, but there's no direct way to turn the bat fallback off. I also would be happy to run bat if the --language argument were omitted. I'd suggest:

  1. Some sort of argument/syntax that configures vim's filetype (-t) and bat's language (--bat-language? -T?) separately
  2. If -T defaults to -t's value, there should be a way to clear it with -T auto or something
  3. When a file path is provided instead of stdin, prefer bat --file-name argument instead of bat --language - this code seems incorrect, the extension is not a language and the filename should be passed instead if it is available. (bat uses the filename to auto-detect the language, but unlike --language, it will not panic if it does not recognize it)
  4. There should probably be a flag to explicitly turn bat on/off, perhaps -T none for example. So page -O -t pager -T auto would use bat, while page -O -t rust -T none would cat instead

arcnmx avatar Mar 22 '23 15:03 arcnmx

Hello, thank you for this bug report and sorry that I was silent for so long. Currently life overwhelms me.


Honestly, I'd really like to get rid of bat dependency altogether.

As for now I like your suggestion, it really seems to be the best solution possible. Just no time/energy to implement it.

But I'll definitely return to this in the future!

I60R avatar Apr 13 '23 13:04 I60R

I'd like to propose a solution that does not involve bat.

  1. Install delta: https://dandavison.github.io/delta/installation.html
  2. Add the following to your .gitconfig: https://github.com/dandavison/delta#get-started
[core]
    pager = delta
[interactive]
    diffFilter = delta --color-only
[delta]
    navigate = true    # use n and N to move between diff sections
    light = false      # set to true if you're in a terminal w/ a light background color (e.g. the default macOS terminal)
[merge]
    conflictstyle = diff3
[diff]
    colorMoved = default
[pager]
    log = delta | page -WC -q 90000 -z 90000
    diff = delta | page -WC -q 90000 -z 90000
  1. Add the following variables to your .zshrc, .bashrc, etc.:
export DELTA_PAGER="less -R"
export PAGER="page -WC -q 90000 -z 90000"

With the above, I have delta syntax highlighting and all of the page features for all git commands that involve paging (e.g. log, diff, etc.) except for interative git command (e.g. those the --patch option). I think it does not make sense to use page for interactive git commands, so I will not open an issue for that.

By the way, I came up with a way to have bat syntax highlighting for man pages in page:

export MANPAGER="sh -c 'col -bx | bat -l man -p --color=always | page'"

This is working well for me so far and if I want to switch to page man://$PROGRAM($SECTION), i just press K on the program name. page man://$PROGRAM($SECTION) has better formatting, but bat -pl man has better syntax highlighting, in my opinion.

I also created an alias m that does the same thing as the m function in the page README, but it also allows for passing options to page.

alias m="func() { local PROGRAM=$(echo '$@[-1]'); local SECTION=$(echo '${@[-2]:+($@[-2])}'); local OPTIONS=$(echo '${@:1:$# -2}'); page $(echo '$OPTIONS -W man://$PROGRAM$SECTION'); }; func"

I hope at least some of the above will be useful. If you can think of a better way to do any of the above, I'm open to suggestions :)

maptv avatar Jun 20 '23 18:06 maptv