Add explicit support for python and conda venvs to clone-in-kitty

Still needs to be implemented for fish
This commit is contained in:
Kovid Goyal 2022-04-16 13:15:10 +05:30
parent d6c5b40ead
commit 1daf745d74
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 63 additions and 0 deletions

View file

@ -562,6 +562,9 @@ def parse_message(self, msg: str) -> None:
if k == 'pid':
self.pid = int(v)
continue
if k == 'envfmt':
self.envfmt = v
continue
v = base64.standard_b64decode(v).decode('utf-8', 'replace')
if k == 'a':
self.args.append(v)
@ -589,6 +592,8 @@ def clone_and_launch(msg: str, window: Window) -> None:
c.opts.cwd = c.cwd
c.opts.copy_colors = True
c.opts.copy_env = False
c.opts.env = list(c.opts.env) + ['KITTY_IS_CLONE_LAUNCH=1']
cmdline = c.cmdline
if c.pid > -1:
try:
cmdline = cmdline_of_process(c.pid)

View file

@ -6,6 +6,14 @@ if [[ -z "$KITTY_SHELL_INTEGRATION" ]]; then builtin return; fi
_ksi_inject() {
# Load the normal bash startup files
if [[ -n "$KITTY_BASH_INJECT" ]]; then
if [ -n "$KITTY_IS_CLONE_LAUNCH" ]; then
# store some vars before the rc files have a chance to change them
builtin declare -Ag _ksi_pre_rc
_ksi_pre_rc=(
[path]="$PATH" [conda_default_env]="$CONDA_DEFAULT_ENV" [python_venv]="$VIRTUAL_ENV" [is_clone_launch]="$KITTY_IS_CLONE_LAUNCH"
)
fi
builtin local kitty_bash_inject="$KITTY_BASH_INJECT"
builtin local ksi_val="$KITTY_SHELL_INTEGRATION"
builtin unset KITTY_SHELL_INTEGRATION # ensure manual sourcing of this file in bashrc does not have any effect
@ -59,6 +67,7 @@ _ksi_inject() {
}
_ksi_inject
builtin unset -f _ksi_inject
builtin unset KITTY_IS_CLONE_LAUNCH
if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then
builtin unset KITTY_SHELL_INTEGRATION
@ -268,6 +277,31 @@ _ksi_main() {
builtin eval "$oldval"
PROMPT_COMMAND+="; $pc"
fi
if [ -n "${_ksi_pre_rc[is_clone_launch]}" ]; then
builtin export PATH="${_ksi_pre_rc[path]}"
builtin hash -r 2> /dev/null 1> /dev/null
if [ -n "${_ksi_pre_rc[python_venv]}" -a -r "${_ksi_pre_rc[python_venv]}/bin/activate" ]; then
builtin unset VIRTUAL_ENV
. "${_ksi_pre_rc[python_venv]}/bin/activate"
elif [ -n "${_ksi_pre_rc[conda_default_env]}" ] && builtin command -v conda >/dev/null 2>/dev/null && [ "${_ksi_pre_rc[conda_default_env]}" != "$CONDA_DEFAULT_ENV" ]; then
conda activate "${_ksi_pre_rc[conda_default_env]}"
fi
# Ensure PATH has no duplicate entries
if [ -n "$PATH" ]; then
builtin local old_PATH=$PATH:; PATH=
while [ -n "$old_PATH" ]; do
builtin local x
x=${old_PATH%%:*}
case $PATH: in
*:"$x":*) ;;
*) PATH=$PATH:$x;;
esac
old_PATH=${old_PATH#*:}
done
PATH=${PATH#:}
fi
fi
builtin unset _ksi_pre_rc
}
_ksi_main
builtin unset -f _ksi_main

View file

@ -32,6 +32,16 @@ builtin emulate -L zsh -o no_warn_create_global -o no_aliases
# 2: none of the above.
builtin typeset -gi _ksi_state
if [ -n "$KITTY_IS_CLONE_LAUNCH" ]; then
# store some vars before the rc files have a chance to change them
builtin typeset -gA _ksi_pre_rc
_ksi_pre_rc[path]="$PATH"
_ksi_pre_rc[conda_default_env]="$CONDA_DEFAULT_ENV"
_ksi_pre_rc[python_venv]="$VIRTUAL_ENV"
_ksi_pre_rc[is_clone_launch]="$KITTY_IS_CLONE_LAUNCH"
builtin unset KITTY_IS_CLONE_LAUNCH
fi
# Attempt to create a writable file descriptor to the TTY so that we can print
# to the TTY later even when STDOUT is redirected. This code is fairly subtle.
#
@ -349,6 +359,20 @@ _ksi_deferred_init() {
precmd_functions=(${precmd_functions:#_ksi_deferred_init})
fi
if [ -n "${_ksi_pre_rc[is_clone_launch]}" ]; then
builtin export PATH="${_ksi_pre_rc[path]}"
builtin hash -r 2> /dev/null 1> /dev/null
if [ -n "${_ksi_pre_rc[python_venv]}" -a -r "${_ksi_pre_rc[python_venv]}/bin/activate" ]; then
builtin unset VIRTUAL_ENV
. "${_ksi_pre_rc[python_venv]}/bin/activate"
elif [[ -n "${_ksi_pre_rc[conda_default_env]}" && (( $+commands[conda] )) && "${_ksi_pre_rc[conda_default_env]}" != "$CONDA_DEFAULT_ENV" ]]; then
conda activate "${_ksi_pre_rc[conda_default_env]}"
fi
# Ensure PATH has no duplicate entries
typeset -U path
fi
builtin unset _ksi_pre_rc
# Unfunction _ksi_deferred_init to save memory. Don't unfunction
# kitty-integration though because decent public functions aren't supposed to
# to unfunction themselves when invoked. Unfunctioning is done by calling code.