walles / moar

Moar is a pager. It's designed to just do the right thing without any configuration.
Other
587 stars 17 forks source link

Support `less -F` "quit if one screen" #113

Closed Snurppa closed 1 year ago

Snurppa commented 1 year ago

I think this was kind of asked in #75.

There exists -F command in less:

Quit if end-of-file on first screen

Hint to source.

There seems not to be similar option in moar.


I noticed this as I've used bat for long time, and recently changed to use moar as default PAGER. bat by default uses less.

What changed in behaviour after less -> moar is that small files that would fit into one screen were processed with less -F in bat. With moar they will pop into "full screen mode" (as intended by pager).

Demonstration

Created a file with echo "[Google](https://google.com)" > google.md

With bat --paging=always --pager='less -F' google.md less will quit instantly as contents fit to screen:

less

With bat --paging=always --pager='moar' google.md moar does not quit (by design naturally):

moar

I came here looking for an equivalent -F switch so I could adjust how bat calls moar, but seems it does not exist yet.


I don't really have background in Go neither in terminal development, so don't (yet) have suggestion how to implement this. Quick first idea from looking to source made me think if the pager.ReprintAfterExit() could somehow be used to achieve this? Just missing the "does this fit to screen"/"is eof" logic?

I might be able to scratch something during Q1/23, but no time to dig further at this time.

Opened to issue get comments and possible ideas for implementation.

Thanks and happy new year!

walles commented 1 year ago

Thanks for the clear feature request!

Looking at how less does this (⭐ for the link to the less source code), in the main loop prompt() function (which I guess prints the prompt at the bottom of the page), they check:

If all of those are fulfilled, then they drop out.

We can do this.

When testing a bit I noted that unless -F is combined with -X, less just quits without showing anything, which is super confusing. moar must handle this better.

Outstanding considerations:

walles commented 1 year ago

I'm not getting what you're getting with bat and less, possibly because of version inconsistencies?

I have:

If I tell bat to use less -F I don't get any output at all.

With less -FRX (bail early, don't clear screen and handle ANSI codes) I get a decorated printout.

Either way, I'll just try to mimic what you're getting.

bat-less-f
Snurppa commented 1 year ago

Thanks for looking into it!

possibly because of version inconsistencies?

Yeah, my version is: less 581.2 (POSIX regular expressions).

FYI, bat has some default special handling for less. To ensure those don't get into our way I used --paging and --pager explicitly.

There seems to be some conditional for less "smaller than 530", maybe that --no-init is related to what you are seeing with your less? Don't know the history related to that unfortunately, but maybe that helps you to find more reasoning for the behavior you are seeing, idk.

Thanks, much obliged! :)

edit: Forgot to comment to considerations:

Should we auto disable line numbers in this case?

I don't know, maybe the -F should do just one thing, quit if those preconditions are met?

And there should/could be other switch to control the line numbers. Looking at bat, it seems to by default retain the line numbers. My above screenshots actually have taken my custom style config for bat: --style="changes,header-filename,snip". So I have configured them to be disabled. And I guess you have bat also using the default styles, as your output contains the line number.

Should we wait for background syntax highlighting to finish before dropping out

Not sure, maybe here bat and moar kind of overlap as both can do some styling. And by default bat sends that formatted data to less. First idea is that maybe by default moar could apply the highlighting, but also would be good to have some switch to turn it off, if user is only interested in other pager capabilities that moar has to offer, such as the search etc.

All in all, maybe have one option, the -F, do only one thing in this case, and achieve the other features with other options? At least thinking only in this -F context that IMO feels most clearest:

dsully commented 1 year ago

Any progress on this? I'm trying to switch to moar, but this is a deal breaker for me. Thanks.

walles commented 1 year ago

In progress (slowlyish): https://github.com/walles/moar/commits/johan/quit-if-one-screen

walles commented 1 year ago

Released in v1.15.1.