agnoster.zsh-theme 7.69 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

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)"
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  (( $+commands[bzr] )) || return

  # Test if bzr repository in directory hierarchy
  local dir="$PWD"
  while [[ ! -d "$dir/.bzr" ]]; do
    [[ "$dir" = "/" ]] && return
    dir="${dir:h}"
  done

  local bzr_status status_mod status_all revision
  if bzr_status=$(bzr status 2>&1); then
    status_mod=$(echo -n "$bzr_status" | head -n1 | grep "modified" | wc -m)
    status_all=$(echo -n "$bzr_status" | head -n1 | wc -m)
    revision=$(bzr log -r-1 --log-format line | cut -d: -f1)
    if [[ $status_mod -gt 0 ]] ; then
      prompt_segment yellow black "bzr@$revision ✚"
    else
      if [[ $status_all -gt 0 ]] ; then
        prompt_segment yellow black "bzr@$revision"
      else
        prompt_segment green black "bzr@$revision"
      fi
165
    fi
166
  fi
167
168
}

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

205
206
# Dir: current working directory
prompt_dir() {
207
  prompt_segment blue $CURRENT_FG '%~'
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
208
209
}

210
211
212
# Virtualenv: current working virtualenv
prompt_virtualenv() {
  local virtualenv_path="$VIRTUAL_ENV"
213
  if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
214
215
216
217
    prompt_segment blue black "(`basename $virtualenv_path`)"
  fi
}

218
219
220
221
222
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
223
  local -a symbols
224

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

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

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

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