boban.zsh-theme
5085 bytes
1# vim:ft=zsh ts=2 sw=2 sts=2
2#
3# Boban
4# A Powerline-inspired theme for ZSH
5
6CURRENT_BG='NONE'
7DEFAULT_USER=${DEFAULT_USER:-default}
8
9# Special Powerline characters
10() {
11 local LC_ALL="" LC_CTYPE="en_US.UTF-8"
12 # NOTE: This segment separator character is correct. In 2012, Powerline changed
13 # the code points they use for their special characters. This is the new code point.
14 # If this is not working for you, you probably have an old version of the
15 # Powerline-patched fonts installed. Download and install the new version.
16 # Do not submit PRs to change this unless you have reviewed the Powerline code point
17 # history and have new information.
18 # This is defined using a Unicode escape sequence so it is unambiguously readable, regardless of
19 # what font the user is viewing this source code in. Do not replace the
20 # escape sequence with a single literal character.
21 # Do not change this! Do not make it '\u2b80'; that is the old, wrong code point.
22 SEGMENT_SEPARATOR=$'\ue0b0'
23 RIGHT_SEPARATOR=$'\ue0b3'
24}
25
26# Begin a segment
27# Takes two arguments, background and foreground. Both can be omitted,
28# rendering default background/foreground.
29prompt_segment() {
30 local bg fg
31 [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
32 [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
33 if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
34 echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
35 else
36 echo -n "%{$bg%}%{$fg%} "
37 fi
38 CURRENT_BG=$1
39 [[ -n $3 ]] && echo -n $3
40}
41
42# End the prompt, closing any open segments
43prompt_end() {
44 if [[ -n $CURRENT_BG ]]; then
45 echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
46 else
47 echo -n "%{%k%}"
48 fi
49 echo -n "%{%f%}"
50 CURRENT_BG=''
51}
52
53# Begin the right prompt
54prompt_endr() {
55 echo -n " %{%k%F{grey}%}$RIGHT_SEPARATOR"
56 echo -n "%{%f%}"
57 CURRENT_BG=''
58}
59
60### Prompt components
61# Each component will draw itself, and hide itself if no information needs to be shown
62
63# Context: user@hostname (who am I and where am I)
64prompt_context() {
65 if [[ "$USER" != "$DEFAULT_USER" || "$SSH_CLIENT" ]]; then
66 prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
67 fi
68}
69
70# Git: branch/detached head, dirty status
71prompt_git() {
72
73 local PL_BRANCH_CHAR
74 () {
75 local LC_ALL="" LC_CTYPE="en_US.UTF-8"
76 PL_BRANCH_CHAR=$'\ue0a0' #
77 }
78 local ref dirty mode repo_path
79 repo_path=$(git rev-parse --git-dir 2>/dev/null)
80
81 if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
82 dirty=$(parse_git_dirty)
83 ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
84 if [[ -n $dirty ]]; then
85 prompt_segment yellow black
86 else
87 prompt_segment green black
88 fi
89
90 if [[ -e "${repo_path}/BISECT_LOG" ]]; then
91 mode=" <B>"
92 elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
93 mode=" >M<"
94 elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
95 mode=" >R>"
96 fi
97
98 setopt promptsubst
99 autoload -Uz vcs_info
100
101 zstyle ':vcs_info:*' enable git
102 zstyle ':vcs_info:*' get-revision true
103 zstyle ':vcs_info:*' check-for-changes true
104 zstyle ':vcs_info:*' stagedstr '+'
105 zstyle ':vcs_info:*' unstagedstr '●'
106 zstyle ':vcs_info:*' formats ' %u%c'
107 zstyle ':vcs_info:*' actionformats ' %u%c'
108 vcs_info
109 echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
110 fi
111}
112
113# Dir: current working directory
114prompt_dir() {
115 if [[ -w "$PWD" ]]; then
116 prompt_segment blue black '%~'
117 else
118 prompt_segment red black '%~'
119 fi
120}
121
122# Virtualenv: current working virtualenv
123prompt_virtualenv() {
124 local virtualenv_path="$VIRTUAL_ENV"
125 if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
126 prompt_segment blue black ""
127 fi
128}
129
130# AWS profile
131prompt_aws() {
132 local aws_profile="$AWS_PROFILE"
133 if [[ -n $aws_profile ]]; then
134 prompt_segment black yellow " $aws_profile"
135 fi
136}
137
138# Terraform: current terraform workspace
139prompt_tf() {
140 # dont show 'default' workspace in home dir
141 [[ "$PWD" == ~ ]] && return
142 # check if in terraform dir
143 if [ -d .terraform ]; then
144 workspace=$(terraform workspace show 2> /dev/null) || return
145 prompt_segment black magenta " ${workspace}"
146 fi
147}
148
149# Kubectl: current active kubeconfig file
150prompt_kubectl() {
151 if [[ -n $KUBECONFIG ]]; then
152 prompt_segment magenta black " $(basename $KUBECONFIG)"
153 fi
154}
155
156# Status:
157# - was there an error?
158# - am I root?
159# - are there background jobs?
160prompt_status() {
161 local symbols
162 symbols=()
163 [ $RETVAL -ne 0 ] && symbols+="%{%F{red}%}$RETVAL"
164 [ $UID -eq 0 ] && symbols+="%{%F{yellow}%}⚡"
165 [ $(jobs -l | wc -l) -gt 0 ] && symbols+="%{%F{cyan}%}⚙"
166
167 [ -n "$symbols" ] && prompt_segment background default "$symbols"
168}
169
170prompt_time() {
171 prompt_segment background default $(date "+%H:%M:%S")
172}
173
174## Main prompt
175build_prompt() {
176 prompt_context
177 prompt_dir
178 prompt_virtualenv
179 prompt_aws
180 prompt_kubectl
181 prompt_tf
182 prompt_git
183 prompt_end
184}
185
186## Right prompt
187build_rprompt() {
188 RETVAL=$?
189 prompt_status
190 prompt_endr
191 # prompt_time
192}
193
194PROMPT='%{%f%b%k%}$(build_prompt) '
195RPROMPT='%{%f%b%k%}$(build_rprompt) '