blob: 518f054c1afbb58e75dd051d311bc7f4c7e1dd26 [file] [log] [blame]
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +01001#!/usr/bin/env bash
2
3THEME_PROMPT_SEPARATOR=""
4
5SHELL_SSH_CHAR=" "
6SHELL_THEME_PROMPT_COLOR=32
7SHELL_SSH_THEME_PROMPT_COLOR=208
8
9VIRTUALENV_CHAR="ⓔ "
10VIRTUALENV_THEME_PROMPT_COLOR=35
11
12SCM_NONE_CHAR=""
13SCM_GIT_CHAR=" "
14SCM_GIT_BEHIND_CHAR="↓"
15SCM_GIT_AHEAD_CHAR="↑"
16SCM_THEME_PROMPT_CLEAN=""
17SCM_THEME_PROMPT_DIRTY=""
18SCM_THEME_PROMPT_COLOR=238
Eduardo Bellido Bellido91538bc2013-11-15 20:57:27 +010019SCM_THEME_PROMPT_CLEAN_COLOR=231
Travis Swicegood16704a62013-11-25 18:02:58 -060020SCM_THEME_PROMPT_DIRTY_COLOR=196
21SCM_THEME_PROMPT_STAGED_COLOR=220
22SCM_THEME_PROMPT_UNTRACKED_COLOR=033
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010023
24CWD_THEME_PROMPT_COLOR=240
25
26LAST_STATUS_THEME_PROMPT_COLOR=52
27
28function set_rgb_color {
29 if [[ "${1}" != "-" ]]; then
30 fg="38;5;${1}"
31 fi
32 if [[ "${2}" != "-" ]]; then
33 bg="48;5;${2}"
34 [[ -n "${fg}" ]] && bg=";${bg}"
35 fi
36 echo -e "\[\033[${fg}${bg}m\]"
37}
38
39function powerline_shell_prompt {
40 if [[ -n "${SSH_CLIENT}" ]]; then
41 SHELL_PROMPT="${bold_white}$(set_rgb_color - ${SHELL_SSH_THEME_PROMPT_COLOR}) ${SHELL_SSH_CHAR}\u@\h ${normal}"
42 LAST_THEME_COLOR=${SHELL_SSH_THEME_PROMPT_COLOR}
43 else
44 SHELL_PROMPT="${bold_white}$(set_rgb_color - ${SHELL_THEME_PROMPT_COLOR}) \u ${normal}"
45 LAST_THEME_COLOR=${SHELL_THEME_PROMPT_COLOR}
46 fi
47}
48
49function powerline_virtualenv_prompt {
50 if [[ -n "$VIRTUAL_ENV" ]]; then
51 virtualenv=$(basename "$VIRTUAL_ENV")
52 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}"
53 LAST_THEME_COLOR=${VIRTUALENV_THEME_PROMPT_COLOR}
54 else
55 VIRTUALENV_PROMPT=""
56 fi
57}
58
59function powerline_scm_prompt {
60 scm_prompt_vars
Travis Swicegood16704a62013-11-25 18:02:58 -060061 local git_status_output
62 git_status_output=$(git status 2> /dev/null )
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010063
64 if [[ "${SCM_NONE_CHAR}" != "${SCM_CHAR}" ]]; then
65 if [[ "${SCM_DIRTY}" -eq 1 ]]; then
Travis Swicegood16704a62013-11-25 18:02:58 -060066 if [ -n "$(echo $git_status_output | grep 'Changes not staged')" ]; then
67 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})"
68 elif [ -n "$(echo $git_status_output | grep 'Changes to be committed')" ]; then
69 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_STAGED_COLOR} ${SCM_THEME_PROMPT_COLOR})"
70 elif [ -n "$(echo $git_status_output | grep 'Untracked files')" ]; then
71 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_UNTRACKED_COLOR} ${SCM_THEME_PROMPT_COLOR})"
Travis Swicegood1a885b92013-11-25 18:06:33 -060072 else
73 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})"
Travis Swicegood16704a62013-11-25 18:02:58 -060074 fi
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010075 else
76 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_CLEAN_COLOR} ${SCM_THEME_PROMPT_COLOR})"
77 fi
78 [[ "${SCM_GIT_CHAR}" == "${SCM_CHAR}" ]] && SCM_PROMPT+=" ${SCM_CHAR}${SCM_BRANCH}${SCM_STATE}${SCM_GIT_BEHIND}${SCM_GIT_AHEAD}${SCM_GIT_STASH}"
79 SCM_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${SCM_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}${SCM_PROMPT} ${normal}"
80 LAST_THEME_COLOR=${SCM_THEME_PROMPT_COLOR}
81 else
82 SCM_PROMPT=""
83 fi
84}
85
86function powerline_cwd_prompt {
87 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}"
88 LAST_THEME_COLOR=${CWD_THEME_PROMPT_COLOR}
89}
90
91function powerline_last_status_prompt {
92 if [[ "$1" -eq 0 ]]; then
93 LAST_STATUS_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} -)${THEME_PROMPT_SEPARATOR}${normal}"
94 else
95 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}"
96 fi
97}
98
99function powerline_prompt_command() {
100 local LAST_STATUS="$?"
101
102 powerline_shell_prompt
103 powerline_virtualenv_prompt
104 powerline_scm_prompt
105 powerline_cwd_prompt
106 powerline_last_status_prompt LAST_STATUS
107
108 PS1="${SHELL_PROMPT}${VIRTUALENV_PROMPT}${SCM_PROMPT}${CWD_PROMPT}${LAST_STATUS_PROMPT} "
109}
110
111PROMPT_COMMAND=powerline_prompt_command
112