Implement option to control clone sourcing strategies

This commit is contained in:
Kovid Goyal
2022-04-17 19:39:32 +05:30
parent f2189b3e70
commit 230a9f4678
4 changed files with 71 additions and 21 deletions

View File

@@ -264,12 +264,27 @@ _ksi_main() {
builtin eval "${KITTY_IS_CLONE_LAUNCH}"
builtin hash -r 2> /dev/null 1> /dev/null
builtin local venv="${VIRTUAL_ENV}/bin/activate"
if [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then
builtin local sourced=""
_ksi_s_is_ok() {
[[ -z "$sourced" && "$KITTY_CLONE_SOURCE_STRATEGIES" == *",$1,"* ]] && return 0
return 1
}
if _ksi_s_is_ok "venv" && [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then
sourced="y"
builtin unset VIRTUAL_ENV
. "$venv"
elif [ -n "${CONDA_DEFAULT_ENV}" ] && builtin command -v conda >/dev/null 2>/dev/null && [ "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]; then
fi; if _ksi_s_is_ok "conda" && [ -n "${CONDA_DEFAULT_ENV}" ] && builtin command -v conda >/dev/null 2>/dev/null && [ "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]; then
sourced="y"
conda activate "${CONDA_DEFAULT_ENV}"
fi; if _ksi_s_is_ok "env_var" && [[ -n "${KITTY_CLONE_SOURCE_CODE}" ]]; then
sourced="y"
eval "${KITTY_CLONE_SOURCE_CODE}"
fi; if _ksi_s_is_ok "path" && [[ -r "${KITTY_CLONE_SOURCE_PATH}" ]]; then
sourced="y"
builtin source "${KITTY_CLONE_SOURCE_PATH}"
fi
builtin unset -f _ksi_s_is_ok
# Ensure PATH has no duplicate entries
if [ -n "$PATH" ]; then
builtin local old_PATH=$PATH:; PATH=
@@ -285,7 +300,7 @@ _ksi_main() {
PATH=${PATH#:}
fi
fi
builtin unset KITTY_IS_CLONE_LAUNCH
builtin unset KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES
}
_ksi_main
builtin unset -f _ksi_main

View File

@@ -113,17 +113,41 @@ function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after
set -l orig_conda_env "$CONDA_DEFAULT_ENV"
eval "$KITTY_IS_CLONE_LAUNCH"
set -l venv "$VIRTUAL_ENV/bin/activate.fish"
if test -n "$VIRTUAL_ENV" -a -r "$venv"
set -g _ksi_sourced 'n'
function _ksi_s_is_ok
if test "$_ksi_sourced" = 'n'
and string match -q -- "*,$argv[1],*" "$KITTY_CLONE_SOURCE_STRATEGIES"
return 0
end
return 1
end
if _ksi_s_is_ok "venv"
and test -n "$VIRTUAL_ENV" -a -r "$venv"
set _ksi_sourced "y"
set -e VIRTUAL_ENV _OLD_FISH_PROMPT_OVERRIDE # activate.fish stupidly exports _OLD_FISH_PROMPT_OVERRIDE
source "$venv"
else if test -n "$CONDA_DEFAULT_ENV"
end
if _ksi_s_is_ok "conda"
and test -n "$CONDA_DEFAULT_ENV"
and type -q conda
and test "$CONDA_DEFAULT_ENV" != "$orig_conda_env"
set _ksi_sourced "y"
# for some reason that I cant be bothered to figure out this doesnt take effect
# conda activate $_ksi_pre_rc_conda_default_env
eval ($CONDA_EXE shell.fish activate $CONDA_DEFAULT_ENV)
end
set --erase KITTY_IS_CLONE_LAUNCH
if _ksi_s_is_ok "env_var"
and test -n "$KITTY_CLONE_SOURCE_CODE"
set _ksi_sourced "y"
eval "$KITTY_CLONE_SOURCE_CODE"
end
if _ksi_s_is_ok "path"
and test -r "$KITTY_CLONE_SOURCE_PATH"
set _ksi_sourced "y"
source "$KITTY_CLONE_SOURCE_PATH"
end
set --erase KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES _ksi_sourced
functions --erase _ksi_s_is_ok
end
end

View File

@@ -354,16 +354,31 @@ _ksi_deferred_init() {
builtin eval "${KITTY_IS_CLONE_LAUNCH}"
builtin hash -r 2> /dev/null 1> /dev/null
builtin local venv="${VIRTUAL_ENV}/bin/activate"
if [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then
builtin local sourced=""
_ksi_s_is_ok() {
[[ -z "$sourced" && "$KITTY_CLONE_SOURCE_STRATEGIES" == *",$1,"* ]] && return 0
return 1
}
if _ksi_s_is_ok "venv" && [[ -n "${VIRTUAL_ENV}" && -r "$venv" ]]; then
sourced="y"
builtin unset VIRTUAL_ENV
. "$venv"
elif [[ -n "${CONDA_DEFAULT_ENV}" && (( $+commands[conda] )) && "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]]; then
builtin source "$venv"
fi; if _ksi_s_is_ok "conda" && [[ -n "${CONDA_DEFAULT_ENV}" && (( $+commands[conda] )) && "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]]; then
sourced="y"
conda activate "${CONDA_DEFAULT_ENV}"
fi; if _ksi_s_is_ok "env_var" && [[ -n "${KITTY_CLONE_SOURCE_CODE}" ]]; then
sourced="y"
eval "${KITTY_CLONE_SOURCE_CODE}"
fi; if _ksi_s_is_ok "path" && [[ -r "${KITTY_CLONE_SOURCE_PATH}" ]]; then
sourced="y"
builtin source "${KITTY_CLONE_SOURCE_PATH}"
fi
builtin unfunction _ksi_s_is_ok
# Ensure PATH has no duplicate entries
typeset -U path
builtin typeset -U path
fi
builtin unset KITTY_IS_CLONE_LAUNCH
builtin unset KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES
# Unfunction _ksi_deferred_init to save memory. Don't unfunction
# kitty-integration though because decent public functions aren't supposed to