Skip to content

Instantly share code, notes, and snippets.

@syrm
Last active March 7, 2020 09:35
Show Gist options
  • Save syrm/180dd9f78f90e2e78ade89bd292746cc to your computer and use it in GitHub Desktop.
Save syrm/180dd9f78f90e2e78ade89bd292746cc to your computer and use it in GitHub Desktop.
# vim:ft=zsh ts=2 sw=2 sts=2
#
# agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for ZSH
#
# # README
#
# Use font : https://github.com/gabrielelana/awesome-terminal-fonts
#
# In addition, I recommend the
# [Solarized theme](https://github.com/altercation/solarized/) and, if you're
# using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
# it has significantly better color fidelity.
#
# # 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.
### Theme Configuration Initialization
#
# Override these settings in your ~/.zshrc
# Current working directory
: ${AGNOSTER_DIR_ICON:=""}
: ${AGNOSTER_DIR_FG:=black}
: ${AGNOSTER_DIR_BG:=blue}
# user@host
: ${AGNOSTER_CONTEXT_FG:=default}
: ${AGNOSTER_CONTEXT_BG:=black}
# Git related
: ${AGNOSTER_GIT_CLEAN_FG:=black}
: ${AGNOSTER_GIT_CLEAN_BG:=green}
: ${AGNOSTER_GIT_DIRTY_FG:=black}
: ${AGNOSTER_GIT_DIRTY_BG:=yellow}
# Mercurial related
: ${AGNOSTER_HG_NEWFILE_FG:=white}
: ${AGNOSTER_HG_NEWFILE_BG:=red}
: ${AGNOSTER_HG_CHANGED_FG:=black}
: ${AGNOSTER_HG_CHANGED_BG:=yellow}
: ${AGNOSTER_HG_CLEAN_FG:=black}
: ${AGNOSTER_HG_CLEAN_BG:=green}
# gEnv colors
: ${AGNOSTER_VENV_FG:=black}
: ${AGNOSTER_VENV_BG:=blue}
# Status symbols
: ${AGNOSTER_STATUS_RETVAL_FG:=red}
: ${AGNOSTER_STATUS_ROOT_FG:=yellow}
: ${AGNOSTER_STATUS_JOB_FG:=cyan}
: ${AGNOSTER_STATUS_BG:=black}
## Non-Color settings - set to 'true' to enable
# Show the actual numeric return value rather than a cross symbol.
: ${AGNOSTER_STATUS_RETVAL_NUMERIC:=false}
# Show git working dir in the style "/git/root   master  relative/dir" instead of "/git/root/relative/dir   master"
: ${AGNOSTER_GIT_INLINE:=false}
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
CURRENT_BG='NONE'
if [[ -z "$PRIMARY_FG" ]]; then
PRIMARY_FG=black
fi
# Characters
SEGMENT_SEPARATOR="\ue0b0"
PLUSMINUS="\u00b1"
BRANCH="\ue0a0"
DETACHED="\u27a6"
CROSS="\u2718"
LIGHTNING="\u26a1"
GEAR="\u2699"
# 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
print -n "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}"
else
print -n "%{$bg%}%{$fg%}"
fi
CURRENT_BG=$1
[[ -n $3 ]] && print -n $3
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
else
print -n "%{%k%}"
fi
print -n "%{%f%}"
CURRENT_BG=''
}
git_toplevel() {
local repo_root=$(git rev-parse --show-toplevel)
if [[ $repo_root = '' ]]; then
# We are in a bare repo. Use git dir as root
repo_root=$(git rev-parse --git-dir)
if [[ $repo_root = '.' ]]; then
repo_root=$(pwd)
fi
fi
echo -n $repo_root
}
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown
# Context: user@hostname (who am I and where am I)
prompt_context() {
local user=`whoami`
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
prompt_segment "$AGNOSTER_CONTEXT_BG" "$AGNOSTER_CONTEXT_FG" " %(!.%{%F{yellow}%}.)$user@%m "
fi
}
prompt_git_relative() {
local repo_root=$(git_toplevel)
local path_in_repo=$(pwd | sed "s/^$(echo "$repo_root" | sed 's:/:\\/:g;s/\$/\\$/g')//;s:^/::;s:/$::;")
if [[ $path_in_repo != '' ]]; then
prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$path_in_repo"
fi;
}
# Git: branch/detached head, dirty status
prompt_git() {
local color fg ref
is_dirty() {
test -n "$(git status --porcelain --ignore-submodules)"
}
ref="$vcs_info_msg_0_"
if [[ -n "$ref" ]]; then
if is_dirty; then
color=$AGNOSTER_GIT_DIRTY_BG
fg=$AGNOSTER_GIT_DIRTY_FG
ref="${ref} $PLUSMINUS"
else
color=$AGNOSTER_GIT_CLEAN_BG
fg=$AGNOSTER_GIT_CLEAN_FG
ref="${ref} "
fi
if [[ "${ref/.../}" == "$ref" ]]; then
ref="$BRANCH $ref"
else
ref="$DETACHED ${ref/.../}"
fi
prompt_segment $color $fg
print -n " $ref"
[[ $AGNOSTER_GIT_INLINE == 'true' ]] && prompt_git_relative
fi
}
# Dir: current working directory
# Patched by Vincent Tommasi (https://github.com/VinceTommasi)
prompt_dir() {
if [ $(git rev-parse --is-inside-work-tree 2> /dev/null) ]; then
DIR=$(git rev-parse --show-prefix)
if [ -z "$DIR" ]; then
prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$AGNOSTER_DIR_ICON"" "$(basename "`git rev-parse --show-toplevel`")" "
else
prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$AGNOSTER_DIR_ICON"" "$(basename "`git rev-parse --show-toplevel`")"/"${DIR%/}" "
fi
else
prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$AGNOSTER_DIR_ICON"" %1~ "
fi
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
if [[ $AGNOSTER_STATUS_RETVAL_NUMERIC == 'true' ]]; then
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_RETVAL_FG}%}$RETVAL"
else
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_RETVAL_FG}%}$CROSS"
fi
[[ $UID -eq 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_ROOT_FG}%}$LIGHTNING"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_JOB_FG}%}$GEAR"
[[ -n "$symbols" ]] && prompt_segment "$AGNOSTER_STATUS_BG" default " $symbols "
}
# Display current virtual environment
prompt_virtualenv() {
if [[ -n $VIRTUAL_ENV ]]; then
prompt_segment "$AGNOSTER_VENV_BG" "$AGNOSTER_VENV_FG"
print -Pn " $(basename $VIRTUAL_ENV) "
fi
}
## Main prompt
prompt_agnoster_main() {
RETVAL=$?
CURRENT_BG='NONE'
prompt_status
prompt_context
prompt_virtualenv
prompt_dir
prompt_git
prompt_end
}
prompt_agnoster_precmd() {
vcs_info
PROMPT='%{%f%b%k%}$(prompt_agnoster_main) '
}
prompt_agnoster_setup() {
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
prompt_opts=(cr subst percent)
add-zsh-hook precmd prompt_agnoster_precmd
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' check-for-changes false
zstyle ':vcs_info:git*' formats '%b'
zstyle ':vcs_info:git*' actionformats '%b (%a)'
}
prompt_agnoster_setup "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment