-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.sh
executable file
·64 lines (54 loc) · 1.99 KB
/
prompt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# colors
export PS1_GREY="\[$(tput bold; tput setaf 0)\]"
export PS1_GREEN="\[$(tput bold; tput setaf 2)\]"
export PS1_YELLOW="\[$(tput bold; tput setaf 3)\]"
export PS1_MAGENTA="\[$(tput bold; tput setaf 5)\]"
export PS1_CYAN="\[$(tput bold; tput setaf 6)\]"
export PS1_WHITE="\[$(tput bold; tput setaf 7)\]"
export PS1_RED="\[$(tput bold; tput setaf 1)\]"
export PS1_RESET="\[$(tput sgr0)\]"
export GIT_PROMPT_DIRTY="±"
export GIT_PROMPT_UNTRACKED="+"
export GIT_PROMPT_CLEAN=""
__git_ps1 ()
{
local b="$(git symbolic-ref HEAD 2>/dev/null)";
if [ -n "$b" ]; then
printf "%s" "${b##refs/heads/}";
fi
}
__parse_git_status () {
gitstat=$(git status 2>/dev/null | grep '\(# Untracked\|# Changes\|# Changed but not updated:\|modified:\)')
if [[ $(echo ${gitstat} | grep -c "^# Changes to be committed:\|modified:") > 0 ]]; then
echo -n "$GIT_PROMPT_DIRTY"
fi
if [[ $(echo ${gitstat} | grep -c "^\(# Untracked files:\|# Changed but not updated:\)") > 0 ]]; then
echo -n "$GIT_PROMPT_UNTRACKED"
fi
if [[ $(echo ${gitstat} | wc -l | tr -d ' ') == 0 ]]; then
echo -n "$GIT_PROMPT_CLEAN"
fi
}
# function to set PS1
function _bash_prompt(){
# git info
if [[ $? == 0 ]]; then
local EXIT_CODE_PROMPT="${PS1_GREY}[${PS1_RESET}$?${PS1_GREY}]"
else
local EXIT_CODE_PROMPT="${PS1_GREY}[${PS1_RED}$?${PS1_GREY}]"
fi
local GIT_INFO=$(git branch &>/dev/null && echo "${PS1_CYAN}git${PS1_WHITE}: ($(__git_ps1 '%s'))$(__parse_git_status)")
# add <esc>k<esc>\ to PS1 if screen is running
# see man(1) screen, section TITLES for more
if [[ "$TERM" == screen* ]]; then
local SCREEN_ESC='\[\ek\e\\\]'
else
local SCREEN_ESC=''
fi
# finally, set PS1
PS1="\n${PS1_MAGENTA}\u ${PS1_GREY}at${PS1_YELLOW} \h ${PS1_GREY}in${PS1_GREEN} \w ${EXIT_CODE_PROMPT} ${GIT_INFO}\
\n${SCREEN_ESC}${PS1_WHITE}\\\$${PS1_RESET} "
}
# call _bash_prompt() each time the prompt is refreshed
export PROMPT_COMMAND=_bash_prompt