Skip to content

Commit 0f83c33

Browse files
authored
Add fish prompt changing (#475)
1 parent 2b2b87a commit 0f83c33

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

bin/pyenv-sh-activate

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,20 @@ if [ -z "${PYENV_VIRTUALENV_DISABLE_PROMPT}" ]; then
245245
case "${shell}" in
246246
fish )
247247
if [ -z "${QUIET}" ]; then
248-
echo "pyenv-virtualenv: prompt changing not working for fish." 1>&2
248+
cat <<EOS
249+
functions -e _pyenv_old_prompt # remove old prompt function if exists.
250+
# since everything is in memory, it's safe to
251+
# remove it.
252+
functions -c fish_prompt _pyenv_old_prompt # backup old prompt function
253+
254+
# from python-venv
255+
function fish_prompt
256+
set -l prompt (_pyenv_old_prompt) # call old prompt function first since it might
257+
# read exit status
258+
echo -n "(${venv}) " # add virtualenv to prompt
259+
string join -- \n \$prompt # handle multiline prompts
260+
end
261+
EOS
249262
fi
250263
;;
251264
* )

bin/pyenv-sh-deactivate

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,15 @@ esac
187187

188188
case "${shell}" in
189189
fish )
190-
:
190+
cat <<EOS
191+
# check if old prompt function exists
192+
if functions -q _pyenv_old_prompt
193+
# remove old prompt function if exists.
194+
functions -e fish_prompt
195+
functions -c _pyenv_old_prompt fish_prompt
196+
functions -e _pyenv_old_prompt
197+
end
198+
EOS
191199
;;
192200
* )
193201
cat <<EOS

test/activate.bats

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,18 @@ EOS
138138
deactivated
139139
set -gx PYENV_VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
140140
set -gx VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
141-
pyenv-virtualenv: prompt changing not working for fish.
141+
functions -e _pyenv_old_prompt # remove old prompt function if exists.
142+
# since everything is in memory, it's safe to
143+
# remove it.
144+
functions -c fish_prompt _pyenv_old_prompt # backup old prompt function
145+
146+
# from python-venv
147+
function fish_prompt
148+
set -l prompt (_pyenv_old_prompt) # call old prompt function first since it might
149+
# read exit status
150+
echo -n "(venv) " # add virtualenv to prompt
151+
string join -- \n \$prompt # handle multiline prompts
152+
end
142153
EOS
143154

144155
unstub pyenv-version-name
@@ -164,7 +175,18 @@ set -gx PYENV_VERSION "venv";
164175
set -gx PYENV_ACTIVATE_SHELL 1;
165176
set -gx PYENV_VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
166177
set -gx VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
167-
pyenv-virtualenv: prompt changing not working for fish.
178+
functions -e _pyenv_old_prompt # remove old prompt function if exists.
179+
# since everything is in memory, it's safe to
180+
# remove it.
181+
functions -c fish_prompt _pyenv_old_prompt # backup old prompt function
182+
183+
# from python-venv
184+
function fish_prompt
185+
set -l prompt (_pyenv_old_prompt) # call old prompt function first since it might
186+
# read exit status
187+
echo -n "(venv) " # add virtualenv to prompt
188+
string join -- \n \$prompt # handle multiline prompts
189+
end
168190
EOS
169191

170192
unstub pyenv-version-name
@@ -239,7 +261,18 @@ set -gx PYENV_VERSION "venv27";
239261
set -gx PYENV_ACTIVATE_SHELL 1;
240262
set -gx PYENV_VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
241263
set -gx VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
242-
pyenv-virtualenv: prompt changing not working for fish.
264+
functions -e _pyenv_old_prompt # remove old prompt function if exists.
265+
# since everything is in memory, it's safe to
266+
# remove it.
267+
functions -c fish_prompt _pyenv_old_prompt # backup old prompt function
268+
269+
# from python-venv
270+
function fish_prompt
271+
set -l prompt (_pyenv_old_prompt) # call old prompt function first since it might
272+
# read exit status
273+
echo -n "(venv27) " # add virtualenv to prompt
274+
string join -- \n \$prompt # handle multiline prompts
275+
end
243276
EOS
244277

245278
unstub pyenv-virtualenv-prefix
@@ -263,7 +296,18 @@ set -gx PYENV_VERSION "venv27";
263296
set -gx PYENV_ACTIVATE_SHELL 1;
264297
set -gx PYENV_VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
265298
set -gx VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
266-
pyenv-virtualenv: prompt changing not working for fish.
299+
functions -e _pyenv_old_prompt # remove old prompt function if exists.
300+
# since everything is in memory, it's safe to
301+
# remove it.
302+
functions -c fish_prompt _pyenv_old_prompt # backup old prompt function
303+
304+
# from python-venv
305+
function fish_prompt
306+
set -l prompt (_pyenv_old_prompt) # call old prompt function first since it might
307+
# read exit status
308+
echo -n "(venv27) " # add virtualenv to prompt
309+
string join -- \n \$prompt # handle multiline prompts
310+
end
267311
EOS
268312

269313
unstub pyenv-virtualenv-prefix

test/conda-activate.bats

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,18 @@ deactivated
7070
set -gx PYENV_VIRTUAL_ENV "${TMP}/pyenv/versions/anaconda-2.3.0";
7171
set -gx VIRTUAL_ENV "${TMP}/pyenv/versions/anaconda-2.3.0";
7272
set -gx CONDA_DEFAULT_ENV "root";
73-
pyenv-virtualenv: prompt changing not working for fish.
73+
functions -e _pyenv_old_prompt # remove old prompt function if exists.
74+
# since everything is in memory, it's safe to
75+
# remove it.
76+
functions -c fish_prompt _pyenv_old_prompt # backup old prompt function
77+
78+
# from python-venv
79+
function fish_prompt
80+
set -l prompt (_pyenv_old_prompt) # call old prompt function first since it might
81+
# read exit status
82+
echo -n "(anaconda-2.3.0) " # add virtualenv to prompt
83+
string join -- \n \$prompt # handle multiline prompts
84+
end
7485
EOS
7586

7687
unstub pyenv-version-name

test/conda-deactivate.bats

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ if [ -n "\$_OLD_VIRTUAL_PYTHONHOME" ];
8282
set -gx PYTHONHOME "\$_OLD_VIRTUAL_PYTHONHOME";
8383
set -e _OLD_VIRTUAL_PYTHONHOME;
8484
end;
85+
# check if old prompt function exists
86+
if functions -q _pyenv_old_prompt
87+
# remove old prompt function if exists.
88+
functions -e fish_prompt
89+
functions -c _pyenv_old_prompt fish_prompt
90+
functions -e _pyenv_old_prompt
91+
end
8592
if functions -q deactivate;
8693
functions -e deactivate;
8794
end;

test/deactivate.bats

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ if [ -n "\$_OLD_VIRTUAL_PYTHONHOME" ];
225225
set -gx PYTHONHOME "\$_OLD_VIRTUAL_PYTHONHOME";
226226
set -e _OLD_VIRTUAL_PYTHONHOME;
227227
end;
228+
# check if old prompt function exists
229+
if functions -q _pyenv_old_prompt
230+
# remove old prompt function if exists.
231+
functions -e fish_prompt
232+
functions -c _pyenv_old_prompt fish_prompt
233+
functions -e _pyenv_old_prompt
234+
end
228235
if functions -q deactivate;
229236
functions -e deactivate;
230237
end;
@@ -251,6 +258,13 @@ if [ -n "\$_OLD_VIRTUAL_PYTHONHOME" ];
251258
set -gx PYTHONHOME "\$_OLD_VIRTUAL_PYTHONHOME";
252259
set -e _OLD_VIRTUAL_PYTHONHOME;
253260
end;
261+
# check if old prompt function exists
262+
if functions -q _pyenv_old_prompt
263+
# remove old prompt function if exists.
264+
functions -e fish_prompt
265+
functions -c _pyenv_old_prompt fish_prompt
266+
functions -e _pyenv_old_prompt
267+
end
254268
if functions -q deactivate;
255269
functions -e deactivate;
256270
end;
@@ -279,6 +293,13 @@ if [ -n "\$_OLD_VIRTUAL_PYTHONHOME" ];
279293
set -gx PYTHONHOME "\$_OLD_VIRTUAL_PYTHONHOME";
280294
set -e _OLD_VIRTUAL_PYTHONHOME;
281295
end;
296+
# check if old prompt function exists
297+
if functions -q _pyenv_old_prompt
298+
# remove old prompt function if exists.
299+
functions -e fish_prompt
300+
functions -c _pyenv_old_prompt fish_prompt
301+
functions -e _pyenv_old_prompt
302+
end
282303
if functions -q deactivate;
283304
functions -e deactivate;
284305
end;
@@ -307,6 +328,13 @@ if [ -n "\$_OLD_VIRTUAL_PYTHONHOME" ];
307328
set -gx PYTHONHOME "\$_OLD_VIRTUAL_PYTHONHOME";
308329
set -e _OLD_VIRTUAL_PYTHONHOME;
309330
end;
331+
# check if old prompt function exists
332+
if functions -q _pyenv_old_prompt
333+
# remove old prompt function if exists.
334+
functions -e fish_prompt
335+
functions -c _pyenv_old_prompt fish_prompt
336+
functions -e _pyenv_old_prompt
337+
end
310338
if functions -q deactivate;
311339
functions -e deactivate;
312340
end;
@@ -333,6 +361,13 @@ if [ -n "\$_OLD_VIRTUAL_PYTHONHOME" ];
333361
set -gx PYTHONHOME "\$_OLD_VIRTUAL_PYTHONHOME";
334362
set -e _OLD_VIRTUAL_PYTHONHOME;
335363
end;
364+
# check if old prompt function exists
365+
if functions -q _pyenv_old_prompt
366+
# remove old prompt function if exists.
367+
functions -e fish_prompt
368+
functions -c _pyenv_old_prompt fish_prompt
369+
functions -e _pyenv_old_prompt
370+
end
336371
if functions -q deactivate;
337372
functions -e deactivate;
338373
end;

0 commit comments

Comments
 (0)