Make direnv use .venv/, show Python version in PS1.

This commit is contained in:
Julien Palard 2023-12-02 23:22:52 +01:00
parent 6a497cf3b7
commit 546bc3f01d
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
1 changed files with 41 additions and 20 deletions

61
.bashrc
View File

@ -74,20 +74,48 @@ do
fi fi
done done
trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "$var"
}
shortest()
{
# Returns the shortest of two strings
if [[ ${#1} -lt ${#2} ]]
then
printf "%s" "$1"
else
printf "%s" "$2"
fi
}
python_ps1() python_ps1()
{ {
local pypath="$(which python 2>/dev/null)" local relative venvpath pyversion _
if [[ -z "$pypath" ]] || [[ "$pypath" == "/usr/bin/python" ]]
then venvpath="$(which python 2>/dev/null)"
venvpath="${venvpath%/bin/python}"
if [[ -z "$venvpath" ]] || [[ "$venvpath" == "/usr" ]]; then
return return
fi fi
local relative="$(realpath --relative-to=$(pwd) -s "$pypath")"
if [[ ${#relative} -lt ${#pypath} ]] relative="$(realpath --relative-to="$(pwd)" -s "$venvpath")"
then venvpath="$(shortest "$venvpath" "$relative")"
pypath="$relative"
if [[ "$venvpath" == ".venv" ]]; then
venvpath="" # No need to tell me my venv is in .venv, I can guess it.
fi fi
printf "$1" "${pypath%/bin/python}"
read -r _ pyversion <<<"$(python --version)"
printf "$1" "$(trim "$venvpath $pyversion")"
} }
if [[ "$TERM" != 'dumb' ]] if [[ "$TERM" != 'dumb' ]]
@ -203,21 +231,14 @@ e()
venv() venv()
{ {
# direnv-first venv creation. # Simple helper to use direnv to create Python venvs.
if [[ -f .envrc ]] #
then # See https://github.com/pappasam/jedi-language-server/issues/199#issuecomment-1836880575
echo '`.envrc` file already exists!' # for more context.
return printf "VIRTUAL_ENV=.venv\nlayout python3\n" > .envrc
fi
echo "layout python3" > .envrc
direnv allow . direnv allow .
} }
pip-common()
{
python -m pip install --upgrade --upgrade-strategy eager mypy black flake8 jedi-language-server pylint build twine grip tox pip
}
github-gpg() github-gpg()
{ {
curl https://github.com/$1.gpg | gpg --import-options show-only --import - curl https://github.com/$1.gpg | gpg --import-options show-only --import -