moar icon indicating copy to clipboard operation
moar copied to clipboard

Bug: `--quit-if-one-screen` still sets alternate screen mode before exit when text fits into one screen

Open mikelei8291 opened this issue 6 months ago • 2 comments

When using moor with the --quit-if-one-screen option, it still sets the alternate screen mode before exit when the text fits into one screen, causing the terminal to flash for a brief moment. Please see the below screen recording for a comparison between moor and less, both using the --quit-if-one-screen option:

https://github.com/user-attachments/assets/78365866-d9cf-4dff-9fb6-41fd2638a1bf

Watch for the scroll bar on the right edge of the recording if the switching between modes is hard to catch.

This is purely cosmetic though, and after reading the code I think it requires some structural change of how the pager gets run, so you may mark this issue as not very high priority.

Thank you as always!

mikelei8291 avatar May 24 '26 22:05 mikelei8291

Imagine the stream being slow. You pipe something into moor that produces one (short) line per second.

After three lines it closes the stream.

Since the stream was open, moor had to be open while waiting for the stream to close. Since its UI is built around using an alt screen, this means going into alt screen mode.

So I don't really know how that could be done, maybe delay UI a bit when running with --quit-if-one-screen?

And on another note, in that screen recording, less never exits so they are not comparable.

walles avatar May 27 '26 17:05 walles

I did a small repro and code-path check for this.

Repro command I used from a terminal-sized pty:

TERM=xterm-256color script -q /tmp/moor-425.typescript sh -c 'stty rows 24 cols 80; printf "hello\\n" | ./moor --quit-if-one-screen'

The captured output includes both alternate-screen enter and leave sequences before printing the final line back on the normal screen:

ESC[?1049h ... ESC[?1049l ... hello

That matches what I see in the current flow: pagerFromArgs() creates the real screen as soon as the first byte is available, while Pager.StartPaging() only evaluates QuitIfOneScreen later inside the main loop after a redraw, once ReadingDone and HighlightingDone are true. At that point it sets DeInit = false, quits, closes the screen, and reprints the content after exit. So the final visible output is right, but the alternate screen has already been entered.

I think the tricky boundary is exactly the slow-stream case you mentioned: waiting for EOF before opening the UI would make something_slow | moor --quit-if-one-screen look hung until the producer closes. A narrow fix would probably need to be scoped to inputs where moor can know the complete content cheaply before screen setup, or to a very small startup grace period. I would not treat this as a simple pager-loop-only change because the decision currently happens after the real screen exists.

TIR44 avatar May 31 '26 10:05 TIR44

I think the whole thing may work fine with a combo of --quit-if-one-screen --no-clear-on-exit, even on the slow stream.

The thought is to enter alternate screen only if it doesn't fit into one screen, but I realise that treating this specific combo of options may be hacky and/or not desirable.

qezz avatar Jul 06 '26 21:07 qezz