From 6362b0616a29bffb6398c4c3a2e83a3daf3a2f73 Mon Sep 17 00:00:00 2001 From: tavo Date: Tue, 24 Dec 2024 22:44:16 -0600 Subject: [PATCH] autocomp --- shell/functions/pyv | 8 ++++---- shell/functions/pyv_comp.bash | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 shell/functions/pyv_comp.bash diff --git a/shell/functions/pyv b/shell/functions/pyv index 16490d8..65f8365 100644 --- a/shell/functions/pyv +++ b/shell/functions/pyv @@ -13,7 +13,7 @@ _pyv_log() { } _pyv_help() { - printf 'Usage:\n \033[92mpyv\033[0m \033[94m[ls|new|rm|go|exit]\033[0m \033[95mmyvenv\033[0m\n' + printf 'Usage:\n \033[92mpyv\033[0m \033[94m[ls|new|rm|enter|exit]\033[0m \033[95mmyvenv\033[0m\n' } _pyv_avail() { @@ -62,7 +62,7 @@ pyv() { mkdir -p "$VENV_DIR" case "$1" in - new|go|rm) + new|enter|rm) VENV_OPT="$1" ; VENV_LIST="${@#${VENV_OPT}}" ; VENV_LIST="${VENV_LIST#* }" ;; -d) VENV_OPT="rm" ; VENV_LIST="${@#${VENV_OPT}}" ; VENV_LIST="${VENV_LIST#* }" ;; @@ -83,7 +83,7 @@ pyv() { [ -n "$avail" ] && printf '\n%s\n' "$avail" return 0 ;; *) - VENV_OPT="go" ; VENV_LIST="$@" ;; + VENV_OPT="enter" ; VENV_LIST="$@" ;; esac for v in $VENV_LIST ; do @@ -122,7 +122,7 @@ pyv() { done fi - if [ "$VENV_OPT" == "go" ] ; then + if [ "$VENV_OPT" == "enter" ] ; then command -v deactivate >&- 2>&- && deactivate venv="${VENV_LIST%% *}" _pyv_log "Activating $venv..." diff --git a/shell/functions/pyv_comp.bash b/shell/functions/pyv_comp.bash new file mode 100644 index 0000000..2699cb6 --- /dev/null +++ b/shell/functions/pyv_comp.bash @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +_pyv_comp() { + if [ -z "$VENV_DIR" ] ; then + if [ -n "$XDG_DATA_HOME" ] ; then + VENV_DIR="$XDG_DATA_HOME/venv" + else + [ -z "$HOME" ] && return 1 + VENV_DIR="$HOME/.local/share/venv" + fi + fi + + if [ "${#COMP_WORDS[@]}" == "2" ]; then + COMPREPLY=($(compgen -W "ls new rm enter exit" "${COMP_WORDS[1]}")) + fi + + if [ "${#COMP_WORDS[@]}" -gt 2 ]; then + venvs="$(for v in "$VENV_DIR"/* ; do + if [ -d "$v" ] ; then + printf '%s ' "${v##*/}" + fi + done)" + venvs="${venvs% *}" + COMPREPLY=($(compgen -W "$venvs" -- "${COMP_WORDS[-1]}")) + fi +} + +complete -F _pyv_comp pyv