blob: 012f93338eb0474ceb4c3d193182019e80a339dc [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 {
Travis Swicegoodaeb08ae2014-04-05 09:14:55 -050056 local environ=""
57
58 if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
59 environ="conda: $CONDA_DEFAULT_ENV"
60 elif [[ -n "$VIRTUAL_ENV" ]]; then
61 environ=$(basename "$VIRTUAL_ENV")
62 fi
63
64 if [[ -n "$environ" ]]; then
65 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}$environ ${normal}"
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010066 LAST_THEME_COLOR=${VIRTUALENV_THEME_PROMPT_COLOR}
67 else
68 VIRTUALENV_PROMPT=""
69 fi
70}
71
72function powerline_scm_prompt {
73 scm_prompt_vars
Travis Swicegood16704a62013-11-25 18:02:58 -060074 local git_status_output
75 git_status_output=$(git status 2> /dev/null )
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010076
77 if [[ "${SCM_NONE_CHAR}" != "${SCM_CHAR}" ]]; then
78 if [[ "${SCM_DIRTY}" -eq 1 ]]; then
Travis Swicegood16704a62013-11-25 18:02:58 -060079 if [ -n "$(echo $git_status_output | grep 'Changes not staged')" ]; then
80 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})"
81 elif [ -n "$(echo $git_status_output | grep 'Changes to be committed')" ]; then
82 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_STAGED_COLOR} ${SCM_THEME_PROMPT_COLOR})"
83 elif [ -n "$(echo $git_status_output | grep 'Untracked files')" ]; then
84 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_UNTRACKED_COLOR} ${SCM_THEME_PROMPT_COLOR})"
Travis Swicegood1a885b92013-11-25 18:06:33 -060085 else
86 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_DIRTY_COLOR} ${SCM_THEME_PROMPT_COLOR})"
Travis Swicegood16704a62013-11-25 18:02:58 -060087 fi
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010088 else
89 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_CLEAN_COLOR} ${SCM_THEME_PROMPT_COLOR})"
90 fi
Travis Swicegood51416052014-04-01 10:41:00 -050091 if [[ "${SCM_GIT_CHAR}" == "${SCM_CHAR}" ]]; then
92 local tag=""
93 if [[ $SCM_IS_TAG -eq "1" ]]; then
94 tag=$SCM_TAG_PREFIX
95 fi
96 SCM_PROMPT+=" ${SCM_CHAR}${tag}${SCM_BRANCH}${SCM_STATE}${SCM_GIT_BEHIND}${SCM_GIT_AHEAD}${SCM_GIT_STASH}"
97 fi
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010098 SCM_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${SCM_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}${SCM_PROMPT} ${normal}"
99 LAST_THEME_COLOR=${SCM_THEME_PROMPT_COLOR}
100 else
101 SCM_PROMPT=""
102 fi
103}
104
105function powerline_cwd_prompt {
106 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}"
107 LAST_THEME_COLOR=${CWD_THEME_PROMPT_COLOR}
108}
109
110function powerline_last_status_prompt {
111 if [[ "$1" -eq 0 ]]; then
112 LAST_STATUS_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} -)${THEME_PROMPT_SEPARATOR}${normal}"
113 else
114 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}"
115 fi
116}
117
118function powerline_prompt_command() {
119 local LAST_STATUS="$?"
120
121 powerline_shell_prompt
122 powerline_virtualenv_prompt
123 powerline_scm_prompt
124 powerline_cwd_prompt
125 powerline_last_status_prompt LAST_STATUS
126
127 PS1="${SHELL_PROMPT}${VIRTUALENV_PROMPT}${SCM_PROMPT}${CWD_PROMPT}${LAST_STATUS_PROMPT} "
128}
129
130PROMPT_COMMAND=powerline_prompt_command
131