agnoster.zsh-theme
12475 bytes
1# vim:ft=zsh ts=2 sw=2 sts=2
2#
3# agnoster's Theme - https://gist.github.com/3712874
4# A Powerline-inspired theme for ZSH
5#
6# # README
7#
8# In order for this theme to render correctly, you will need a
9# [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts).
10# Make sure you have a recent version: the code points that Powerline
11# uses changed in 2012, and older versions will display incorrectly,
12# in confusing ways.
13#
14# In addition, I recommend the
15# [Solarized theme](https://github.com/altercation/solarized/) and, if you're
16# using it on Mac OS X, [iTerm 2](https://iterm2.com/) over Terminal.app -
17# it has significantly better color fidelity.
18#
19# If using with "light" variant of the Solarized color schema, set
20# SOLARIZED_THEME variable to "light". If you don't specify, we'll assume
21# you're using the "dark" variant.
22#
23# # Goals
24#
25# The aim of this theme is to only show you *relevant* information. Like most
26# prompts, it will only show git information when in a git working directory.
27# However, it goes a step further: everything from the current user and
28# hostname to whether the last call exited with an error to whether background
29# jobs are running in this shell will all be displayed automatically when
30# appropriate.
31
32### Segment drawing
33# A few utility functions to make it easy and re-usable to draw segmented prompts
34
35CURRENT_BG='NONE'
36
37case ${SOLARIZED_THEME:-dark} in
38 light)
39 CURRENT_FG=${CURRENT_FG:-'white'}
40 CURRENT_DEFAULT_FG=${CURRENT_DEFAULT_FG:-'white'}
41 ;;
42 *)
43 CURRENT_FG=${CURRENT_FG:-'black'}
44 CURRENT_DEFAULT_FG=${CURRENT_DEFAULT_FG:-'default'}
45 ;;
46esac
47
48### Theme Configuration Initialization
49#
50# Override these settings in your ~/.zshrc
51
52# Current working directory
53: ${AGNOSTER_DIR_FG:=${CURRENT_FG}}
54: ${AGNOSTER_DIR_BG:=blue}
55
56# user@host
57: ${AGNOSTER_CONTEXT_FG:=${CURRENT_DEFAULT_FG}}
58: ${AGNOSTER_CONTEXT_BG:=black}
59
60# Git related
61: ${AGNOSTER_GIT_CLEAN_FG:=${CURRENT_FG}}
62: ${AGNOSTER_GIT_CLEAN_BG:=green}
63: ${AGNOSTER_GIT_DIRTY_FG:=black}
64: ${AGNOSTER_GIT_DIRTY_BG:=yellow}
65
66# Bazaar related
67: ${AGNOSTER_BZR_CLEAN_FG:=${CURRENT_FG}}
68: ${AGNOSTER_BZR_CLEAN_BG:=green}
69: ${AGNOSTER_BZR_DIRTY_FG:=black}
70: ${AGNOSTER_BZR_DIRTY_BG:=yellow}
71
72# Mercurial related
73: ${AGNOSTER_HG_NEWFILE_FG:=white}
74: ${AGNOSTER_HG_NEWFILE_BG:=red}
75: ${AGNOSTER_HG_CHANGED_FG:=black}
76: ${AGNOSTER_HG_CHANGED_BG:=yellow}
77: ${AGNOSTER_HG_CLEAN_FG:=${CURRENT_FG}}
78: ${AGNOSTER_HG_CLEAN_BG:=green}
79
80# VirtualEnv colors
81: ${AGNOSTER_VENV_FG:=black}
82: ${AGNOSTER_VENV_BG:=blue}
83
84# AWS Profile colors
85: ${AGNOSTER_AWS_PROD_FG:=yellow}
86: ${AGNOSTER_AWS_PROD_BG:=red}
87: ${AGNOSTER_AWS_FG:=black}
88: ${AGNOSTER_AWS_BG:=green}
89
90# Status symbols
91: ${AGNOSTER_STATUS_RETVAL_FG:=red}
92: ${AGNOSTER_STATUS_ROOT_FG:=yellow}
93: ${AGNOSTER_STATUS_JOB_FG:=cyan}
94: ${AGNOSTER_STATUS_FG:=${CURRENT_DEFAULT_FG}}
95: ${AGNOSTER_STATUS_BG:=black}
96
97## Non-Color settings - set to 'true' to enable
98# Show the actual numeric return value rather than a cross symbol.
99: ${AGNOSTER_STATUS_RETVAL_NUMERIC:=false}
100# Show git working dir in the style "/git/root master relative/dir" instead of "/git/root/relative/dir master"
101: ${AGNOSTER_GIT_INLINE:=false}
102# Show the git branch status in the prompt rather than the generic branch symbol
103: ${AGNOSTER_GIT_BRANCH_STATUS:=true}
104
105
106# Special Powerline characters
107
108() {
109 local LC_ALL="" LC_CTYPE="en_US.UTF-8"
110 # NOTE: This segment separator character is correct. In 2012, Powerline changed
111 # the code points they use for their special characters. This is the new code point.
112 # If this is not working for you, you probably have an old version of the
113 # Powerline-patched fonts installed. Download and install the new version.
114 # Do not submit PRs to change this unless you have reviewed the Powerline code point
115 # history and have new information.
116 # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
117 # what font the user is viewing this source code in. Do not replace the
118 # escape sequence with a single literal character.
119 # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
120 SEGMENT_SEPARATOR=$'\ue0b0'
121}
122
123# Begin a segment
124# Takes two arguments, background and foreground. Both can be omitted,
125# rendering default background/foreground.
126prompt_segment() {
127 local bg fg
128 [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
129 [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
130 if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
131 echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
132 else
133 echo -n "%{$bg%}%{$fg%} "
134 fi
135 CURRENT_BG=$1
136 [[ -n $3 ]] && echo -n $3
137}
138
139# End the prompt, closing any open segments
140prompt_end() {
141 if [[ -n $CURRENT_BG ]]; then
142 echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
143 else
144 echo -n "%{%k%}"
145 fi
146 echo -n "%{%f%}"
147 CURRENT_BG=''
148}
149
150git_toplevel() {
151 local repo_root=$(git rev-parse --show-toplevel)
152 if [[ $repo_root = '' ]]; then
153 # We are in a bare repo. Use git dir as root
154 repo_root=$(git rev-parse --git-dir)
155 if [[ $repo_root = '.' ]]; then
156 repo_root=$PWD
157 fi
158 fi
159 echo -n $repo_root
160}
161
162### Prompt components
163# Each component will draw itself, and hide itself if no information needs to be shown
164
165# Context: user@hostname (who am I and where am I)
166prompt_context() {
167 if [[ "$USERNAME" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
168 prompt_segment "$AGNOSTER_CONTEXT_BG" "$AGNOSTER_CONTEXT_FG" "%(!.%{%F{$AGNOSTER_STATUS_ROOT_FG}%}.)%n@%m"
169 fi
170}
171
172prompt_git_relative() {
173 local repo_root=$(git_toplevel)
174 local path_in_repo=$(pwd | sed "s/^$(echo "$repo_root" | sed 's:/:\\/:g;s/\$/\\$/g')//;s:^/::;s:/$::;")
175 if [[ $path_in_repo != '' ]]; then
176 prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$path_in_repo"
177 fi;
178}
179
180# Git: branch/detached head, dirty status
181prompt_git() {
182 (( $+commands[git] )) || return
183 if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]]; then
184 return
185 fi
186 local PL_BRANCH_CHAR
187 () {
188 local LC_ALL="" LC_CTYPE="en_US.UTF-8"
189 PL_BRANCH_CHAR=$'\ue0a0' #
190 }
191 local ref dirty mode repo_path
192
193 if [[ "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]]; then
194 repo_path=$(command git rev-parse --git-dir 2>/dev/null)
195 dirty=$(parse_git_dirty)
196 ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
197 ref="◈ $(command git describe --exact-match --tags HEAD 2> /dev/null)" || \
198 ref="➦ $(command git rev-parse --short HEAD 2> /dev/null)"
199 if [[ -n $dirty ]]; then
200 prompt_segment "$AGNOSTER_GIT_DIRTY_BG" "$AGNOSTER_GIT_DIRTY_FG"
201 else
202 prompt_segment "$AGNOSTER_GIT_CLEAN_BG" "$AGNOSTER_GIT_CLEAN_FG"
203 fi
204
205 if [[ $AGNOSTER_GIT_BRANCH_STATUS == 'true' ]]; then
206 local ahead behind
207 ahead=$(command git log --oneline @{upstream}.. 2>/dev/null)
208 behind=$(command git log --oneline ..@{upstream} 2>/dev/null)
209 if [[ -n "$ahead" ]] && [[ -n "$behind" ]]; then
210 PL_BRANCH_CHAR=$'\u21c5'
211 elif [[ -n "$ahead" ]]; then
212 PL_BRANCH_CHAR=$'\u21b1'
213 elif [[ -n "$behind" ]]; then
214 PL_BRANCH_CHAR=$'\u21b0'
215 fi
216 fi
217
218 if [[ -e "${repo_path}/BISECT_LOG" ]]; then
219 mode=" <B>"
220 elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
221 mode=" >M<"
222 elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
223 mode=" >R>"
224 fi
225
226 setopt promptsubst
227 autoload -Uz vcs_info
228
229 zstyle ':vcs_info:*' enable git
230 zstyle ':vcs_info:*' get-revision true
231 zstyle ':vcs_info:*' check-for-changes true
232 zstyle ':vcs_info:*' stagedstr '✚'
233 zstyle ':vcs_info:*' unstagedstr '±'
234 zstyle ':vcs_info:*' formats ' %u%c'
235 zstyle ':vcs_info:*' actionformats ' %u%c'
236 vcs_info
237 echo -n "${${ref:gs/%/%%}/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
238 [[ $AGNOSTER_GIT_INLINE == 'true' ]] && prompt_git_relative
239 fi
240}
241
242prompt_bzr() {
243 (( $+commands[bzr] )) || return
244
245 # Test if bzr repository in directory hierarchy
246 local dir="$PWD"
247 while [[ ! -d "$dir/.bzr" ]]; do
248 [[ "$dir" = "/" ]] && return
249 dir="${dir:h}"
250 done
251
252 local bzr_status status_mod status_all revision
253 if bzr_status=$(command bzr status 2>&1); then
254 status_mod=$(echo -n "$bzr_status" | head -n1 | grep "modified" | wc -m)
255 status_all=$(echo -n "$bzr_status" | head -n1 | wc -m)
256 revision=${$(command bzr log -r-1 --log-format line | cut -d: -f1):gs/%/%%}
257 if [[ $status_mod -gt 0 ]] ; then
258 prompt_segment "$AGNOSTER_BZR_DIRTY_BG" "$AGNOSTER_BZR_DIRTY_FG" "bzr@$revision ✚"
259 else
260 if [[ $status_all -gt 0 ]] ; then
261 prompt_segment "$AGNOSTER_BZR_DIRTY_BG" "$AGNOSTER_BZR_DIRTY_FG" "bzr@$revision"
262 else
263 prompt_segment "$AGNOSTER_BZR_CLEAN_BG" "$AGNOSTER_BZR_CLEAN_FG" "bzr@$revision"
264 fi
265 fi
266 fi
267}
268
269prompt_hg() {
270 (( $+commands[hg] )) || return
271 local rev st branch
272 if $(command hg id >/dev/null 2>&1); then
273 if $(command hg prompt >/dev/null 2>&1); then
274 if [[ $(command hg prompt "{status|unknown}") = "?" ]]; then
275 # if files are not added
276 prompt_segment "$AGNOSTER_HG_NEWFILE_BG" "$AGNOSTER_HG_NEWFILE_FG"
277 st='±'
278 elif [[ -n $(command hg prompt "{status|modified}") ]]; then
279 # if any modification
280 prompt_segment "$AGNOSTER_HG_CHANGED_BG" "$AGNOSTER_HG_CHANGED_FG"
281 st='±'
282 else
283 # if working copy is clean
284 prompt_segment "$AGNOSTER_HG_CLEAN_BG" "$AGNOSTER_HG_CLEAN_FG"
285 fi
286 echo -n ${$(command hg prompt "☿ {rev}@{branch}"):gs/%/%%} $st
287 else
288 st=""
289 rev=$(command hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
290 branch=$(command hg id -b 2>/dev/null)
291 if command hg st | command grep -q "^\?"; then
292 prompt_segment "$AGNOSTER_HG_NEWFILE_BG" "$AGNOSTER_HG_NEWFILE_FG"
293 st='±'
294 elif command hg st | command grep -q "^[MA]"; then
295 prompt_segment "$AGNOSTER_HG_CHANGED_BG" "$AGNOSTER_HG_CHANGED_FG"
296 st='±'
297 else
298 prompt_segment "$AGNOSTER_HG_CLEAN_BG" "$AGNOSTER_HG_CLEAN_FG"
299 fi
300 echo -n "☿ ${rev:gs/%/%%}@${branch:gs/%/%%}" $st
301 fi
302 fi
303}
304
305# Dir: current working directory
306prompt_dir() {
307 if [[ $AGNOSTER_GIT_INLINE == 'true' ]] && $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
308 # Git repo and inline path enabled, hence only show the git root
309 prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$(git_toplevel | sed "s:^$HOME:~:")"
310 else
311 prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" '%~'
312 fi
313}
314
315# Virtualenv: current working virtualenv
316prompt_virtualenv() {
317 if [ -n "$CONDA_DEFAULT_ENV" ]; then
318 prompt_segment magenta $CURRENT_FG "🐍 $CONDA_DEFAULT_ENV"
319 fi
320 if [[ -n "$VIRTUAL_ENV" && -n "$VIRTUAL_ENV_DISABLE_PROMPT" ]]; then
321 prompt_segment "$AGNOSTER_VENV_BG" "$AGNOSTER_VENV_FG" "(${VIRTUAL_ENV:t:gs/%/%%})"
322 fi
323}
324
325# Status:
326# - was there an error
327# - am I root
328# - are there background jobs?
329prompt_status() {
330 local -a symbols
331
332 if [[ $AGNOSTER_STATUS_RETVAL_NUMERIC == 'true' ]]; then
333 [[ $RETVAL -ne 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_RETVAL_FG}%}$RETVAL"
334 else
335 [[ $RETVAL -ne 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_RETVAL_FG}%}✘"
336 fi
337 [[ $UID -eq 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_ROOT_FG}%}⚡"
338 [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_JOB_FG}%}⚙"
339
340 [[ -n "$symbols" ]] && prompt_segment "$AGNOSTER_STATUS_BG" "$AGNOSTER_STATUS_FG" "$symbols"
341}
342
343#AWS Profile:
344# - display current AWS_PROFILE name
345# - displays yellow on red if profile name contains 'production' or
346# ends in '-prod'
347# - displays black on green otherwise
348prompt_aws() {
349 [[ -z "$AWS_PROFILE" || "$SHOW_AWS_PROMPT" = false ]] && return
350 case "$AWS_PROFILE" in
351 *-prod|*production*) prompt_segment "$AGNOSTER_AWS_PROD_BG" "$AGNOSTER_AWS_PROD_FG" "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
352 *) prompt_segment "$AGNOSTER_AWS_BG" "$AGNOSTER_AWS_FG" "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
353 esac
354}
355
356prompt_terraform() {
357 local terraform_info=$(tf_prompt_info)
358 [[ -z "$terraform_info" ]] && return
359 prompt_segment magenta yellow "TF: $terraform_info"
360}
361
362## Main prompt
363build_prompt() {
364 RETVAL=$?
365 prompt_status
366 prompt_virtualenv
367 prompt_aws
368 prompt_terraform
369 prompt_context
370 prompt_dir
371 prompt_git
372 prompt_bzr
373 prompt_hg
374 prompt_end
375}
376
377PROMPT='%{%f%b%k%}$(build_prompt) '