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
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()
{
local pypath="$(which python 2>/dev/null)"
if [[ -z "$pypath" ]] || [[ "$pypath" == "/usr/bin/python" ]]
then
local relative venvpath pyversion _
venvpath="$(which python 2>/dev/null)"
venvpath="${venvpath%/bin/python}"
if [[ -z "$venvpath" ]] || [[ "$venvpath" == "/usr" ]]; then
return
fi
local relative="$(realpath --relative-to=$(pwd) -s "$pypath")"
if [[ ${#relative} -lt ${#pypath} ]]
then
pypath="$relative"
relative="$(realpath --relative-to="$(pwd)" -s "$venvpath")"
venvpath="$(shortest "$venvpath" "$relative")"
if [[ "$venvpath" == ".venv" ]]; then
venvpath="" # No need to tell me my venv is in .venv, I can guess it.
fi
printf "$1" "${pypath%/bin/python}"
read -r _ pyversion <<<"$(python --version)"
printf "$1" "$(trim "$venvpath $pyversion")"
}
if [[ "$TERM" != 'dumb' ]]
@ -203,21 +231,14 @@ e()
venv()
{
# direnv-first venv creation.
if [[ -f .envrc ]]
then
echo '`.envrc` file already exists!'
return
fi
echo "layout python3" > .envrc
# Simple helper to use direnv to create Python venvs.
#
# See https://github.com/pappasam/jedi-language-server/issues/199#issuecomment-1836880575
# for more context.
printf "VIRTUAL_ENV=.venv\nlayout python3\n" > .envrc
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()
{
curl https://github.com/$1.gpg | gpg --import-options show-only --import -