back to listing index

paulirish/dotfiles

[web search]
Original source (github.com)
Tags: bash examples shell dotfiles terminal-prompt
Clipped on: 2017-10-08

1 # This prompt inspired by gf3, sindresorhus, alrra, and mathiasbynens.
2 # but customized to me. <3
3
4 default_username='paulirish'
5
6
7 if which thefuck > /dev/null; then
8 eval "$(thefuck --alias)"
9 fi;
10
11 # Automatically trim long paths in the prompt (requires Bash 4.x)
12 PROMPT_DIRTRIM=2
13
14 if [[ -n "$ZSH_VERSION" ]]; then # quit now if in zsh
15 return 1 2> /dev/null || exit 1;
16 fi;
17
18
19 if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
20 export TERM=gnome-256color
21 elif infocmp xterm-256color >/dev/null 2>&1; then
22 export TERM=xterm-256color
23 fi
24
25
26 set_prompts() {
27
28 local black="" blue="" bold="" cyan="" green="" orange="" \
29 purple="" red="" reset="" white="" yellow=""
30
31 local dateCmd=""
32
33 if [ -x /usr/bin/tput ] && tput setaf 1 &> /dev/null; then
34
35 tput sgr0 # Reset colors
36
37 bold=$(tput bold)
38 reset=$(tput sgr0)
39
40 # Solarized colors
41 # (https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized#the-values)
42 black=$(tput setaf 0)
43 blue=$(tput setaf 33)
44 cyan=$(tput setaf 37)
45 green=$(tput setaf 190)
46 orange=$(tput setaf 172)
47 purple=$(tput setaf 141)
48 red=$(tput setaf 124)
49 violet=$(tput setaf 61)
50 magenta=$(tput setaf 9)
51 white=$(tput setaf 8)
52 yellow=$(tput setaf 136)
53
54 else
55
56 bold=""
57 reset="\e[0m"
58
59 black="\e[1;30m"
60 blue="\e[1;34m"
61 cyan="\e[1;36m"
62 green="\e[1;32m"
63 orange="\e[1;33m"
64 purple="\e[1;35m"
65 red="\e[1;31m"
66 magenta="\e[1;31m"
67 violet="\e[1;35m"
68 white="\e[1;37m"
69 yellow="\e[1;33m"
70
71 fi
72
73 # Only show username/host if not default
74 function usernamehost() {
75
76 # Highlight the user name when logged in as root.
77 if [[ "${USER}" == *"root" ]]; then
78 userStyle="${red}";
79 else
80 userStyle="${magenta}";
81 fi;
82
83 userhost=""
84 userhost+="\[${userStyle}\]$USER "
85 userhost+="\[${white}\]at "
86 userhost+="\[${orange}\]$HOSTNAME "
87 userhost+="\[${white}\]in"
88
89 if [ $USER != "$default_username" ]; then echo $userhost ""; fi
90 }
91
92
93 function prompt_git() {
94 # this is >5x faster than mathias's.
95
96 # check if we're in a git repo. (fast)
97 git rev-parse --is-inside-work-tree &>/dev/null || return
98
99 # check for what branch we're on. (fast)
100 # if… HEAD isn’t a symbolic ref (typical branch),
101 # then… get a tracking remote branch or tag
102 # otherwise… get the short SHA for the latest commit
103 # lastly just give up.
104 branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
105 git describe --all --exact-match HEAD 2> /dev/null || \
106 git rev-parse --short HEAD 2> /dev/null || \
107 echo '(unknown)')";
108
109
110 ## early exit for Chromium & Blink repo, as the dirty check takes ~5s
111 ## also recommended (via goo.gl/wAVZLa ) : sudo sysctl kern.maxvnodes=$((512*1024))
112 repoUrl=$(git config --get remote.origin.url)
113 if grep -q chromium.googlesource.com <<<$repoUrl; then
114 dirty=" ⁂"
115 else
116
117 # check if it's dirty (slow)
118 # technique via github.com/git/git/blob/355d4e173/contrib/completion/git-prompt.sh#L472-L475
119 dirty=$(git diff --no-ext-diff --quiet --ignore-submodules --exit-code || echo -e "*")
120
121 # mathias has a few more checks some may like:
122 # github.com/mathiasbynens/dotfiles/blob/a8bd0d4300/.bash_prompt#L30-L43
123 fi
124
125
126 [ -n "${s}" ] && s=" [${s}]";
127 echo -e "${1}${branchName}${2}$dirty";
128
129 return
130 }
131
132
133
134 # ------------------------------------------------------------------
135 # | Prompt string |
136 # ------------------------------------------------------------------
137
138 PS1="\[\033]0;\w\007\]" # terminal title (set to the current working directory)
139 PS1+="\n\[$bold\]"
140 PS1+="\[$(usernamehost)\]" # username at host
141 PS1+="\[$green\]\w" # working directory
142 PS1+="\$(prompt_git \"$white on $purple\" \"$cyan\")" # git repository details
143 PS1+="\n"
144 PS1+="\[$reset$white\]\\$ \[$reset\]"
145
146 export PS1
147
148 # ------------------------------------------------------------------
149 # | Subshell prompt string |
150 # ------------------------------------------------------------------
151
152 export PS2="⚡ "
153
154 # ------------------------------------------------------------------
155 # | Debug prompt string (when using `set -x`) |
156 # ------------------------------------------------------------------
157
158 # When debugging a shell script via `set -x` this tricked-out prompt is used.
159
160 # The first character (+) is used and repeated for stack depth
161 # Then, we log the current time, filename and line number, followed by function name, followed by actual source line
162
163 # FWIW, I have spent hours attempting to get time-per-command in here, but it's not possible. ~paul
164 export PS4='+ \011\e[1;30m\t\011\e[1;34m${BASH_SOURCE}\e[0m:\e[1;36m${LINENO}\e[0m \011 ${FUNCNAME[0]:+\e[0;35m${FUNCNAME[0]}\e[1;30m()\e[0m:\011\011 }'
165
166
167 # shoutouts:
168 # https://github.com/dholm/dotshell/blob/master/.local/lib/sh/profile.sh is quite nice.
169 # zprof is also hot.
170
171 }
172
173
174
175 set_prompts
176 unset set_prompts