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
157

            else
                prompt_segment green black
Marc Cornellà's avatar
Marc Cornellà committed
158
                echo -n "bzr@"$revision
159
160
161
162
163
            fi
        fi
    fi
}

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

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

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

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

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

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

227
228
229
230
231
232
233
234
235
236
237
238
239
#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
240
## Main prompt
241
build_prompt() {
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
242
243
  RETVAL=$?
  prompt_status
244
  prompt_virtualenv
245
  prompt_aws
246
  prompt_context
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
247
248
  prompt_dir
  prompt_git
249
  prompt_bzr
250
  prompt_hg
251
  prompt_end
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
252
}
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
253

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