-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_prompt
167 lines (142 loc) · 5.34 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env bash
###############################################################################
# See top of .bashrc for usage instructions and overview.
#
# This uses the ANSI colors set by your terminal.
# See this post for an overview: https://chrisyeh96.github.io/2020/03/28/terminal-colors.html
# I recommend using the Nord.itermcolors theme with iTerm in my dotfiles repo.
# Set it in iTerm's Preferences -> Profiles -> Colors -> Color Presets.
#
# The git prompts are mostly copied from: https://github.com/alrra/dotfiles
# Some bits and pieces from: https://github.com/mathiasbynens/dotfiles
###############################################################################
enable_color_support() {
if [[ $COLORTERM == gnome-* && $TERM == xterm ]] \
&& infocmp gnome-256color &> /dev/null; then
export TERM="gnome-256color"
fi
}
# Command line in git repos show helpful info
get_git_repository_details() {
local branchName=""
local tmp=""
# Check if the current directory is in a Git repository.
git rev-parse &>/dev/null || return
# Check if in `.git/` directory (some of the following
# checks don't make sense/won't work in the `.git` directory).
[ "$(git rev-parse --is-inside-git-dir)" == "false" ] || return
# Check for what branch we’re on.
# Get the short symbolic ref. If HEAD isn’t a symbolic ref, get a
# tracking remote branch or tag. Otherwise, get the
# short SHA for the latest commit, or give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git describe --all --exact-match HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')"
# Check for uncommitted changes in the index.
if ! git diff --quiet --ignore-submodules --cached; then
tmp+="+"
fi
# Check for unstaged changes.
if ! git diff-files --quiet --ignore-submodules --; then
tmp+="!"
fi
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
tmp+="?"
fi
# Check for stashed files.
if git rev-parse --verify refs/stash &>/dev/null; then
tmp+="$"
fi
# Prepend a space if tmp has non-zero length
[ -n "$tmp" ] && tmp=" [$tmp]"
echo -e "${1}${branchName}${2}${tmp}"
}
set_prompts() {
# ASCI escape codes for terminal graphics: the escape literal (in Bash, \e)
# followed by some numbers, and punctuated with "m" forms a Select Graphic
# Rendition (SGR) control sequence.
local reset="\e[0m"
local bold="\e[1m"
local black="\e[30m" # Unused
local red="\e[31m"
local green="\e[32m"
local yellow="\e[33m"
local blue="\e[34m"
local magenta="\e[35m"
local cyan="\e[36m" # Unused
local white="\e[37m"
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
local userStyle=${bold}${red}
else
local userStyle=${bold}${yellow}
fi
# Highlight the hostname when connected via SSH.
if [[ "${SSH_TTY}" ]]; then
local hostStyle=${bold}${red}
else
local hostStyle=${yellow}
fi
# Prompt Statement variables. See: http://ss64.com/bash/syntax-prompt.html
# PS1 is the default interactive prompt and the one we see the most often.
# PS2, PS3, and PS4 also exist, but I haven't customized these; I just
# copied from: https://github.com/alrra/dotfiles
# | PS1 - Default interactive prompt
PS1="\[\033]0;\W\007\]" # Terminal title and prompt
PS1+="$reset\n" # Newline
PS1+="$userStyle\u" # Username
PS1+="$white in "
PS1+="$green\w" # Working directory full path
PS1+="\$(get_git_repository_details \"$white on $magenta\" \"$blue\")"
PS1+="\n"
PS1+="\[$reset\]\[$white\]\$ \[$reset\]"
export PS1
# | PS2 - Continuation interactive prompt
PS2="⚡ "
export PS2
# | PS4 - Debug prompt
# e.g:
#
# The GNU `date` command has the `%N` interpreted sequence while
# other implementations don't (on macOS `gdate` can be used instead
# of the native `date` if the `coreutils` package has been installed).
#
# local dateCmd=""
#
# if [ "$(date +%N)" != "N" ] || \
# [ ! -x "$(command -v "gdate")" ]; then
# dateCmd="date +%s.%N"
# else
# dateCmd="gdate +%s.%N"
# fi
#
# PS4="+$( tput cr && tput cuf 6 &&
# printf "$yellow %s $green%6s $reset" "$($dateCmd)" "[$LINENO]" )"
#
# PS4 output:
#
# ++ 1357074705.875970000 [123] '[' 1 == 0 ']'
# └──┬─┘└────┬───┘ └───┬───┘ └──┬─┘ └──────┬─────┘
# │ │ │ │ │
# │ │ │ │ └─ command
# │ │ │ └─ line number
# │ │ └─ nanoseconds
# │ └─ seconds since 1970-01-01 00:00:00 UTC
# └─ depth-level of the subshell
PS4="+$( tput cr 2> /dev/null;
tput cuf 6 2> /dev/null;
printf "%s" "$reset" )"
export PS4
}
main() {
enable_color_support
set_prompts
}
main
###############################################################################
# Cleanup.
unset -f enable_color_support
unset -f main
unset -f set_prompts