-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
conda.fish
141 lines (118 loc) · 3.66 KB
/
conda.fish
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
#
# INSTALL
#
# Run 'conda init fish' and restart your shell.
#
if not set -q CONDA_SHLVL
set -gx CONDA_SHLVL 0
set -g _CONDA_ROOT (dirname (dirname $CONDA_EXE))
set -gx PATH $_CONDA_ROOT/condabin $PATH
end
function __conda_add_prompt
if set -q CONDA_PROMPT_MODIFIER
set_color -o green
echo -n $CONDA_PROMPT_MODIFIER
set_color normal
end
end
if functions -q fish_prompt
if not functions -q __fish_prompt_orig
functions -c fish_prompt __fish_prompt_orig
end
functions -e fish_prompt
else
function __fish_prompt_orig
end
end
function return_last_status
return $argv
end
function fish_prompt
set -l last_status $status
if set -q CONDA_LEFT_PROMPT
__conda_add_prompt
end
return_last_status $last_status
__fish_prompt_orig
end
if functions -q fish_right_prompt
if not functions -q __fish_right_prompt_orig
functions -c fish_right_prompt __fish_right_prompt_orig
end
functions -e fish_right_prompt
else
function __fish_right_prompt_orig
end
end
function fish_right_prompt
if not set -q CONDA_LEFT_PROMPT
__conda_add_prompt
end
__fish_right_prompt_orig
end
function conda --inherit-variable CONDA_EXE
if [ (count $argv) -lt 1 ]
$CONDA_EXE
else
set -l cmd $argv[1]
set -e argv[1]
switch $cmd
case activate deactivate
eval ($CONDA_EXE shell.fish $cmd $argv)
case install update upgrade remove uninstall
$CONDA_EXE $cmd $argv
and eval ($CONDA_EXE shell.fish reactivate)
case '*'
$CONDA_EXE $cmd $argv
end
end
end
# Autocompletions below
# Faster but less tested (?)
function __fish_conda_commands
string replace -r '.*_([a-z]+)\.py$' '$1' $_CONDA_ROOT/lib/python*/site-packages/conda/cli/main_*.py
for f in $_CONDA_ROOT/bin/conda-*
if test -x "$f" -a ! -d "$f"
string replace -r '^.*/conda-' '' "$f"
end
end
echo activate
echo deactivate
end
function __fish_conda_env_commands
string replace -r '.*_([a-z]+)\.py$' '$1' $_CONDA_ROOT/lib/python*/site-packages/conda_env/cli/main_*.py
end
function __fish_conda_envs
conda config --json --show envs_dirs | python -c "import json, os, sys; from os.path import isdir, join; print('\n'.join(d for ed in json.load(sys.stdin)['envs_dirs'] if isdir(ed) for d in os.listdir(ed) if isdir(join(ed, d))))"
end
function __fish_conda_packages
conda list | awk 'NR > 3 {print $1}'
end
function __fish_conda_needs_command
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = conda ]
return 0
end
return 1
end
function __fish_conda_using_command
set cmd (commandline -opc)
if [ (count $cmd) -gt 1 ]
if [ $argv[1] = $cmd[2] ]
return 0
end
end
return 1
end
# Conda commands
complete -f -c conda -n __fish_conda_needs_command -a '(__fish_conda_commands)'
complete -f -c conda -n '__fish_conda_using_command env' -a '(__fish_conda_env_commands)'
# Commands that need environment as parameter
complete -f -c conda -n '__fish_conda_using_command activate' -a '(__fish_conda_envs)'
# Commands that need package as parameter
complete -f -c conda -n '__fish_conda_using_command remove' -a '(__fish_conda_packages)'
complete -f -c conda -n '__fish_conda_using_command uninstall' -a '(__fish_conda_packages)'
complete -f -c conda -n '__fish_conda_using_command upgrade' -a '(__fish_conda_packages)'
complete -f -c conda -n '__fish_conda_using_command update' -a '(__fish_conda_packages)'