From e412c1f327d9db7a50e3d867a60bdef4bb7c66ea Mon Sep 17 00:00:00 2001
From: Marco Pantaleoni <marco.pantaleoni@gmail.com>
Date: Mon, 10 Jul 2017 19:52:19 +0200
Subject: [PATCH] Add support for Python virtualenv.

Based on ideas from intinig/bash-powerline and Hexcles/bash-powerline:

- https://github.com/riobard/bash-powerline/pull/11/commits/665c2113bc0b0f13990db29a9aaa62b342156572
- https://github.com/riobard/bash-powerline/pull/9/commits/43e67c6e22b414582a4a07261536c13e8f42f96d
---
 README.md         |  1 +
 bash-powerline.sh | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index fb7c165..b2c9f11 100644
--- a/README.md
+++ b/README.md
@@ -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)
diff --git a/bash-powerline.sh b/bash-powerline.sh
index cb69e2b..9106dcd 100644
--- a/bash-powerline.sh
+++ b/bash-powerline.sh
@@ -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
@@ -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. 
@@ -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.