]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/themes/agnoster.zsh-theme
5f4efe813b4aadc49efa42d30fbbe8d8c2c43cf7
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / themes / agnoster.zsh-theme
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
35 CURRENT_BG='NONE'
36
37 case ${SOLARIZED_THEME:-dark} in
38     light) CURRENT_FG='white';;
39     *)     CURRENT_FG='black';;
40 esac
41
42 # Special Powerline characters
43
44 () {
45   local LC_ALL="" LC_CTYPE="en_US.UTF-8"
46   # NOTE: This segment separator character is correct.  In 2012, Powerline changed
47   # the code points they use for their special characters. This is the new code point.
48   # If this is not working for you, you probably have an old version of the
49   # Powerline-patched fonts installed. Download and install the new version.
50   # Do not submit PRs to change this unless you have reviewed the Powerline code point
51   # history and have new information.
52   # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
53   # what font the user is viewing this source code in. Do not replace the
54   # escape sequence with a single literal character.
55   # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
56   SEGMENT_SEPARATOR=$'\ue0b0'
57 }
58
59 # Begin a segment
60 # Takes two arguments, background and foreground. Both can be omitted,
61 # rendering default background/foreground.
62 prompt_segment() {
63   local bg fg
64   [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
65   [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
66   if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
67     echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
68   else
69     echo -n "%{$bg%}%{$fg%} "
70   fi
71   CURRENT_BG=$1
72   [[ -n $3 ]] && echo -n $3
73 }
74
75 # End the prompt, closing any open segments
76 prompt_end() {
77   if [[ -n $CURRENT_BG ]]; then
78     echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
79   else
80     echo -n "%{%k%}"
81   fi
82   echo -n "%{%f%}"
83   CURRENT_BG=''
84 }
85
86 ### Prompt components
87 # Each component will draw itself, and hide itself if no information needs to be shown
88
89 # Context: user@hostname (who am I and where am I)
90 prompt_context() {
91   if [[ "$USERNAME" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
92     prompt_segment black default "%(!.%{%F{yellow}%}.)%n@%m"
93   fi
94 }
95
96 # Git: branch/detached head, dirty status
97 prompt_git() {
98   (( $+commands[git] )) || return
99   if [[ "$(git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]]; then
100     return
101   fi
102   local PL_BRANCH_CHAR
103   () {
104     local LC_ALL="" LC_CTYPE="en_US.UTF-8"
105     PL_BRANCH_CHAR=$'\ue0a0'         # 
106   }
107   local ref dirty mode repo_path
108
109    if [[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]]; then
110     repo_path=$(git rev-parse --git-dir 2>/dev/null)
111     dirty=$(parse_git_dirty)
112     ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
113     if [[ -n $dirty ]]; then
114       prompt_segment yellow black
115     else
116       prompt_segment green $CURRENT_FG
117     fi
118
119     if [[ -e "${repo_path}/BISECT_LOG" ]]; then
120       mode=" <B>"
121     elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
122       mode=" >M<"
123     elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
124       mode=" >R>"
125     fi
126
127     setopt promptsubst
128     autoload -Uz vcs_info
129
130     zstyle ':vcs_info:*' enable git
131     zstyle ':vcs_info:*' get-revision true
132     zstyle ':vcs_info:*' check-for-changes true
133     zstyle ':vcs_info:*' stagedstr '✚'
134     zstyle ':vcs_info:*' unstagedstr '±'
135     zstyle ':vcs_info:*' formats ' %u%c'
136     zstyle ':vcs_info:*' actionformats ' %u%c'
137     vcs_info
138     echo -n "${${ref:gs/%/%%}/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
139   fi
140 }
141
142 prompt_bzr() {
143   (( $+commands[bzr] )) || return
144
145   # Test if bzr repository in directory hierarchy
146   local dir="$PWD"
147   while [[ ! -d "$dir/.bzr" ]]; do
148     [[ "$dir" = "/" ]] && return
149     dir="${dir:h}"
150   done
151
152   local bzr_status status_mod status_all revision
153   if bzr_status=$(bzr status 2>&1); then
154     status_mod=$(echo -n "$bzr_status" | head -n1 | grep "modified" | wc -m)
155     status_all=$(echo -n "$bzr_status" | head -n1 | wc -m)
156     revision=${$(bzr log -r-1 --log-format line | cut -d: -f1):gs/%/%%}
157     if [[ $status_mod -gt 0 ]] ; then
158       prompt_segment yellow black "bzr@$revision ✚"
159     else
160       if [[ $status_all -gt 0 ]] ; then
161         prompt_segment yellow black "bzr@$revision"
162       else
163         prompt_segment green black "bzr@$revision"
164       fi
165     fi
166   fi
167 }
168
169 prompt_hg() {
170   (( $+commands[hg] )) || return
171   local rev st branch
172   if $(hg id >/dev/null 2>&1); then
173     if $(hg prompt >/dev/null 2>&1); then
174       if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
175         # if files are not added
176         prompt_segment red white
177         st='±'
178       elif [[ -n $(hg prompt "{status|modified}") ]]; then
179         # if any modification
180         prompt_segment yellow black
181         st='±'
182       else
183         # if working copy is clean
184         prompt_segment green $CURRENT_FG
185       fi
186       echo -n ${$(hg prompt "☿ {rev}@{branch}"):gs/%/%%} $st
187     else
188       st=""
189       rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
190       branch=$(hg id -b 2>/dev/null)
191       if `hg st | grep -q "^\?"`; then
192         prompt_segment red black
193         st='±'
194       elif `hg st | grep -q "^[MA]"`; then
195         prompt_segment yellow black
196         st='±'
197       else
198         prompt_segment green $CURRENT_FG
199       fi
200       echo -n "☿ ${rev:gs/%/%%}@${branch:gs/%/%%}" $st
201     fi
202   fi
203 }
204
205 # Dir: current working directory
206 prompt_dir() {
207   prompt_segment blue $CURRENT_FG '%~'
208 }
209
210 # Virtualenv: current working virtualenv
211 prompt_virtualenv() {
212   if [[ -n "$VIRTUAL_ENV" && -n "$VIRTUAL_ENV_DISABLE_PROMPT" ]]; then
213     prompt_segment blue black "(${VIRTUAL_ENV:t:gs/%/%%})"
214   fi
215 }
216
217 # Status:
218 # - was there an error
219 # - am I root
220 # - are there background jobs?
221 prompt_status() {
222   local -a symbols
223
224   [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
225   [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
226   [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
227
228   [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
229 }
230
231 #AWS Profile:
232 # - display current AWS_PROFILE name
233 # - displays yellow on red if profile name contains 'production' or
234 #   ends in '-prod'
235 # - displays black on green otherwise
236 prompt_aws() {
237   [[ -z "$AWS_PROFILE" || "$SHOW_AWS_PROMPT" = false ]] && return
238   case "$AWS_PROFILE" in
239     *-prod|*production*) prompt_segment red yellow  "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
240     *) prompt_segment green black "AWS: ${AWS_PROFILE:gs/%/%%}" ;;
241   esac
242 }
243
244 ## Main prompt
245 build_prompt() {
246   RETVAL=$?
247   prompt_status
248   prompt_virtualenv
249   prompt_aws
250   prompt_context
251   prompt_dir
252   prompt_git
253   prompt_bzr
254   prompt_hg
255   prompt_end
256 }
257
258 PROMPT='%{%f%b%k%}$(build_prompt) '