-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_prompt
102 lines (86 loc) · 3.44 KB
/
.bash_prompt
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Set a more descriptive prompt
# Revert to lightweight prompt on low-resource machines
lite=false
# Set remote to `true` to show the hostname
remote=false
# Abbreviate long directory names
PROMPT_DIRTRIM=5
shorten-prompt() {
export PROMPT_DIRTRIM=$(($PROMPT_DIRTRIM - 1))
}
declare -A prompt
prompt['line']="\342\224\200"
# `\x01\` and `\x02` ensure white space is not counted on measured text width
prompt['normal']="\x01${COLOR_NORMAL@E}\x02"
prompt['gray']="\x01${COLOR_GRAY_FG@E}\x02"
prompt['red']="\x01${COLOR_RED_FG@E}\x02"
prompt['green']="\x01${COLOR_GREEN_FG@E}\x02"
prompt['lime']="\x01${COLOR_LIME_FG@E}\x02"
prompt['red_bold']="\x01${COLOR_RED_FG_BOLD@E}\x02"
prompt['red_bold']="\x01${COLOR_RED_FG_BOLD@E}\x02"
prompt['green_bold']="\x01${COLOR_GREEN_FG_BOLD@E}\x02"
prompt['yellow_bold']="\x01${COLOR_YELLOW_FG_BOLD@E}\x02"
prompt['blue_bold']="\x01${COLOR_BLUE_FG_BOLD@E}\x02"
prompt['white_bold']="\x01${COLOR_WHITE_FG_BOLD@E}\x02"
_form_prompt_line1() {
local _prompt_open_l1="${prompt['gray']}\342\224\214${prompt['line']}"
if [ $remote == true ]; then
local _prompt_user="[${prompt['lime']}\u${prompt['green']}@\h${prompt['gray']}]${prompt['line']}"
else
local _prompt_user="[${prompt['green']}\u${prompt['gray']}]${prompt['line']}"
fi
local _prompt_dir="[${prompt['green']}\w${prompt['gray']}]"
local _prompt_x="\342\234\227"
local _prompt_error="\$([[ \$? != 0 ]] && echo \"[${prompt['red']}${_prompt_x}${prompt['gray']}]${prompt['line']}\")"
local _prompt_git="\$(_form_git_prompt)"
local _prompt_venv="\$(_form_venv_prompt)"
echo "${_prompt_open_l1}${_prompt_error}${_prompt_user}${_prompt_dir}${_prompt_git}${_prompt_venv}${prompt['normal']}"
}
_form_prompt_line2() {
local _prompt_open_l2="${prompt['gray']}\342\224\224${prompt['line']}${prompt['line']}\342\225\274"
echo "${_prompt_open_l2} ${prompt['gray']}\$${prompt['normal']} "
}
# Color the Git prompt
# (adapted from Nitesh Tosniwal: https://github.com/sudonitesh/beautiful-bash)
_assign_prompt_git_color() {
local status=$1
# Staged and ready to commit changes
local staged=$([[ -n $(grep -E '^[MADRC]' <<< "$status") ]] && echo true)
# Unstaged and untracked changes
local unstaged=$([[ -n $(grep -E '^.[MD]' <<< "$status") ]] && echo true)
local untracked=$([[ -n $(grep -E '^\?\?' <<< "$status") ]] && echo true)
local dirty=$([ -n "$unstaged" ] || [ -n "$untracked" ] && echo true)
# Committed changes ready to be pushed
local needs_push=$([[ -n $(egrep 'ahead' <<< "$status") ]] && echo true)
if [[ -n $staged ]] && [[ -n $dirty ]]; then
echo "${prompt['yellow_bold']}"
elif [[ -n $staged ]]; then
echo "${prompt['green_bold']}"
elif [[ -n $dirty ]]; then
echo "${prompt['red_bold']}"
elif [[ -n $needs_push ]]; then
echo "${prompt['blue_bold']}"
else
echo "${prompt['white_bold']}"
fi
}
_form_git_prompt() {
local git_prompt=$(__git_ps1 '%s')
if [ -n "$git_prompt" ]; then
local status="$(git status --porcelain --branch 2> /dev/null)"
local color="$(_assign_prompt_git_color "$status")"
echo "${prompt['line']}(${color}${git_prompt}${prompt['gray']})"
fi
}
_form_venv_prompt() {
if [ -n "$VIRTUAL_ENV" ]; then
# Just display the virtual environment name (not path)
echo "${prompt['line']}(${prompt['green']}${VIRTUAL_ENV##*/}${prompt['gray']})"
fi
}
if [ $lite == true ]; then
PS1='[\u \W$(__git_ps1 " (%s)")]\$ '
else
PS1="\$(printf \"$(_form_prompt_line1)\n$(_form_prompt_line2)\")"
fi