timvisee / prs

🔐 A secure, fast & convenient password manager CLI using GPG and git to sync.
https://gitlab.com/timvisee/prs
GNU General Public License v3.0
211 stars 8 forks source link

Autocomplete on bash and fish #7

Closed XenGi closed 1 year ago

XenGi commented 3 years ago

Ahoi,

how could autocompletion be accomplished? It would nice if prx could generate the needed output for these shells. I don't know how pass does it, but I could just type pass <tab> and get a list of entries. Would be nice to have the same feature with prs.

timvisee commented 3 years ago

how could autocompletion be accomplished?

This is done through an autocomplete script. Yes, such a script can be generated from prs's code through clap.

I'm not sure whether it would provide autocomplete for entries itself by default, so it might need some patching. But autocompleting sub-commands definitely works.

I might implement this through a hidden sub-command to generate the scripts at runtime, in a similar way I've done in ffsend.

timvisee commented 3 years ago

I'm currently working on implementing this.

You'll be able to generate a shell completion script for your preferred shell from prs:

# For example
prs internal completions --help
prs internal completions bash
prs internal completions fish --stdout | source

This is what I'm currently seeing in the fish shell after pressing Tab:

image

It also appears to be somewhat of a mess in bash at this moment:

image

It doesn't complete secret/password items/names now (but completes subcommands, arguments and flags), though it is a step in the right direction. Completing secrets is something I might implement later, as it is a bit more complex.

Related merge request: https://gitlab.com/timvisee/prs/-/merge_requests/19

dotlambda commented 3 years ago

The completions for ZSH don't work yet:

$ RUST_BACKTRACE=full prs internal completions zsh
Generating completions for zsh...thread 'main' panicked at 'Fatal internal error. Please consider filing a bug report at https://github.com/clap-rs/clap/issues', /build/prs-0.2.8-vendor.tar.gz/clap/src/completions/zsh.rs:407:29
stack backtrace:
   0:     0x56471631913c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hc72b2a79902e20a2
   1:     0x56471634455f - core::fmt::write::h363ea858f8d20109
   2:     0x564716318516 - std::io::Write::write_fmt::hdcbf0bc3dc301e4b
   3:     0x5647162fd4d5 - std::panicking::default_hook::{{closure}}::hf776c4b8a29e2c23
   4:     0x5647162fd0a3 - std::panicking::default_hook::hd9a17e7e9b9c0d25
   5:     0x5647162fdb05 - std::panicking::rust_panic_with_hook::hf65e38fdef13d248
   6:     0x564716319957 - std::panicking::begin_panic_handler::{{closure}}::h469be7df0b5aefa3
   7:     0x56471631929c - std::sys_common::backtrace::__rust_end_short_backtrace::hc9b4220705693734
   8:     0x5647162fd6b2 - rust_begin_unwind
   9:     0x564716344301 - core::panicking::panic_fmt::h0c4f421549c9e9f1
  10:     0x56471634b303 - core::option::expect_failed::h9463ba78b917711c
  11:     0x5647162efa0c - clap::completions::zsh::get_args_of::h86eff9d199e34388
  12:     0x5647162ed1d3 - clap::completions::zsh::get_subcommands_of::h44c43202de2f1071
  13:     0x5647162ec402 - clap::completions::zsh::ZshGen::generate_to::h99899a869746645e
  14:     0x56471629ad1c - clap::app::parser::Parser::gen_completions::h1d499f2e85f41aee
  15:     0x564715fd94c7 - prs::action::internal::Internal::invoke::hde9f01aff9c4de05
  16:     0x564715fb448c - prs::main::h8647502fcd461a35
  17:     0x564715fb79d3 - std::sys_common::backtrace::__rust_begin_short_backtrace::h9129a2b4b3244bf6
  18:     0x564715fb79e9 - std::rt::lang_start::{{closure}}::h66a466acfee3db59
  19:     0x5647162fdeba - std::rt::lang_start_internal::h085ffe6d399bf24c
  20:     0x564715fb7872 - main
  21:     0x7ff3b8284ded - __libc_start_main
  22:     0x564715f920ca - _start
  23:                0x0 - <unknown>
timvisee commented 3 years ago

@dotlambda Thanks for the report. I can indeed reproduce this with:

prs internal completions zsh --stdout
timvisee commented 3 years ago

@dotlambda

The completions for ZSH don't work yet:

This has been fixed in prs v0.2.9.

titaniumtraveler commented 2 years ago

@timvisee I manually edited edited the prs.bash file to get autocompletion for secrets too, by using prs list --list

Though I'm not sure how practical it would be to automatically replace it for autogeneration, but I thought it would be good to communicate how I solved it.

Example for prs show

Before:

        prs__show)
            opts=" -s -t -p -c -f -I -y -q -v -h  --first --store --timeout --property --copy --force --no-interact --yes --quiet --verbose --gpg-tty --help  <QUERY> "
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
                return 0
            fi

After:

        prs__show)
            opts=" -s -t -p -c -f -I -y -q -v -h  --first --store --timeout --property --copy --force --no-interact --yes --quiet --verbose --gpg-tty --help  <QUERY> "
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
        if  [[ ${COMPREPLY} == "" ]] ; then
            COMPREPLY=( $(compgen -W "$(prs list --list)" -- "${cur}") )
        fi
                return 0
            fi

An alternative way would be to String::replace() in the autogenerated code <QUERY> with $(prs list --list), but that leads to a less nice result as it makes it harder to use autocompletion to find options.

timvisee commented 2 years ago

@titaniumtraveler Awesome work, thanks for this! I'd love to see a feature in clap to generate this, though I don't think there is.

It may be worth to add the changed script to this repository, feel free to do so in a PR.

An alternative way would be to String::replace() in the autogenerated code <QUERY> with $(prs list --list), but that leads to a less nice result as it makes it harder to use autocompletion to find options.

This sounds like the best approach we currently have.