blob: 81598decc2086ed048313d9f8cf6f28d77a5072d [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="↑"
Travis Swicegood51416052014-04-01 10:41:00 -050016if [[ -z "$THEME_SCM_TAG_PREFIX" ]]; then
17 SCM_TAG_PREFIX="tag > "
18else
19 SCM_TAG_PREFIX="$THEME_SCM_TAG_PREFIX"
20fi
21
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010022SCM_THEME_PROMPT_CLEAN=""
23SCM_THEME_PROMPT_DIRTY=""
24SCM_THEME_PROMPT_COLOR=238
Eduardo Bellido Bellido91538bc2013-11-15 20:57:27 +010025SCM_THEME_PROMPT_CLEAN_COLOR=231
Travis Swicegood16704a62013-11-25 18:02:58 -060026SCM_THEME_PROMPT_DIRTY_COLOR=196
27SCM_THEME_PROMPT_STAGED_COLOR=220
28SCM_THEME_PROMPT_UNTRACKED_COLOR=033
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010029
30CWD_THEME_PROMPT_COLOR=240
31
32LAST_STATUS_THEME_PROMPT_COLOR=52
33
34function set_rgb_color {
35 if [[ "${1}" != "-" ]]; then
36 fg="38;5;${1}"
37 fi
38 if [[ "${2}" != "-" ]]; then
39 bg="48;5;${2}"
40 [[ -n "${fg}" ]] && bg=";${bg}"
41 fi
42 echo -e "\[\033[${fg}${bg}m\]"
43}
44
45function powerline_shell_prompt {
46 if [[ -n "${SSH_CLIENT}" ]]; then
47 SHELL_PROMPT="${bold_white}$(set_rgb_color - ${SHELL_SSH_THEME_PROMPT_COLOR}) ${SHELL_SSH_CHAR}\u@\h ${normal}"
48 LAST_THEME_COLOR=${SHELL_SSH_THEME_PROMPT_COLOR}
49 else
50 SHELL_PROMPT="${bold_white}$(set_rgb_color - ${SHELL_THEME_PROMPT_COLOR}) \u ${normal}"
51 LAST_THEME_COLOR=${SHELL_THEME_PROMPT_COLOR}
52 fi
53}
54
55function powerline_virtualenv_prompt {
56 if [[ -n "$VIRTUAL_ENV" ]]; then
57 virtualenv=$(basename "$VIRTUAL_ENV")
58 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}"
59 LAST_THEME_COLOR=${VIRTUALENV_THEME_PROMPT_COLOR}
60 else
61 VIRTUALENV_PROMPT=""
62 fi
63}
64
65function powerline_scm_prompt {
66 scm_prompt_vars
Travis Swicegood16704a62013-11-25 18:02:58 -060067 local git_status_output
68 git_status_output=$(git status 2> /dev/null )
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010069
70 if [[ "${SCM_NONE_CHAR}" != "${SCM_CHAR}" ]]; then
71 if [[ "${SCM_DIRTY}" -eq 1 ]]; then
Travis Swicegood16704a62013-11-25 18:02:58 -060072 if [ -n "$(echo $git_status_output | grep 'Changes not staged')" ]; then
73 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})"
74 elif [ -n "$(echo $git_status_output | grep 'Changes to be committed')" ]; then
75 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_STAGED_COLOR} ${SCM_THEME_PROMPT_COLOR})"
76 elif [ -n "$(echo $git_status_output | grep 'Untracked files')" ]; then
77 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_UNTRACKED_COLOR} ${SCM_THEME_PROMPT_COLOR})"
Travis Swicegood1a885b92013-11-25 18:06:33 -060078 else
79 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})"
Travis Swicegood16704a62013-11-25 18:02:58 -060080 fi
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010081 else
82 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_CLEAN_COLOR} ${SCM_THEME_PROMPT_COLOR})"
83 fi
Travis Swicegood51416052014-04-01 10:41:00 -050084 if [[ "${SCM_GIT_CHAR}" == "${SCM_CHAR}" ]]; then
85 local tag=""
86 if [[ $SCM_IS_TAG -eq "1" ]]; then
87 tag=$SCM_TAG_PREFIX
88 fi
89 SCM_PROMPT+=" ${SCM_CHAR}${tag}${SCM_BRANCH}${SCM_STATE}${SCM_GIT_BEHIND}${SCM_GIT_AHEAD}${SCM_GIT_STASH}"
90 fi
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010091 SCM_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${SCM_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}${SCM_PROMPT} ${normal}"
92 LAST_THEME_COLOR=${SCM_THEME_PROMPT_COLOR}
93 else
94 SCM_PROMPT=""
95 fi
96}
97
98function powerline_cwd_prompt {
99 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}"
100 LAST_THEME_COLOR=${CWD_THEME_PROMPT_COLOR}
101}
102
103function powerline_last_status_prompt {
104 if [[ "$1" -eq 0 ]]; then
105 LAST_STATUS_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} -)${THEME_PROMPT_SEPARATOR}${normal}"
106 else
107 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}"
108 fi
109}
110
111function powerline_prompt_command() {
112 local LAST_STATUS="$?"
113
114 powerline_shell_prompt
115 powerline_virtualenv_prompt
116 powerline_scm_prompt
117 powerline_cwd_prompt
118 powerline_last_status_prompt LAST_STATUS
119
120 PS1="${SHELL_PROMPT}${VIRTUALENV_PROMPT}${SCM_PROMPT}${CWD_PROMPT}${LAST_STATUS_PROMPT} "
121}
122
123PROMPT_COMMAND=powerline_prompt_command
124