Skip to content

Instantly share code, notes, and snippets.

@mudasobwa
Last active February 17, 2016 11:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mudasobwa/5308070 to your computer and use it in GitHub Desktop.
Save mudasobwa/5308070 to your computer and use it in GitHub Desktop.
YA Theme for YADR
#
# Mudasobwa yadr theme.
#
# Based on Agnoster theme from https://gist.github.com/3712874
#
# NB! In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://gist.github.com/1595572).
#
# Authors:
# agnoster (original)
# digitalformula (modified for compatibility with Prezto - https://github.com/sorin-ionescu/prezto)
# mudasobwa (added right prompt for ruby environment, code cleanup etc)
#
# Original agnoster theme was written for Oh My Zsh - https://github.com/robbyrussell/oh-my-zsh
# The modified version by digitalformula required git-omz.zsh and get-short-path.zsh
# The code of latter is included here for convenience
####################################################################
# Content of get-short-path.zsh copypasted here for convenience
####################################################################
# function to get the short version of the current path
# requires PWD_LENGTH variable to be set defaults to 15, if not set
function get_short_path() {
HOME_TILDE=~
LONG_PATH=`pwd`
if [[ $LONG_PATH == $HOME_TILDE ]]; then LONG_PATH="~"; fi
# check to see if the prompt path length has been specified
if [ ! -n "$PWD_LENGTH" ]; then export PWD_LENGTH=15; fi
if [ ${#LONG_PATH} -gt $PWD_LENGTH ]; then
echo "...${LONG_PATH: -$PWD_LENGTH}"
else
echo $LONG_PATH
fi
}
####################################################################
# Content of git-omz.zsh copypasted here for convenience
####################################################################
# get the name of the git branch we are on
function git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
}
# Checks if working tree is dirty
parse_git_dirty() {
local SUBMODULE_SYNTAX=''
if [[ $POST_1_7_2_GIT -gt 0 ]]; then
SUBMODULE_SYNTAX="--ignore-submodules=dirty"
fi
if [[ -n $(git status -s ${SUBMODULE_SYNTAX} 2> /dev/null) ]]; then
echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
else
echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
fi
}
# Checks if there are commits ahead from remote
function git_prompt_ahead() {
if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
fi
}
# Formats prompt string for current git commit short SHA
function git_prompt_short_sha() {
SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
}
# Formats prompt string for current git commit long SHA
function git_prompt_long_sha() {
SHA=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
}
# Get the status of the working tree
git_prompt_status() {
INDEX=$(git status --porcelain 2> /dev/null)
STATUS=""
if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
fi
if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
fi
if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
fi
if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
fi
if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
fi
if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
fi
echo $STATUS
}
# compares the provided version of git to the version installed and on path
# prints 1 if input version <= installed version, -1 otherwise
function git_compare_version() {
local INPUT_GIT_VERSION=$1;
local INSTALLED_GIT_VERSION
INPUT_GIT_VERSION=(${(s/./)INPUT_GIT_VERSION});
INSTALLED_GIT_VERSION=($(git --version));
INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]});
for i in {1..3}; do
if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
echo -1
return 0
fi
done
echo 1
}
#this is unlikely to change so make it all statically assigned
POST_1_7_2_GIT=$(git_compare_version "1.7.2")
#clean up the namespace slightly by removing the checker function
unset -f git_compare_version
####################################################################
# The theme itself
####################################################################
# Load dependencies.
pmodload 'helper'
function prompt_mudasobwa_precmd {
setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS
# Get git info
if (( $+functions[git-info] )); then
git-info
fi
# Get ruby information
if (( $+functions[ruby-info] )); then
ruby-info
fi
}
# 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
echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
else
echo -n "%{$bg%}%{$fg%} "
fi
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
# Begin an right segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_rsegment() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $1 != $CURRENT_BG ]]; then
echo -n " %{%F{$1}%}$SEGMENT_RSEPARATOR%{$bg%}%{$fg%} "
else
echo -n "%{$bg%}%{$fg%} "
fi
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
else
echo -n "%{%k%}"
fi
echo -n "%{%f%}"
CURRENT_BG=''
}
# End the rprompt, closing any open segments
prompt_rend() {
if [[ -n $CURRENT_BG ]]; then
echo -n " %{%k%F{$CURRENT_BG}%}"
else
echo -n "%{%k%}"
fi
echo -n "%{%f%}"
CURRENT_BG=''
}
### 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_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$user@%m"
fi
}
# Git: branch/detached head, dirty status
prompt_git() {
local ref dirty
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
ZSH_THEME_GIT_PROMPT_DIRTY='±'
dirty=$(parse_git_dirty)
ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
if [[ -n $dirty ]]; then
prompt_segment yellow black
else
prompt_segment green black
fi
echo -n "${ref/refs\/heads\//⭠ }$dirty"
fi
}
# Dir: current working directory
prompt_dir_old() {
prompt_segment blue black '%~'
}
prompt_dir() {
prompt_segment blue black `get_short_path`
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘ [$RETVAL]"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
[[ -n "$symbols" ]] && prompt_segment black black "$symbols"
}
### current time
### function added by digitalformula
prompt_time() {
prompt_segment white black "⌚ `date +%H:%M:%S`"
}
### current hostname
### function added by digitalformula
prompt_hostname() {
hostname=`hostname`
prompt_segment green black "$hostname"
}
### current hostname for right prompt
### added by mudasobwa
prompt_rhostname() {
hostname=`hostname`
prompt_rsegment magenta black "$hostname"
}
### current ruby environment
### added by mudasobwa
prompt_rbenv() {
# rbver=$(echo ${ruby_info[version]} | tr '@' '\n')
rbver=$(echo ${ruby_info[version]} | cut -d @ -f 1)
rbgem=$(echo ${ruby_info[version]} | cut -d @ -f 2)
prompt_rsegment green black "$rbgem ⚓"
prompt_rsegment blue black "$rbver"
}
## Main prompt (Old version, includes currect directory)
build_prompt_old() {
RETVAL=$?
prompt_status
prompt_context
prompt_dir
prompt_git
prompt_end
}
build_prompt() {
RETVAL=$?
prompt_status
prompt_time
prompt_dir
prompt_git
prompt_end
}
build_rprompt() {
RETVAL=$?
prompt_rbenv
prompt_rhostname
prompt_rend
}
function prompt_mudasobwa_setup {
setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS
prompt_opts=(cr percent subst)
# Load required functions.
autoload -Uz add-zsh-hook
# Add hook for calling git-info before each command.
add-zsh-hook precmd prompt_mudasobwa_precmd
zstyle ':prezto:module:editor' completing '%B%F{red}...%f%b'
zstyle ':prezto:module:editor:keymap:primary' overwrite ' %F{red}♺%f'
zstyle ':prezto:module:editor:keymap' primary ' %B%F{red}❯%F{yellow}❯%F{green}❯%f%b'
zstyle ':prezto:module:editor:keymap' alternate ' %B%F{green}❮%F{yellow}❮%F{red}❮%f%b'
zstyle ':prezto:module:git' action ':%%B%F{yellow}%s%f%%b'
zstyle ':prezto:module:git' added ' %%B%F{green}✚%f%%b'
zstyle ':prezto:module:git' ahead ' %%B%F{yellow}⬆%f%%b'
zstyle ':prezto:module:git' behind ' %%B%F{yellow}⬇%f%%b'
zstyle ':prezto:module:git' branch ':%F{green}%b%f'
zstyle ':prezto:module:git' commit ':%F{green}%.7c%f'
zstyle ':prezto:module:git' deleted ' %%B%F{red}✖%f%%b'
zstyle ':prezto:module:git' modified ' %%B%F{blue}✱%f%%b'
zstyle ':prezto:module:git' position ':%F{green}%p%f'
zstyle ':prezto:module:git' renamed ' %%B%F{magenta}➜%f%%b'
zstyle ':prezto:module:git' stashed ' %%B%F{cyan}✭%f%%b'
zstyle ':prezto:module:git' unmerged ' %%B%F{yellow}═%f%%b'
zstyle ':prezto:module:git' untracked ' %%B%F{white}◼%f%%b'
zstyle ':prezto:module:git' info \
'prompt' ' %F{blue}git%f$(coalesce "%b" "%p" "%c")%s' \
'rprompt' '%A%B%S%a%d%m%r%U%u'
zstyle ':prezto:module:ruby:info:version' format '%v'
# Original theme's comments left intact:
# vim:ft=zsh ts=2 sw=2 sts=2
#
# agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for ZSH
#
# # README
#
# In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://gist.github.com/1595572).
#
# 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.
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
CURRENT_BG='NONE'
SEGMENT_SEPARATOR='⮀'
SEGMENT_RSEPARATOR='⮂'
PROMPT='%{%f%b%k%}$(build_prompt) '
RPROMPT=' %{%f%b%k%}$(build_rprompt)'
}
prompt_mudasobwa_setup "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment