Skip to content

Instantly share code, notes, and snippets.

@connorfuhrman
Created September 16, 2022 16:11
Show Gist options
  • Save connorfuhrman/99f9369db8eadd5e87835cbdb2ef8195 to your computer and use it in GitHub Desktop.
Save connorfuhrman/99f9369db8eadd5e87835cbdb2ef8195 to your computer and use it in GitHub Desktop.
Function to set a nice shell prompt with Pyenv and ROS shown along with a nicely trimmed current working directory
#!/bin/bash
## Prompt setup
update_shell_prompt() {
RED='\[\e[0;31m\]'
GREEN='\[\e[0;32m\]'
BLUE='\[\e[0;34m\]'
PURPLE='\[\e[0;35m\]'
RESET='\[\e[0m\]'
# Check if using pyenv
if [[ -z "${USING_PYENV}" ]]; then
PYENV_PS1_ADD=""
else
[ -z "$PYENV_VIRTUALENV_ORIGINAL_PS1" ] && export PYENV_VIRTUALENV_ORIGINAL_PS1="$PS1"
[ -z "$PYENV_VIRTUALENV_GLOBAL_NAME" ] && export PYENV_VIRTUALENV_GLOBAL_NAME="$(pyenv global)"
VENV_NAME="$(pyenv version-name)"
VENV_NAME="${VENV_NAME##*/}"
GLOBAL_NAME="$PYENV_VIRTUALENV_GLOBAL_NAME"
# non-global versions:
COLOR="$BLUE"
# global version:
[ "$VENV_NAME" == "$GLOBAL_NAME" ] && COLOR="$RED"
# virtual envs:
[ "${VIRTUAL_ENV##*/}" == "$VENV_NAME" ] && COLOR="$GREEN"
PYENV_PS1_ADD="[Py $COLOR${VENV_NAME}$RESET] "
fi
# Check if using ROS
if [[ -z "${ROS_DISTRO}" ]]; then
ROS_PS1_ADD=""
else
ROS_PS1_ADD="[ROS $PURPLE${ROS_DISTRO}$RESET] "
fi
# Check if in a ROS overlay
if [[ -z "${COLCON_PREFIX_PATH}" ]]; then
ROS_UL_PS1_ADD=""
else
ROS_UL_PS1_ADD="[ROS Overlay: ${BLUE}$(dirname ${COLCON_PREFIX_PATH})$RESET]\n"
fi
export PS1="${ROS_UL_PS1_ADD}[\T] ${PYENV_PS1_ADD}${ROS_PS1_ADD}$GREEN\u$RESET: $(prompt_dir_str) >> "
}
export PROMPT_COMMAND="update_shell_prompt;"
## pyenv setup
pyenv_init() {
export PATH="$HOME/.pyenv/bin:$PATH"
#eval "$(pyenv init - bash --no-rehash)"
eval "$(pyenv init - )"
eval "$(pyenv virtualenv-init -)"
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
export USING_PYENV=1
}
## ROS Setup
ros_init() {
source /opt/ros/foxy/setup.bash
[[ -z "${USING_PYENV}" ]] && pyenv_init
pyenv shell ros2-foxy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment