dbuenzli / cmdliner

Declarative definition of command line interfaces for OCaml
http://erratique.ch/software/cmdliner
ISC License
292 stars 56 forks source link

How to clear screen content after quitting `opam --help` #191

Open muzimuzhi opened 1 week ago

muzimuzhi commented 1 week ago

As a macOS user new to OCaml, I find after I quit opam --help or dune --help, the help content is kept in the terminal. This default behavior is different from (most?) other CLI programs like git or man.

$ opam --help
OPAM(1)                           Opam Manual                          OPAM(1)

NAME
       opam - source-based package management

SYNOPSIS
       opam [COMMAND] …
...
$ git --paginate --help
$ man less
$

Is there a way to overwrite this behavior?

I have environment variables

$ echo $PAGER
less
$ echo $LESS
-R

It seems this is caused by the use of less option -X/--no-init, added in 1de361182abcb809bbc9856238b958b12039efd7 and f239981642aefc2792382a1417c690bb6eaeb264 for #167.

https://github.com/dbuenzli/cmdliner/blob/cb235b559e29a063fe3c4fb1a60b444cf70dd402/src/cmdliner_manpage.ml#L466-L472

-X or --no-init
       Disables sending the termcap initialization and deinitialization strings to the
       terminal.  This is sometimes desirable if the deinitialization string does something
       unnecessary, like clearing the screen.
dbuenzli commented 1 week ago

Here are a few points.

  1. This behaviour is more ergonomic than the alternative you suggest: you can get to the documentation of an option of interest, quit the pager and then try to use it while still having the documentation in sight.
  2. git log works this way by default so I'm not sure about your comment about git (see man git-config).

We could perhaps carry on the value of the LESS variable if the user defined it (like git does). That would allow you to use the LESS value you'd want. But I'm a bit vary and not very enthusiastic of changing things in this area. The whole groff and paging business is extremely fragile and I can already see more people coming here complaining that their help is garbled because they have a LESS variable set without the R.

muzimuzhi commented 1 week ago

I just realized PAGER=less LESS=-R is set by oh-my-zsh by default, which I haven't thought about why and already taken as granted.

The whole groff and paging business is extremely fragile and I can already see more people coming here complaining that their help is garbled because they have a LESS variable set without the R.

Understandable. Maintaining backwards compatibility is hard.

BTW what's inconvenient for me is that, in the file opened by less, it's possible to scroll lines using mouse wheel, but not if it's opened by less -X, in both macOS terminal.app and iTerm2. Maybe this is configurable in these terminal emulators.