~gpanders/dotfiles

ref: 466bbcb447b26f0f86b41d8d2834277f36764a2a dotfiles/.config/fish/functions/venv.fish -rw-r--r-- 1.7 KiB
466bbcb4 — Gregory Anders fish: allow multiple venvs to be removed at once a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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:
    venv create <venv>
    venv rm <venv> [venv [...]]
    venv ls
    venv <venv>' >&2
        return 1
    end

    switch $argv[1]
        case create
            if __venv_create $venv_dir/$argv[2]
                echo "Virtual environment $argv[2] created. Activate it using 'venv $argv[2]'"
            end
        case ls
            printf '%s\n' (command ls $venv_dir)
        case rm
            for _venv in $argv[2..-1]
                if not test -d $venv_dir/$_venv
                    echo "venv '$_venv' not found" >&2
                    return 1
                end
                rm -rf $venv_dir/$_venv
                if test $status
                    echo "Virtual environment '$_venv' was successfully deleted"
                else
                    echo "Something went wrong; virtual environment deletion failed" >&2
                    return 1
                end
            end
        case '*'
            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
    end
end