From 5c9c605184dcec8549b1f67a835f890a62f938cf Mon Sep 17 00:00:00 2001 From: fluzzi Date: Mon, 4 Apr 2022 19:09:00 -0300 Subject: [PATCH] added completion bash/zsh --- connpy/__init__.py | 2 +- connpy/connapp.py | 84 +++++++++++++++++++++++++++++++++++------ docs/connpy/index.html | 86 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 148 insertions(+), 24 deletions(-) diff --git a/connpy/__init__.py b/connpy/__init__.py index b65d75b..ed28ed0 100644 --- a/connpy/__init__.py +++ b/connpy/__init__.py @@ -126,7 +126,7 @@ from .connapp import connapp from pkg_resources import get_distribution __all__ = ["node", "nodes", "configfile", "connapp"] -__version__ = "2.0.0" +__version__ = "2.0.1" __author__ = "Federico Luzzi" __pdoc__ = { 'core': False, diff --git a/connpy/connapp.py b/connpy/connapp.py index ce8f0bc..2cd13f6 100755 --- a/connpy/connapp.py +++ b/connpy/connapp.py @@ -75,7 +75,7 @@ class connapp: configparser = subparsers.add_parser("config", help="Manage app config") configparser.add_argument("--allow-uppercase", dest="case", nargs=1, action=self._store_type, help="Allow case sensitive names", choices=["true","false"]) configparser.add_argument("--keepalive", dest="idletime", nargs=1, action=self._store_type, help="Set keepalive time in seconds, 0 to disable", type=int, metavar="INT") - configparser.add_argument("--completion", dest="completion", nargs=0, action=self._store_type, help="Get bash completion configuration for conn") + configparser.add_argument("--completion", dest="completion", nargs=1, choices=["bash","zsh"], action=self._store_type, help="Get terminal completion configuration for conn") configparser.set_defaults(func=self._func_others) #Set default subparser and tune arguments commands = ["node", "profile", "mv", "move","copy", "cp", "bulk", "ls", "list", "config"] @@ -390,7 +390,10 @@ class connapp: print("0 nodes added") else: if args.command == "completion": - print(self._help("completion")) + if args.data[0] == "bash": + print(self._help("bashcompletion")) + elif args.data[0] == "zsh": + print(self._help("zshcompletion")) else: if args.command == "case": if args.data[0] == "true": @@ -679,7 +682,7 @@ class connapp: return "conn [-h] [--add | --del | --mod | --show | --debug] [node|folder]\n conn {profile,move,mv,copy,cp,list,ls,bulk,config} ..." if type == "end": return "Commands:\n profile Manage profiles\n move (mv) Move node\n copy (cp) Copy node\n list (ls) List profiles, nodes or folders\n bulk Add nodes in bulk\n config Manage app config" - if type == "completion": + if type == "bashcompletion": return ''' #Here starts bash completion for conn #You need jq installed in order to use this @@ -691,7 +694,7 @@ _conn() mapfile -t folders < <(jq -r ' .["connections"] | paths as $path | select(getpath($path) == "folder" or getpath($path) == "subfolder") | $path | [map(select(. != "type"))[-1,-2]] | map(select(. !=null)) | join("@")' $DATADIR/config.json) mapfile -t profiles < <(jq -r '.["profiles"] | keys[]' $DATADIR/config.json) if [ "${#COMP_WORDS[@]}" = "2" ]; then - strings="--add --del --rm --edit --mod mv --show ls cp profile bulk config --help" + strings="--add --del --rm --edit --mod --show mv move ls list cp copy profile bulk config --help" strings="$strings ${connections[@]} ${folders[@]/#/@}" COMPREPLY=($(compgen -W "$strings" -- "${COMP_WORDS[1]}")) fi @@ -699,11 +702,11 @@ _conn() strings="" if [ "${COMP_WORDS[1]}" = "profile" ]; then strings="--add --rm --del --edit --mod --show --help"; fi if [ "${COMP_WORDS[1]}" = "config" ]; then strings="--allow-uppercase --keepalive --completion --help"; fi - if [[ "${COMP_WORDS[1]}" =~ ^--mod|--edit|-e|--show|-s|--add|-a|--rm|--del|-r$ ]]; then strings="profile"; fi - if [[ "${COMP_WORDS[1]}" =~ ^list|ls$ ]]; then strings="profiles nodes folders"; fi - if [[ "${COMP_WORDS[1]}" =~ ^bulk|mv|move|cp|copy$ ]]; then strings="--help"; fi - if [[ "${COMP_WORDS[1]}" =~ ^--rm|--del|-r$ ]]; then strings="$strings ${folders[@]/#/@}"; fi - if [[ "${COMP_WORDS[1]}" =~ ^--rm|--del|-r|--mod|--edit|-e|mv|move|cp|copy|--show|-s$ ]]; then + if [[ "${COMP_WORDS[1]}" =~ --mod|--edit|-e|--show|-s|--add|-a|--rm|--del|-r ]]; then strings="profile"; fi + if [[ "${COMP_WORDS[1]}" =~ list|ls ]]; then strings="profiles nodes folders"; fi + if [[ "${COMP_WORDS[1]}" =~ bulk|mv|move|cp|copy ]]; then strings="--help"; fi + if [[ "${COMP_WORDS[1]}" =~ --rm|--del|-r ]]; then strings="$strings ${folders[@]/#/@}"; fi + if [[ "${COMP_WORDS[1]}" =~ --rm|--del|-r|--mod|--edit|-e|mv|move|cp|copy|--show|-s ]]; then strings="$strings ${connections[@]}" fi COMPREPLY=($(compgen -W "$strings" -- "${COMP_WORDS[2]}")) @@ -711,12 +714,12 @@ _conn() if [ "${#COMP_WORDS[@]}" = "4" ]; then strings="" if [ "${COMP_WORDS[1]}" = "profile" ]; then - if [[ "${COMP_WORDS[2]}" =~ ^--rm|--del|-r|--mod|--edit|-e|--show|-s$ ]] ; then + if [[ "${COMP_WORDS[2]}" =~ --rm|--del|-r|--mod|--edit|-e|--show|-s ]] ; then strings="$strings ${profiles[@]}" fi fi if [ "${COMP_WORDS[2]}" = "profile" ]; then - if [[ "${COMP_WORDS[1]}" =~ ^--rm|--del|-r|--mod|--edit|-e|--show|-s$ ]] ; then + if [[ "${COMP_WORDS[1]}" =~ --rm|--del|-r|--mod|--edit|-e|--show|-s ]] ; then strings="$strings ${profiles[@]}" fi fi @@ -724,8 +727,67 @@ _conn() fi } complete -o nosort -F _conn conn +complete -o nosort -F _conn connpy ''' + if type == "zshcompletion": + return ''' +#Here starts zsh completion for conn +#You need jq installed in order to use this + +autoload -U compinit && compinit +_conn() +{ + DATADIR=$HOME/.config/conn + local COMP_WORDS num + COMP_WORDS=( $words ) + num=${#COMP_WORDS[@]} + if [[ $words =~ '.* $' ]]; then + num=$(($num + 1)) + fi + x=`jq -r ' .["connections"] | paths as $path | select(getpath($path) == "connection") | $path | [map(select(. != "type"))[-1,-2,-3]] | map(select(. !=null)) | join("@")' /home/fluzzi32/.config/conn/config.json` + connections=( $x ) + x=`jq -r ' .["connections"] | paths as $path | select(getpath($path) == "folder" or getpath($path) == "subfolder") | $path | [map(select(. != "type"))[-1,-2]] | map(select(. !=null)) | join("@")' $DATADIR/config.json | sed -e 's/^/@/'` + folders=( $x ) + x=`jq -r '.["profiles"] | keys[]' $DATADIR/config.json` + profiles=( $x ) + if [ "${num}" = "2" ]; then + strings="--add --del --rm --edit --mod --show mv move ls list cp copy profile bulk config --help" + strings="$strings ${connections[@]} ${folders[@]}" + compadd "$@" -- `echo $strings` + fi + if [ "${num}" = "3" ]; then + strings="" + if [ "${COMP_WORDS[2]}" = "profile" ]; then strings="--add --rm --del --edit --mod --show --help"; fi + if [ "${COMP_WORDS[2]}" = "config" ]; then strings="--allow-uppercase --keepalive --completion --help"; fi + if [[ "${COMP_WORDS[2]}" =~ '--mod|--edit|-e|--show|-s|--add|-a|--rm|--del|-r' ]]; then strings="profile"; fi + if [[ "${COMP_WORDS[2]}" =~ 'list|ls' ]]; then strings="profiles nodes folders"; fi + if [[ "${COMP_WORDS[2]}" =~ 'bulk|mv|move|cp|copy' ]]; then strings="--help"; fi + if [[ "${COMP_WORDS[2]}" =~ '--rm|--del|-r' ]]; then strings="$strings ${folders[@]}"; fi + if [[ "${COMP_WORDS[2]}" =~ '--rm|--del|-r|--mod|--edit|-e|mv|move|cp|copy|--show|-s' ]]; then + strings="$strings ${connections[@]}" + fi + compadd "$@" -- `echo $strings` + fi + if [ "${num}" = "4" ]; then + strings="" + if [ "${COMP_WORDS[2]}" = "profile" ]; then + if [[ "${COMP_WORDS[3]}" =~ '--rm|--del|-r|--mod|--edit|-e|--show|-s' ]] ; then + strings="$strings ${profiles[@]}" + fi + fi + if [ "${COMP_WORDS[3]}" = "profile" ]; then + if [[ "${COMP_WORDS[2]}" =~ '--rm|--del|-r|--mod|--edit|-e|--show|-s' ]] ; then + strings="$strings ${profiles[@]}" + fi + fi + + compadd "$@" -- `echo $strings` + fi +} +compdef _conn conn +compdef _conn connpy + ''' def _getallnodes(self): #get all nodes on configfile diff --git a/docs/connpy/index.html b/docs/connpy/index.html index 4dd18f4..a9ee016 100644 --- a/docs/connpy/index.html +++ b/docs/connpy/index.html @@ -259,7 +259,7 @@ from .connapp import connapp from pkg_resources import get_distribution __all__ = ["node", "nodes", "configfile", "connapp"] -__version__ = "2.0.0" +__version__ = "2.0.1" __author__ = "Federico Luzzi" __pdoc__ = { 'core': False, @@ -674,7 +674,7 @@ __pdoc__ = { configparser = subparsers.add_parser("config", help="Manage app config") configparser.add_argument("--allow-uppercase", dest="case", nargs=1, action=self._store_type, help="Allow case sensitive names", choices=["true","false"]) configparser.add_argument("--keepalive", dest="idletime", nargs=1, action=self._store_type, help="Set keepalive time in seconds, 0 to disable", type=int, metavar="INT") - configparser.add_argument("--completion", dest="completion", nargs=0, action=self._store_type, help="Get bash completion configuration for conn") + configparser.add_argument("--completion", dest="completion", nargs=1, choices=["bash","zsh"], action=self._store_type, help="Get terminal completion configuration for conn") configparser.set_defaults(func=self._func_others) #Set default subparser and tune arguments commands = ["node", "profile", "mv", "move","copy", "cp", "bulk", "ls", "list", "config"] @@ -989,7 +989,10 @@ __pdoc__ = { print("0 nodes added") else: if args.command == "completion": - print(self._help("completion")) + if args.data[0] == "bash": + print(self._help("bashcompletion")) + elif args.data[0] == "zsh": + print(self._help("zshcompletion")) else: if args.command == "case": if args.data[0] == "true": @@ -1278,7 +1281,7 @@ __pdoc__ = { return "conn [-h] [--add | --del | --mod | --show | --debug] [node|folder]\n conn {profile,move,mv,copy,cp,list,ls,bulk,config} ..." if type == "end": return "Commands:\n profile Manage profiles\n move (mv) Move node\n copy (cp) Copy node\n list (ls) List profiles, nodes or folders\n bulk Add nodes in bulk\n config Manage app config" - if type == "completion": + if type == "bashcompletion": return ''' #Here starts bash completion for conn #You need jq installed in order to use this @@ -1290,7 +1293,7 @@ _conn() mapfile -t folders < <(jq -r ' .["connections"] | paths as $path | select(getpath($path) == "folder" or getpath($path) == "subfolder") | $path | [map(select(. != "type"))[-1,-2]] | map(select(. !=null)) | join("@")' $DATADIR/config.json) mapfile -t profiles < <(jq -r '.["profiles"] | keys[]' $DATADIR/config.json) if [ "${#COMP_WORDS[@]}" = "2" ]; then - strings="--add --del --rm --edit --mod mv --show ls cp profile bulk config --help" + strings="--add --del --rm --edit --mod --show mv move ls list cp copy profile bulk config --help" strings="$strings ${connections[@]} ${folders[@]/#/@}" COMPREPLY=($(compgen -W "$strings" -- "${COMP_WORDS[1]}")) fi @@ -1298,11 +1301,11 @@ _conn() strings="" if [ "${COMP_WORDS[1]}" = "profile" ]; then strings="--add --rm --del --edit --mod --show --help"; fi if [ "${COMP_WORDS[1]}" = "config" ]; then strings="--allow-uppercase --keepalive --completion --help"; fi - if [[ "${COMP_WORDS[1]}" =~ ^--mod|--edit|-e|--show|-s|--add|-a|--rm|--del|-r$ ]]; then strings="profile"; fi - if [[ "${COMP_WORDS[1]}" =~ ^list|ls$ ]]; then strings="profiles nodes folders"; fi - if [[ "${COMP_WORDS[1]}" =~ ^bulk|mv|move|cp|copy$ ]]; then strings="--help"; fi - if [[ "${COMP_WORDS[1]}" =~ ^--rm|--del|-r$ ]]; then strings="$strings ${folders[@]/#/@}"; fi - if [[ "${COMP_WORDS[1]}" =~ ^--rm|--del|-r|--mod|--edit|-e|mv|move|cp|copy|--show|-s$ ]]; then + if [[ "${COMP_WORDS[1]}" =~ --mod|--edit|-e|--show|-s|--add|-a|--rm|--del|-r ]]; then strings="profile"; fi + if [[ "${COMP_WORDS[1]}" =~ list|ls ]]; then strings="profiles nodes folders"; fi + if [[ "${COMP_WORDS[1]}" =~ bulk|mv|move|cp|copy ]]; then strings="--help"; fi + if [[ "${COMP_WORDS[1]}" =~ --rm|--del|-r ]]; then strings="$strings ${folders[@]/#/@}"; fi + if [[ "${COMP_WORDS[1]}" =~ --rm|--del|-r|--mod|--edit|-e|mv|move|cp|copy|--show|-s ]]; then strings="$strings ${connections[@]}" fi COMPREPLY=($(compgen -W "$strings" -- "${COMP_WORDS[2]}")) @@ -1310,12 +1313,12 @@ _conn() if [ "${#COMP_WORDS[@]}" = "4" ]; then strings="" if [ "${COMP_WORDS[1]}" = "profile" ]; then - if [[ "${COMP_WORDS[2]}" =~ ^--rm|--del|-r|--mod|--edit|-e|--show|-s$ ]] ; then + if [[ "${COMP_WORDS[2]}" =~ --rm|--del|-r|--mod|--edit|-e|--show|-s ]] ; then strings="$strings ${profiles[@]}" fi fi if [ "${COMP_WORDS[2]}" = "profile" ]; then - if [[ "${COMP_WORDS[1]}" =~ ^--rm|--del|-r|--mod|--edit|-e|--show|-s$ ]] ; then + if [[ "${COMP_WORDS[1]}" =~ --rm|--del|-r|--mod|--edit|-e|--show|-s ]] ; then strings="$strings ${profiles[@]}" fi fi @@ -1323,8 +1326,67 @@ _conn() fi } complete -o nosort -F _conn conn +complete -o nosort -F _conn connpy ''' + if type == "zshcompletion": + return ''' +#Here starts zsh completion for conn +#You need jq installed in order to use this + +autoload -U compinit && compinit +_conn() +{ + DATADIR=$HOME/.config/conn + local COMP_WORDS num + COMP_WORDS=( $words ) + num=${#COMP_WORDS[@]} + if [[ $words =~ '.* $' ]]; then + num=$(($num + 1)) + fi + x=`jq -r ' .["connections"] | paths as $path | select(getpath($path) == "connection") | $path | [map(select(. != "type"))[-1,-2,-3]] | map(select(. !=null)) | join("@")' /home/fluzzi32/.config/conn/config.json` + connections=( $x ) + x=`jq -r ' .["connections"] | paths as $path | select(getpath($path) == "folder" or getpath($path) == "subfolder") | $path | [map(select(. != "type"))[-1,-2]] | map(select(. !=null)) | join("@")' $DATADIR/config.json | sed -e 's/^/@/'` + folders=( $x ) + x=`jq -r '.["profiles"] | keys[]' $DATADIR/config.json` + profiles=( $x ) + if [ "${num}" = "2" ]; then + strings="--add --del --rm --edit --mod --show mv move ls list cp copy profile bulk config --help" + strings="$strings ${connections[@]} ${folders[@]}" + compadd "$@" -- `echo $strings` + fi + if [ "${num}" = "3" ]; then + strings="" + if [ "${COMP_WORDS[2]}" = "profile" ]; then strings="--add --rm --del --edit --mod --show --help"; fi + if [ "${COMP_WORDS[2]}" = "config" ]; then strings="--allow-uppercase --keepalive --completion --help"; fi + if [[ "${COMP_WORDS[2]}" =~ '--mod|--edit|-e|--show|-s|--add|-a|--rm|--del|-r' ]]; then strings="profile"; fi + if [[ "${COMP_WORDS[2]}" =~ 'list|ls' ]]; then strings="profiles nodes folders"; fi + if [[ "${COMP_WORDS[2]}" =~ 'bulk|mv|move|cp|copy' ]]; then strings="--help"; fi + if [[ "${COMP_WORDS[2]}" =~ '--rm|--del|-r' ]]; then strings="$strings ${folders[@]}"; fi + if [[ "${COMP_WORDS[2]}" =~ '--rm|--del|-r|--mod|--edit|-e|mv|move|cp|copy|--show|-s' ]]; then + strings="$strings ${connections[@]}" + fi + compadd "$@" -- `echo $strings` + fi + if [ "${num}" = "4" ]; then + strings="" + if [ "${COMP_WORDS[2]}" = "profile" ]; then + if [[ "${COMP_WORDS[3]}" =~ '--rm|--del|-r|--mod|--edit|-e|--show|-s' ]] ; then + strings="$strings ${profiles[@]}" + fi + fi + if [ "${COMP_WORDS[3]}" = "profile" ]; then + if [[ "${COMP_WORDS[2]}" =~ '--rm|--del|-r|--mod|--edit|-e|--show|-s' ]] ; then + strings="$strings ${profiles[@]}" + fi + fi + + compadd "$@" -- `echo $strings` + fi +} +compdef _conn conn +compdef _conn connpy + ''' def _getallnodes(self): #get all nodes on configfile