From 74bc2f38bc2790617342dffac06e4d2d0192a950 Mon Sep 17 00:00:00 2001 From: Kilobyte22 Date: Tue, 23 Dec 2014 18:12:30 +0100 Subject: [PATCH] Changed Agnoster theme for more modularity The agnoster theme now supports: * Setting color for all kinds of elements, by setting an appropiate config variable to the color name in your zshrc Example: AGNOSTER_GIT_DIRTY_BG=red # Set the git prompt background color to red for dirty repos * Allowing to separate the git root and the relative path in your prompt by setting AGNOSTER_GIT_INLINE to 'true' You will then have a segment for the git root (ie. ~/.oh-my-zsh) followed by the existing git branch segment (ie. master) and finally the relative directory path (ie. themes) * By setting AGNOSTER_STATUS_RETVAL_NUMERIC to 'true', The return value in the status prompt will become a number instead of a cross. --- themes/agnoster.zsh-theme | 92 ++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/themes/agnoster.zsh-theme b/themes/agnoster.zsh-theme index 8c7be6e01..543836a95 100644 --- a/themes/agnoster.zsh-theme +++ b/themes/agnoster.zsh-theme @@ -22,6 +22,50 @@ # 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_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:=orange} + +# Mercurial related +: ${AGNOSTER_HG_NEWFILE_FG:=white} +: ${AGNOSTER_HG_NEWFILE_BG:=red} +: ${AGNOSTER_HG_CHANGED_FG:=black} +: ${AGNOSTER_HG_CHANGED_BG:=red} +: ${AGNOSTER_HG_CLEAN_FG:=black} +: ${AGNOSTER_HG_CLEAN_BG:=green} + +# VirtualEnv colors +: ${AGNOSTER_VENV_FG:=blue} +: ${AGNOSTER_VENV_BG:=black} + +# 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 @@ -61,10 +105,18 @@ prompt_end() { # Context: user@hostname (who am I and where am I) prompt_context() { if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then - prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m" + prompt_segment "$AGNOSTER_CONTEXT_BG" "$AGNOSTER_CONTEXT_FG" "%(!.%{%F{yellow}%}.)$USER@%m" fi } +prompt_git_relative() { + local repo_root=$(git rev-parse --show-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 ref dirty mode repo_path @@ -74,9 +126,9 @@ prompt_git() { 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 + prompt_segment "$AGNOSTER_GIT_DIRTY_BG" "$AGNOSTER_GIT_DIRTY_FG" else - prompt_segment green black + prompt_segment "$AGNOSTER_GIT_CLEAN_BG" "$AGNOSTER_GIT_CLEAN_FG" fi if [[ -e "${repo_path}/BISECT_LOG" ]]; then @@ -99,6 +151,7 @@ prompt_git() { zstyle ':vcs_info:*' actionformats ' %u%c' vcs_info echo -n "${ref/refs\/heads\// }${vcs_info_msg_0_%% }${mode}" + [[ $AGNOSTER_GIT_INLINE == 'true' ]] && prompt_git_relative fi } @@ -108,15 +161,15 @@ prompt_hg() { if $(hg prompt >/dev/null 2>&1); then if [[ $(hg prompt "{status|unknown}") = "?" ]]; then # if files are not added - prompt_segment red white + prompt_segment "$AGNOSTER_HG_NEWFILE_BG" "$AGNOSTER_HG_NEWFILE_FG" st='±' elif [[ -n $(hg prompt "{status|modified}") ]]; then # if any modification - prompt_segment yellow black + prompt_segment "$AGNOSTER_HG_CHANGED_BG" "$AGNOSTER_HG_CHANGED_FG" st='±' else # if working copy is clean - prompt_segment green black + prompt_segment "$AGNOSTER_HG_CLEAN_BG" "$AGNOSTER_HG_CLEAN_FG" fi echo -n $(hg prompt "☿ {rev}@{branch}") $st else @@ -124,13 +177,13 @@ prompt_hg() { rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g') branch=$(hg id -b 2>/dev/null) if `hg st | grep -q "^\?"`; then - prompt_segment red black + prompt_segment "$AGNOSTER_HG_NEWFILE_BG" "$AGNOSTER_HG_NEWFILE_FG" st='±' elif `hg st | grep -q "^(M|A)"`; then - prompt_segment yellow black + prompt_segment "$AGNOSTER_HG_CHANGED_BG" "$AGNOSTER_HG_CHANGED_FG" st='±' else - prompt_segment green black + prompt_segment "$AGNOSTER_HG_CLEAN_BG" "$AGNOSTER_HG_CLEAN_FG" fi echo -n "☿ $rev@$branch" $st fi @@ -139,14 +192,19 @@ prompt_hg() { # Dir: current working directory prompt_dir() { - prompt_segment blue black '%~' + if [[ $AGNOSTER_GIT_INLINE == 'true' ]] && $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then + # Git repo and inline path enabled, hence only show the git root + prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" "$(git rev-parse --show-toplevel | sed "s:^$HOME:~:")" + else + prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" '%~' + fi } # Virtualenv: current working virtualenv prompt_virtualenv() { local virtualenv_path="$VIRTUAL_ENV" if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then - prompt_segment blue black "(`basename $virtualenv_path`)" + prompt_segment "$AGNOSTER_VENV_BG" "$AGNOSTER_VENV_FG" "(`basename $virtualenv_path`)" fi } @@ -157,11 +215,15 @@ prompt_virtualenv() { prompt_status() { local symbols symbols=() - [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘" - [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡" - [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙" + 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}%}✘" + fi + [[ $UID -eq 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_ROOT_FG}%}⚡" + [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{$AGNOSTER_STATUS_JOB_FG}%}⚙" - [[ -n "$symbols" ]] && prompt_segment black default "$symbols" + [[ -n "$symbols" ]] && prompt_segment "$AGNOSTER_STATUS_BG" default "$symbols" } ## Main prompt