1
0
Fork 0
mirror of synced 2024-12-04 21:15:35 -05:00
zimfw/modules/prompt/themes/magicmace.zsh-theme
Eric Nielsen 40ef228286 [prompt] magicmace: fix tab completion
The offender was `print -P`, where `-P` does [prompt
expansion](http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Prompt-Expansion).

I took the freedom to also change a few things:
Move the `add-zsh-hook precmd` up,
add `prompt_magicmace_` prefix to all function names,
add `COLOR_` prefix to all color global variables,
and move assignment to `PROMPT` to `prompt_magicmace_setup`.

Fixes #70, Closes #81
2016-09-27 15:58:45 -07:00

98 lines
2.8 KiB
Bash

#
# magicmace theme
# Ideas and code taken from:
# xero's zsh prompt <http://code.xero.nu/dotfiles>
# eriner's eriner prompt <https://github.com/Eriner/zim/blob/master/modules/prompt/themes/eriner.zsh-theme>
#
# Global variables
function {
COLOR_ROOT="%F{red}"
COLOR_USER="%F{cyan}"
COLOR_NORMAL="%F{white}"
COLOR_ERROR="%F{red}"
if [[ "$EUID" -ne "0" ]]; then
COLOR_USER_LEVEL="${COLOR_USER}"
else
COLOR_USER_LEVEL="${COLOR_ROOT}"
fi
}
# Status:
# - was there an error?
# - are there background jobs?
# - are we in a ranger session?
prompt_magicmace_status() {
local symbols=""
[[ ${RETVAL} -ne 0 ]] && symbols+="${COLOR_ERROR}${RETVAL}${COLOR_NORMAL}" # $? 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 -n "${COLOR_NORMAL}${symbols}${COLOR_USER_LEVEL}"
}
prompt_magicmace_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 -n "─[${COLOR_NORMAL}${git_info}${COLOR_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_magicmace_setup() {
autoload -Uz colors && colors
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
prompt_opts=(cr subst percent)
add-zsh-hook precmd prompt_magicmace_precmd
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
PROMPT='${COLOR_USER_LEVEL}$(prompt_magicmace_status)[${COLOR_NORMAL}$(short_pwd)${COLOR_USER_LEVEL}]$(prompt_magicmace_git)── ─%f '
}
prompt_magicmace_setup "$@"