blob: 3d0bf80e2fddf3513fc25431a3a5c747bf8e51e7 [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
19SCM_THEME_PROMPT_CLEAN_COLOR=267
20SCM_THEME_PROMPT_DIRTY_COLOR=220
21
22CWD_THEME_PROMPT_COLOR=240
23
24LAST_STATUS_THEME_PROMPT_COLOR=52
25
26function 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
37function 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
47function 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
57function 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
74function 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
79function 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
87function 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
99PROMPT_COMMAND=powerline_prompt_command
100