agnoster.zsh-theme 7.58 KB
Newer Older
1
2
# vim:ft=zsh ts=2 sw=2 sts=2
#
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
3
# agnoster's Theme - https://gist.github.com/3712874
4
5
6
7
8
# A Powerline-inspired theme for ZSH
#
# # README
#
# In order for this theme to render correctly, you will need a
9
# [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts).
10
11
12
# Make sure you have a recent version: the code points that Powerline
# uses changed in 2012, and older versions will display incorrectly,
# in confusing ways.
13
14
15
#
# In addition, I recommend the
# [Solarized theme](https://github.com/altercation/solarized/) and, if you're
Janosch Schwalm's avatar
Janosch Schwalm committed
16
# using it on Mac OS X, [iTerm 2](https://iterm2.com/) over Terminal.app -
17
18
# it has significantly better color fidelity.
#
19
20
21
22
# If using with "light" variant of the Solarized color schema, set
# SOLARIZED_THEME variable to "light". If you don't specify, we'll assume
# you're using the "dark" variant.
#
23
24
25
26
27
28
29
30
31
# # Goals
#
# The aim of this theme is to only show you *relevant* information. Like most
# prompts, it will only show git information when in a git working directory.
# However, it goes a step further: everything from the current user and
# hostname to whether the last call exited with an error to whether background
# jobs are running in this shell will all be displayed automatically when
# appropriate.

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
32
33
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
34

35
CURRENT_BG='NONE'
36

37
38
39
40
41
case ${SOLARIZED_THEME:-dark} in
    light) CURRENT_FG='white';;
    *)     CURRENT_FG='black';;
esac

42
43
# Special Powerline characters

44
45
46
47
() {
  local LC_ALL="" LC_CTYPE="en_US.UTF-8"
  # NOTE: This segment separator character is correct.  In 2012, Powerline changed
  # 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
50
51
52
53
54
  # Powerline-patched fonts installed. Download and install the new version.
  # Do not submit PRs to change this unless you have reviewed the Powerline code point
  # history and have new information.
  # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
  # what font the user is viewing this source code in. Do not replace the
  # escape sequence with a single literal character.
55
56
  # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
  SEGMENT_SEPARATOR=$'\ue0b0'
57
}
58
59
60
61
62
63
64
65
66

# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
  local bg fg
  [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
Marc Cornellà's avatar
Marc Cornellà committed
67
    echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
68
  else
Marc Cornellà's avatar
Marc Cornellà committed
69
    echo -n "%{$bg%}%{$fg%} "
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
70
  fi
71
  CURRENT_BG=$1
Marc Cornellà's avatar
Marc Cornellà committed
72
  [[ -n $3 ]] && echo -n $3
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
73
}
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
74

75
76
# End the prompt, closing any open segments
prompt_end() {
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
77
  if [[ -n $CURRENT_BG ]]; then
Marc Cornellà's avatar
Marc Cornellà committed
78
    echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
79
  else
Marc Cornellà's avatar
Marc Cornellà committed
80
    echo -n "%{%k%}"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
81
  fi
Marc Cornellà's avatar
Marc Cornellà committed
82
83
  echo -n "%{%f%}"
  CURRENT_BG=''
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
84
85
}

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
86
87
88
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown

89
90
# Context: user@hostname (who am I and where am I)
prompt_context() {
91
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
92
    prompt_segment black default "%(!.%{%F{yellow}%}.)%n@%m"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
93
94
95
  fi
}

96
97
# Git: branch/detached head, dirty status
prompt_git() {
98
  (( $+commands[git] )) || return
99
100
101
  if [[ "$(git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]]; then
    return
  fi
102
103
104
105
106
  local PL_BRANCH_CHAR
  () {
    local LC_ALL="" LC_CTYPE="en_US.UTF-8"
    PL_BRANCH_CHAR=$'\ue0a0'         # 
  }
107
108
  local ref dirty mode repo_path

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
109
  if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); 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)"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
113
    if [[ -n $dirty ]]; then
114
      prompt_segment yellow black
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
115
    else
116
      prompt_segment green $CURRENT_FG
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
117
    fi
118

119
120
121
122
123
124
125
126
    if [[ -e "${repo_path}/BISECT_LOG" ]]; then
      mode=" <B>"
    elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
      mode=" >M<"
    elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
      mode=" >R>"
    fi

127
128
129
130
131
132
133
    setopt promptsubst
    autoload -Uz vcs_info

    zstyle ':vcs_info:*' enable git
    zstyle ':vcs_info:*' get-revision true
    zstyle ':vcs_info:*' check-for-changes true
    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
Marc Cornellà's avatar
Marc Cornellà committed
138
    echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
139
140
141
  fi
}

142
prompt_bzr() {
143
    (( $+commands[bzr] )) || return
144
145
146
147
148
149
    if (bzr status >/dev/null 2>&1); then
        status_mod=`bzr status | head -n1 | grep "modified" | wc -m`
        status_all=`bzr status | head -n1 | wc -m`
        revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'`
        if [[ $status_mod -gt 0 ]] ; then
            prompt_segment yellow black
Marc Cornellà's avatar
Marc Cornellà committed
150
            echo -n "bzr@"$revision "✚ "
151
152
153
        else
            if [[ $status_all -gt 0 ]] ; then
                prompt_segment yellow black
Marc Cornellà's avatar
Marc Cornellà committed
154
                echo -n "bzr@"$revision
155
156
            else
                prompt_segment green black
Marc Cornellà's avatar
Marc Cornellà committed
157
                echo -n "bzr@"$revision
158
159
160
161
162
            fi
        fi
    fi
}

163
prompt_hg() {
164
  (( $+commands[hg] )) || return
165
  local rev st branch
Eddie Monge's avatar
Eddie Monge committed
166
167
168
169
170
  if $(hg id >/dev/null 2>&1); then
    if $(hg prompt >/dev/null 2>&1); then
      if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
        # if files are not added
        prompt_segment red white
Marc Cornellà's avatar
Marc Cornellà committed
171
        st='±'
Eddie Monge's avatar
Eddie Monge committed
172
173
174
      elif [[ -n $(hg prompt "{status|modified}") ]]; then
        # if any modification
        prompt_segment yellow black
Marc Cornellà's avatar
Marc Cornellà committed
175
        st='±'
Eddie Monge's avatar
Eddie Monge committed
176
177
      else
        # if working copy is clean
178
        prompt_segment green $CURRENT_FG
Eddie Monge's avatar
Eddie Monge committed
179
      fi
Marc Cornellà's avatar
Marc Cornellà committed
180
      echo -n $(hg prompt "☿ {rev}@{branch}") $st
Eddie Monge's avatar
Eddie Monge committed
181
182
183
184
    else
      st=""
      rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
      branch=$(hg id -b 2>/dev/null)
185
      if `hg st | grep -q "^\?"`; then
Eddie Monge's avatar
Eddie Monge committed
186
        prompt_segment red black
Marc Cornellà's avatar
Marc Cornellà committed
187
        st='±'
Mark Mendoza's avatar
Mark Mendoza committed
188
      elif `hg st | grep -q "^[MA]"`; then
Eddie Monge's avatar
Eddie Monge committed
189
        prompt_segment yellow black
Marc Cornellà's avatar
Marc Cornellà committed
190
        st='±'
Eddie Monge's avatar
Eddie Monge committed
191
      else
192
        prompt_segment green $CURRENT_FG
Eddie Monge's avatar
Eddie Monge committed
193
      fi
Marc Cornellà's avatar
Marc Cornellà committed
194
      echo -n "☿ $rev@$branch" $st
Eddie Monge's avatar
Eddie Monge committed
195
196
    fi
  fi
197
198
}

199
200
# Dir: current working directory
prompt_dir() {
201
  prompt_segment blue $CURRENT_FG '%~'
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
202
203
}

204
205
206
# Virtualenv: current working virtualenv
prompt_virtualenv() {
  local virtualenv_path="$VIRTUAL_ENV"
207
  if [[ -n $virtualenv_path && -z $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
208
209
210
211
    prompt_segment blue black "(`basename $virtualenv_path`)"
  fi
}

212
213
214
215
216
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
217
  local -a symbols
218

219
  [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
220
  [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
Marc Cornellà's avatar
Marc Cornellà committed
221
  [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
222
223

  [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
224
225
}

226
227
228
229
230
231
232
233
234
235
236
237
238
#AWS Profile:
# - display current AWS_PROFILE name
# - displays yellow on red if profile name contains 'production' or
#   ends in '-prod'
# - displays black on green otherwise
prompt_aws() {
  [[ -z "$AWS_PROFILE" ]] && return
  case "$AWS_PROFILE" in
    *-prod|*production*) prompt_segment red yellow  "AWS: $AWS_PROFILE" ;;
    *) prompt_segment green black "AWS: $AWS_PROFILE" ;;
  esac
}

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
239
## Main prompt
240
build_prompt() {
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
241
242
  RETVAL=$?
  prompt_status
243
  prompt_virtualenv
244
  prompt_aws
245
  prompt_context
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
246
247
  prompt_dir
  prompt_git
248
  prompt_bzr
249
  prompt_hg
250
  prompt_end
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
251
}
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
252

Marc Cornellà's avatar
Marc Cornellà committed
253
PROMPT='%{%f%b%k%}$(build_prompt) '