Eduardo Bellido Bellido | 68dc676 | 2013-11-13 00:01:09 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | THEME_PROMPT_SEPARATOR="" |
| 4 | |
| 5 | SHELL_SSH_CHAR=" " |
| 6 | SHELL_THEME_PROMPT_COLOR=32 |
| 7 | SHELL_SSH_THEME_PROMPT_COLOR=208 |
| 8 | |
| 9 | VIRTUALENV_CHAR="ⓔ " |
| 10 | VIRTUALENV_THEME_PROMPT_COLOR=35 |
| 11 | |
| 12 | SCM_NONE_CHAR="" |
| 13 | SCM_GIT_CHAR=" " |
| 14 | SCM_GIT_BEHIND_CHAR="↓" |
| 15 | SCM_GIT_AHEAD_CHAR="↑" |
| 16 | SCM_THEME_PROMPT_CLEAN="" |
| 17 | SCM_THEME_PROMPT_DIRTY="" |
| 18 | SCM_THEME_PROMPT_COLOR=238 |
| 19 | SCM_THEME_PROMPT_CLEAN_COLOR=267 |
| 20 | SCM_THEME_PROMPT_DIRTY_COLOR=220 |
| 21 | |
| 22 | CWD_THEME_PROMPT_COLOR=240 |
| 23 | |
| 24 | LAST_STATUS_THEME_PROMPT_COLOR=52 |
| 25 | |
| 26 | function set_rgb_color { |
| 27 | if [[ "${1}" != "-" ]]; then |
| 28 | fg="38;5;${1}" |
| 29 | fi |
| 30 | if [[ "${2}" != "-" ]]; then |
| 31 | bg="48;5;${2}" |
| 32 | [[ -n "${fg}" ]] && bg=";${bg}" |
| 33 | fi |
| 34 | echo -e "\[\033[${fg}${bg}m\]" |
| 35 | } |
| 36 | |
| 37 | function powerline_shell_prompt { |
| 38 | if [[ -n "${SSH_CLIENT}" ]]; then |
| 39 | SHELL_PROMPT="${bold_white}$(set_rgb_color - ${SHELL_SSH_THEME_PROMPT_COLOR}) ${SHELL_SSH_CHAR}\u@\h ${normal}" |
| 40 | LAST_THEME_COLOR=${SHELL_SSH_THEME_PROMPT_COLOR} |
| 41 | else |
| 42 | SHELL_PROMPT="${bold_white}$(set_rgb_color - ${SHELL_THEME_PROMPT_COLOR}) \u ${normal}" |
| 43 | LAST_THEME_COLOR=${SHELL_THEME_PROMPT_COLOR} |
| 44 | fi |
| 45 | } |
| 46 | |
| 47 | function powerline_virtualenv_prompt { |
| 48 | if [[ -n "$VIRTUAL_ENV" ]]; then |
| 49 | virtualenv=$(basename "$VIRTUAL_ENV") |
| 50 | VIRTUALENV_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${VIRTUALENV_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}$(set_rgb_color - ${VIRTUALENV_THEME_PROMPT_COLOR}) ${VIRTUALENV_CHAR}$virtualenv ${normal}" |
| 51 | LAST_THEME_COLOR=${VIRTUALENV_THEME_PROMPT_COLOR} |
| 52 | else |
| 53 | VIRTUALENV_PROMPT="" |
| 54 | fi |
| 55 | } |
| 56 | |
| 57 | function powerline_scm_prompt { |
| 58 | scm_prompt_vars |
| 59 | |
| 60 | if [[ "${SCM_NONE_CHAR}" != "${SCM_CHAR}" ]]; then |
| 61 | if [[ "${SCM_DIRTY}" -eq 1 ]]; then |
| 62 | SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})" |
| 63 | else |
| 64 | SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_CLEAN_COLOR} ${SCM_THEME_PROMPT_COLOR})" |
| 65 | fi |
| 66 | [[ "${SCM_GIT_CHAR}" == "${SCM_CHAR}" ]] && SCM_PROMPT+=" ${SCM_CHAR}${SCM_BRANCH}${SCM_STATE}${SCM_GIT_BEHIND}${SCM_GIT_AHEAD}${SCM_GIT_STASH}" |
| 67 | SCM_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${SCM_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}${SCM_PROMPT} ${normal}" |
| 68 | LAST_THEME_COLOR=${SCM_THEME_PROMPT_COLOR} |
| 69 | else |
| 70 | SCM_PROMPT="" |
| 71 | fi |
| 72 | } |
| 73 | |
| 74 | function powerline_cwd_prompt { |
| 75 | CWD_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${CWD_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}$(set_rgb_color - ${CWD_THEME_PROMPT_COLOR}) \w ${normal}$(set_rgb_color ${CWD_THEME_PROMPT_COLOR} -)${normal}" |
| 76 | LAST_THEME_COLOR=${CWD_THEME_PROMPT_COLOR} |
| 77 | } |
| 78 | |
| 79 | function powerline_last_status_prompt { |
| 80 | if [[ "$1" -eq 0 ]]; then |
| 81 | LAST_STATUS_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} -)${THEME_PROMPT_SEPARATOR}${normal}" |
| 82 | else |
| 83 | LAST_STATUS_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${LAST_STATUS_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}$(set_rgb_color - ${LAST_STATUS_THEME_PROMPT_COLOR}) ${LAST_STATUS} ${normal}$(set_rgb_color ${LAST_STATUS_THEME_PROMPT_COLOR} -)${THEME_PROMPT_SEPARATOR}${normal}" |
| 84 | fi |
| 85 | } |
| 86 | |
| 87 | function powerline_prompt_command() { |
| 88 | local LAST_STATUS="$?" |
| 89 | |
| 90 | powerline_shell_prompt |
| 91 | powerline_virtualenv_prompt |
| 92 | powerline_scm_prompt |
| 93 | powerline_cwd_prompt |
| 94 | powerline_last_status_prompt LAST_STATUS |
| 95 | |
| 96 | PS1="${SHELL_PROMPT}${VIRTUALENV_PROMPT}${SCM_PROMPT}${CWD_PROMPT}${LAST_STATUS_PROMPT} " |
| 97 | } |
| 98 | |
| 99 | PROMPT_COMMAND=powerline_prompt_command |
| 100 | |