blob: ab179aa46d4ec076cab61800c03d45d9561b9006 [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})"
72 fi
Eduardo Bellido Bellido68dc6762013-11-13 00:01:09 +010073 else
74 SCM_PROMPT="$(set_rgb_color ${SCM_THEME_PROMPT_CLEAN_COLOR} ${SCM_THEME_PROMPT_COLOR})"
75 fi
76 [[ "${SCM_GIT_CHAR}" == "${SCM_CHAR}" ]] && SCM_PROMPT+=" ${SCM_CHAR}${SCM_BRANCH}${SCM_STATE}${SCM_GIT_BEHIND}${SCM_GIT_AHEAD}${SCM_GIT_STASH}"
77 SCM_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} ${SCM_THEME_PROMPT_COLOR})${THEME_PROMPT_SEPARATOR}${normal}${SCM_PROMPT} ${normal}"
78 LAST_THEME_COLOR=${SCM_THEME_PROMPT_COLOR}
79 else
80 SCM_PROMPT=""
81 fi
82}
83
84function powerline_cwd_prompt {
85 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}"
86 LAST_THEME_COLOR=${CWD_THEME_PROMPT_COLOR}
87}
88
89function powerline_last_status_prompt {
90 if [[ "$1" -eq 0 ]]; then
91 LAST_STATUS_PROMPT="$(set_rgb_color ${LAST_THEME_COLOR} -)${THEME_PROMPT_SEPARATOR}${normal}"
92 else
93 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}"
94 fi
95}
96
97function powerline_prompt_command() {
98 local LAST_STATUS="$?"
99
100 powerline_shell_prompt
101 powerline_virtualenv_prompt
102 powerline_scm_prompt
103 powerline_cwd_prompt
104 powerline_last_status_prompt LAST_STATUS
105
106 PS1="${SHELL_PROMPT}${VIRTUALENV_PROMPT}${SCM_PROMPT}${CWD_PROMPT}${LAST_STATUS_PROMPT} "
107}
108
109PROMPT_COMMAND=powerline_prompt_command
110