~gpanders/dotfiles

5392594bc20da85940c2895c4da1b54bcd5f19de — Gregory Anders a day ago 3aee74a master
fish: create non-existent venvs upon activation
1 files changed, 22 insertions(+), 12 deletions(-)

M .config/fish/functions/venv.fish
M .config/fish/functions/venv.fish => .config/fish/functions/venv.fish +22 -12
@@ 1,26 1,30 @@
function __venv_create --argument-names dir
    python3 -m venv $dir
    if not test $status
        echo "Something went wrong; virtual environment creation failed" >&2
        return 1
    end
end

function venv
    set -q venv_dir; or set -U venv_dir $XDG_DATA_HOME/venv

    if test (count $argv) -lt 1
        echo "Usage:
        echo 'Usage:
    venv create <venv>
    venv rm <venv>
    venv ls
    venv <venv>" >&2
    venv <venv>' >&2
        return 1
    end

    switch $argv[1]
        case create
            python3 -m venv $venv_dir/$argv[2]
            if test $status
            if __venv_create $venv_dir/$argv[2]
                echo "Virtual environment $argv[2] created. Activate it using 'venv $argv[2]'"
            else
                echo "Something went wrong; virtual environment creation failed" >&2
                return 1
            end
        case ls
            printf "%s\n" (command ls $venv_dir)
            printf '%s\n' (command ls $venv_dir)
        case rm
            if not test -d $venv_dir/$argv[2]
                echo "venv '$argv[2]' not found" >&2


@@ 34,11 38,17 @@ function venv
                return 1
            end
        case '*'
            if not test -d $venv_dir/$argv[1]
                echo "venv '$argv[1]' not found" >&2
                return 1
            if test -d $venv_dir/$argv[1]
                source $venv_dir/$argv[1]/bin/activate.fish
            else
                read -P "venv '$argv[1]' does not exist. Create it? [Y/n] " ans
                if test (string lower $ans) != 'n'
                    if __venv_create $venv_dir/$argv[1]
                        echo "Virtual environment $argv[1] created"
                        source $venv_dir/$argv[1]/bin/activate.fish
                    end
                end
            end
            source $venv_dir/$argv[1]/bin/activate.fish
    end
end