hanslub42 / rlwrap

A readline wrapper
GNU General Public License v2.0
2.53k stars 149 forks source link

[feature] shell completion #172

Open Freed-Wu opened 1 year ago

Freed-Wu commented 1 year ago

Is it possible that rlwrap provide tab completion for common shells like

$ rlwrap -<TAB>
option
-a
-b
--break-chars
-C
-D
...
$ rlwrap <TAB>
external command
7za                                 A file archiver with highest compression ratio
7z                                  A file archiver with highest compression ratio
7zr                                 A file archiver with highest compression ratio
a2ping                              convert between PS, EPS and PDF and other page description formats
a52dec                              decode ATSC A/52 audio streams
aafire                              aalib example programs
aclocal-1.16                        manual page for aclocal 1.16.5
aclocal                             manual page for aclocal 1.16.5
...
$ rlwrap perl -<TAB>
option
-0     input record separator ($/)
-a     autosplit mode with -n or -p (splits $_ into @F)
...

An example for zsh

/usr/share/zsh/site-functions/_rlwrap

#compdef rlwrap

_arguments -S -s \
  '(- : *)'{-h,--help} \
  {-A,--always-readline}':password prompt' \
  '(-)1: :{_command_names -e}' \
  '*:: :_normal'

This file can be generated automatically from https://github.com/hanslub42/rlwrap/blob/master/src/main.c#L125-L161

TIA!

hanslub42 commented 1 year ago

I think the best way to do this is to make rlwraps help text conform to the GNU tools format. In that way I can leave the job to the different automated tools that exist for bash, zsh and possibly other shells

Freed-Wu commented 1 year ago

_gnu_generic needs zsh to parse the help text which is slower than the pre-generated shell completion script. However, a standard help text is also a good change.

Freed-Wu commented 1 year ago

https://github.com/hanslub42/rlwrap/blob/master/src/main.c#L125-L161 define a stuct to store the information of options, I thought the program can use this options to generate help text from a template. If so, any one can do the similar work to generate shell completion script from a template by this sturct. However, it looks this program only use this struct for getopt. https://github.com/hanslub42/rlwrap/blob/master/src/utils.c#L716-L748 rewrite code to print help text. Perhaps move these two parts about option information to one part will be more convenient to maintain and debug? (When an option is changed, don't need to change two parts). Just my 2c. And the code has some trailing whitespaces which I think can be removed.