lab icon indicating copy to clipboard operation
lab copied to clipboard

[completion] bash tab completion for the uberspace command

Open EV21 opened this issue 2 years ago • 2 comments

As an occasional user it is almost impossible to remember the command chain of the uberspace command. It's nice that each command has its own help/usage text, but autocomplete is even nicer when you already know what you want but don't know the path or for some reasons the typing or spelling is a problem for you. So I think tab completion is a must have. Maybe one day this one or something like it will make it to all Uberspaces 😉

https://github.com/EV21/uberspace-helper/blob/main/_uberspace_completion

run this within your home directory

wget https://raw.githubusercontent.com/EV21/uberspace-helper/main/_uberspace_completion
echo 'source ~/_uberspace_completion' >> .bashrc

then restart your ssh session or do source ~/.bashrc

usage: type uberspace<space><tab><tab> the result should be an output of mail port takeout tools web type uberspace m<tab> -> line transforms to uberspace mail If you then type <tab><tab> you will get the output of the subcommands catchall domain spamfolder user and so on


uberspace command structure

This is just an overview I did so I know the levels for the completion script

uberspace
    mail
        catchall
            del
            set
            status
        domain
            add
            del
            list
        spamfolder
            disable
            enable
            status
        user
            add
            del
            forward
            list
            password
    port
        add
        del
        list
    takeout (ignored that for now)
    tools
        restart
            php
        version
            list
            show
                couchdb
                erlang
                mongodb
                node
                php
                postgresql
                prolog
                ruby
                rust
            use
                couchdb
                erlang
                mongodb
                node
                php
                postgresql
                prolog
                ruby
                rust
    web
        backend
            del
            list
            set
        domain
            add
            del
            list
        errorpage
            enable
            disable
            status
        header
            del
            list
            set
            suppress
        log
            enable
            disable
            status

EV21 avatar Jun 21 '22 09:06 EV21

Hey @EV21! We'll add this in 🥁🥁🥁 U9.

nichtmax avatar Sep 13 '22 13:09 nichtmax

Nice 😎 So there will be no (public) U8, right?

But I think it would be better if you generate the completion scripts than writing them by hand like I did.

Maybe Completely or built-in 3rd-party libraries of your application like Cobra for golang.

Maybe this variant is more maintainable than the hand written level based design.

short example for `uberspace mail`

You just need a simple yaml file like this for Completely:

uberspace:
- mail
- port
- takeout
- tools
- web

uberspace mail:
- catchall
- domain
- spamfolder
- user

uberspace mail catchall:
- del
- set
- status

uberspace mail domain:
- add
- del
- list

uberspace mail spamfolder:
- disable
- enable
- status

uberspace mail user:
- add
- del
- forward
- list
- password

And this is the result:

# uberspace completion                                     -*- shell-script -*-

# This bash completions script was generated by
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_uberspace_completions_filter() {
  local words="$1"
  local cur=${COMP_WORDS[COMP_CWORD]}
  local result=()

  if [[ "${cur:0:1}" == "-" ]]; then
    echo "$words"

  else
    for word in $words; do
      [[ "${word:0:1}" != "-" ]] && result+=("$word")
    done

    echo "${result[*]}"

  fi
}

_uberspace_completions() {
  local cur=${COMP_WORDS[COMP_CWORD]}
  local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
  local compline="${compwords[*]}"

  case "$compline" in
    'mail spamfolder'*)
      while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_uberspace_completions_filter "disable enable status")" -- "$cur" )
      ;;

    'mail catchall'*)
      while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_uberspace_completions_filter "del set status")" -- "$cur" )
      ;;

    'mail domain'*)
      while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_uberspace_completions_filter "add del list")" -- "$cur" )
      ;;

    'mail user'*)
      while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_uberspace_completions_filter "add del forward list password")" -- "$cur" )
      ;;

    'mail'*)
      while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_uberspace_completions_filter "catchall domain spamfolder user")" -- "$cur" )
      ;;

    *)
      while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_uberspace_completions_filter "mail port takeout tools web")" -- "$cur" )
      ;;

  esac
} &&
complete -F _uberspace_completions uberspace

# ex: filetype=sh

EV21 avatar Sep 13 '22 14:09 EV21

We've got this on our radar, but it isn't a task for the lab.

luto avatar Feb 26 '23 19:02 luto