Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python virtualenv. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Powerline for Bash in pure Bash script.
* Git branch: display "+" symbol when current branch is changed but uncommited
* Git branch: display "⇡" symbol and the difference in the number of commits when the current branch is ahead of remote (see screenshot)
* Git branch: display "⇣" symbol and the difference in the number of commits when the current branch is behind of remote (see screenshot)
* Python virtualenv: display current virtual environment
* Platform-dependent prompt symbol for OS X and Linux (see screenshots)
* Color code for the previously failed command
* Fast execution (no noticable delay)
Expand Down
21 changes: 20 additions & 1 deletion bash-powerline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __powerline() {
readonly GIT_BRANCH_CHANGED_SYMBOL='+'
readonly GIT_NEED_PUSH_SYMBOL='⇡'
readonly GIT_NEED_PULL_SYMBOL='⇣'
readonly PS_SYMBOL_PYTHON='ƨ'

# Solarized colorscheme
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
Expand Down Expand Up @@ -128,6 +129,15 @@ __powerline() {
printf " $GIT_BRANCH_SYMBOL$branch$marks "
}

__virtualenv() {
if [ -z "${VIRTUAL_ENV}" ] ; then
return
else
local virtualenv="$(basename $VIRTUAL_ENV)"
printf "($PS_SYMBOL_PYTHON $virtualenv)"
fi
}

ps1() {
# Check the exit code of the previous command and display different
# colors in the prompt accordingly.
Expand All @@ -137,7 +147,16 @@ __powerline() {
local BG_EXIT="$BG_RED"
fi

PS1="$BG_BASE1$FG_BASE3 \w $RESET"
PS1=""
# the indirection is a security measure, explained below
if shopt -q promptvars; then
__powerline_virtualenv="$(__virtualenv)"
PS1+="$BG_VIOLET$FG_BASE3\${__powerline_virtualenv}$RESET"
else
# promptvars is disabled. Avoid creating unnecessary env var.
PS1+="$BG_VIOLET$FG_BASE3$(__virtualenv)$RESET"
fi
PS1+="$BG_BASE1$FG_BASE3 \w $RESET"
# Bash by default expands the content of PS1 unless promptvars is disabled.
# We must use another layer of reference to prevent expanding any user
# provided strings, which would cause security issues.
Expand Down