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

Use PAGER environment variable instead of 'less' #158

Closed
notramo opened this issue May 28, 2018 · 17 comments
Closed

Use PAGER environment variable instead of 'less' #158

notramo opened this issue May 28, 2018 · 17 comments
Labels
feature-request New feature or request good first issue Good for newcomers
Milestone

Comments

@notramo
Copy link

notramo commented May 28, 2018

bat sould use pager name from PAGER environment variable, and only use less if PAGER is unset.

@sharkdp sharkdp added feature-request New feature or request good first issue Good for newcomers labels May 28, 2018
@sharkdp
Copy link
Owner

sharkdp commented May 28, 2018

Good idea!

@sharkdp
Copy link
Owner

sharkdp commented May 29, 2018

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.

@notramo
Copy link
Author

notramo commented May 29, 2018

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.

@moonfruit
Copy link

moonfruit commented Jun 4, 2018

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.

@sharkdp
Copy link
Owner

sharkdp commented Jun 12, 2018

@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.

@moonfruit
Copy link

moonfruit commented Jun 12, 2018

@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 mysql or 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.

@notramo
Copy link
Author

notramo commented Jun 12, 2018

git also has the GIT_PAGER environment variable. I think this few lines of code shouldn't be a big problem.

@yaauie
Copy link

yaauie commented Jul 6, 2018

I think a reasonable detection would follow:

  1. Use BAT_PAGER if present
  2. else, use PAGER if present (optionally: skip this if --no-detect-pager command-line flag present)
  3. else, fall back to current usage of less with bat-optimised flags

In 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)

@sharkdp
Copy link
Owner

sharkdp commented Jul 18, 2018

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'"

@achalddave
Copy link

achalddave commented Jul 20, 2018

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```

@sharkdp
Copy link
Owner

sharkdp commented Jul 23, 2018

I have implemented a first version of this in #192. It'd be great if someone could review this.

@sharkdp sharkdp added this to the v0.5.0 milestone Aug 19, 2018
@sharkdp
Copy link
Owner

sharkdp commented Aug 19, 2018

Released in bat 0.5.

@dharmeshkakadia
Copy link

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?

@mike-mosher
Copy link

mike-mosher commented Aug 31, 2018

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

@Inphi
Copy link

Inphi commented Nov 8, 2018

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.

@tavianator
Copy link

tavianator commented Jun 4, 2019

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.

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.

@sharkdp
Copy link
Owner

sharkdp commented Jun 6, 2019

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:

bat/src/output.rs

Lines 31 to 44 in e9210c0

let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
(Ok(bat_pager), _) => Some(bat_pager),
(_, Ok(pager)) => {
// less needs to be called with the '-R' option in order to properly interpret the
// ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
// therefore need to overwrite the arguments and add '-R'.
//
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
// or bats '--pager' command line option.
replace_arguments_to_less = true;
Some(pager)
}
_ => None,
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

9 participants