From 0a33a3de90c830085d0f8ce98d665d4a86728b59 Mon Sep 17 00:00:00 2001 From: Tmplt Date: Thu, 28 Jul 2016 02:22:44 +0200 Subject: [PATCH] [prompt] magicmace: add theme Closes #69 --- .../prompt/functions/prompt_magicmace_setup | 1 + modules/prompt/themes/magicmace.zsh-theme | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 120000 modules/prompt/functions/prompt_magicmace_setup create mode 100644 modules/prompt/themes/magicmace.zsh-theme diff --git a/modules/prompt/functions/prompt_magicmace_setup b/modules/prompt/functions/prompt_magicmace_setup new file mode 120000 index 0000000..cafc569 --- /dev/null +++ b/modules/prompt/functions/prompt_magicmace_setup @@ -0,0 +1 @@ +../themes/magicmace.zsh-theme \ No newline at end of file diff --git a/modules/prompt/themes/magicmace.zsh-theme b/modules/prompt/themes/magicmace.zsh-theme new file mode 100644 index 0000000..481541e --- /dev/null +++ b/modules/prompt/themes/magicmace.zsh-theme @@ -0,0 +1,100 @@ +# +# magicmace theme +# Ideas and code taken from: +# xero's zsh prompt +# eriner's eriner prompt +# + +# Global variables +function { + COLOR_ROOT="%F{red}" + COLOR_USER="%F{cyan}" + COLOR_NORMAL="%F{white}" + COLOR_ERROR="%F{red}" + + if [[ "$EUID" -ne "0" ]]; then + USER_LEVEL="${COLOR_USER}" + else + USER_LEVEL="${COLOR_ROOT}" + fi +} + +# Status: +# - was there an error? +# - are there background jobs? +# - are we in a ranger session? +prompt_status() { + local symbols="" + + [[ ${RETVAL} -ne 0 ]] && symbols+='${COLOR_ERROR}${RETVAL}${COLOR_NORMAL}' # 'e' for error. + [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+='b' # 'b' for background. + [[ ${RANGER_LEVEL} -ne 0 ]] && symbols+='r' # 'r' for... you guessed it! + + [[ -n ${symbols} ]] && print -Pn "─${COLOR_NORMAL}${symbols}${COLOR_USER}─" +} + +prompt_git() { + local ico_dirty='*' + local ico_ahead='🠙' + local ico_behind='🠛' + local ico_diverged='⥮' + local git_info="" + + is_dirty() { + test -n "$(git status --porcelain --ignore-submodules)" + } + + vcs_info + git_info=${vcs_info_msg_0_} # branch name + if [[ -n ${git_info} ]]; then + + if is_dirty; then + git_info+=${ico_dirty} + fi + + stat=$(git status 2> /dev/null | command sed -n 2p) + case "${stat}" in + *ahead*) git_info+=${ico_ahead} ;; + *behind*) git_info+=${ico_behind} ;; + *diverged*) git_info+=${ico_diverged} ;; + *);; + esac + + print -Pn \ + "${USER_LEVEL}─[${COLOR_NORMAL}${git_info}${USER_LEVEL}]" + fi +} + +prompt_magicmace_precmd() { + # While it would be apt to have this as a local variable in prompt_status(), + # $? (returned value) and ${(%):-%?} ("The return status of the last command + # executed just before the prompt") both change before executing the function. + # Is this perhaps because prompt_status _is_ here? + # We could also just set $? as an argument, and thus get our nifty local variable, + # but that's stretching it, and makes the code harder to read. + RETVAL=$? + + PROMPT='${USER_LEVEL}$(prompt_status)[${COLOR_NORMAL}$(short_pwd)${USER_LEVEL}]$(prompt_git)── -%f ' +} + +prompt_magicmace_setup() { + autoload -Uz colors && colors + autoload -Uz add-zsh-hook + autoload -Uz vcs_info + + prompt_opts=(cr subst percent) + + zstyle ':vcs_info:*' enable git + zstyle ':vcs_info:*' check-for-changes false + zstyle ':vcs_info:*' use-simple true + # Only export branch name, as that is the only data we need. + zstyle ':vcs_info:*' max-exports 1 + zstyle ':vcs_info:git*' formats '%b' + + # Call git directly, ignoring aliases under the same name. + zstyle ':vcs_info:git:*:-all-' command =git + + add-zsh-hook precmd prompt_magicmace_precmd +} + +prompt_magicmace_setup "$@"