config / .zsh / prompt /
c40b4a5 7 years ago
1 contributor
113 lines | 3.701kb
autoload -U colors
colors
#setopt prompt_subst

# Modify the colors and symbols in these variables as desired.
#GIT_PROMPT_SYMBOL="$fg[blue]±"
GIT_PROMPT_PREFIX="$fg[green][$reset_color"
GIT_PROMPT_SUFFIX="$fg[green]]$reset_color"
GIT_PROMPT_AHEAD="$fg[red]ANUM$reset_color"
GIT_PROMPT_BEHIND="$fg[cyan]BNUM$reset_color"
GIT_PROMPT_MERGING="$fg[magenta]Z$reset_color"
GIT_PROMPT_UNTRACKED="$fg[red]●$reset_color"
GIT_PROMPT_MODIFIED="$fg[yellow]●$reset_color"
GIT_PROMPT_STAGED="$fg[green]●$reset_color"

# Show Git branch/tag, or name-rev if on detached head
parse_git_branch() {
    (git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD) 2> /dev/null
}

# Show different symbols as appropriate for various Git repository states
parse_git_state() {
# Compose this value via multiple conditional appends.
    local GIT_STATE=""

    local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
    if [ "$NUM_AHEAD" -gt 0 ]; then
        GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD}
    fi

    local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
    if [ "$NUM_BEHIND" -gt 0 ]; then
        GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND}
    fi

    local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)"
    if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then
        GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING
    fi

    if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then
        GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED
    fi

    if ! git diff --quiet 2> /dev/null; then
        GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED
    fi

    if ! git diff --cached --quiet 2> /dev/null; then
        GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED
    fi

    if [[ -n $GIT_STATE ]]; then
        echo "$GIT_STATE "
    fi
}

# If inside a Git repository, print its branch and state
git_prompt_string() {
    local git_where="$(parse_git_branch)"
    [ -n "$git_where" ] && echo "$GIT_PROMPT_PREFIX$(parse_git_state)$fg[yellow]${git_where#(refs/heads/|tags/)}$GIT_PROMPT_SUFFIX "
}

precmd () {
    local test_code_de_sortie=$?;
    if [ $__LINENO__ -gt 0 ]; then
        local __PROMPT__='[ '
        [[ $test_code_de_sortie != 0 ]] && __PROMPT__+="\e[41m\e[1;39m $test_code_de_sortie \e[0m "
        __PROMPT__+="\e[0;33m$(date '+%H:%M %x')\e[0m ]\n\n"
    fi
    (( __LINENO__ = $__LINENO__+ 1))
    nbrj=$(jobs | grep -c '^\[');
    if [ $nbrj -gt 1 ]; then
        __PROMPT__+="\e[1;34m$nbrj\e[0;34m jobs en cours\n"
    elif [ $nbrj -gt 0 ]; then 
        __PROMPT__+="\e[1;34m$nbrj\e[0;34m job en cours\n"
    fi
    local GIT_PROMPT_STRING="$(git_prompt_string)"
    if [ -n "$GIT_PROMPT_STRING" ]; then
        local GIT_REPO_DIR="$(git rev-parse --show-toplevel 2> /dev/null)"
    fi
    __PROMPT__+="$GIT_PROMPT_STRING"
    if test $SHLVL -eq 1 -o $TERM != "screen"; then
        __PROMPT__+="\e[1;33m$HOST\e[0m:"
    fi
    __PROMPT__+="\e[0;32m$([[ $UID == 0 ]] && echo '\e[47m\e[4;31m /!\ root /!\ \e[0m\e[0;31m ')"
    __PROMPT__+="${PWD/$GIT_REPO_DIR/\e[4m$GIT_REPO_DIR\e[0;32m}\e[0m"
    echo -e "$__PROMPT__"
}

set extented_glob
preexec () {
    if [[ "$TERM" == "screen" ]]; then
        TITLE=''
        local max=18
        local mask=' ...'
        local separator='▰'
        [[ $UID == 0 ]] && 1="☢$1"
        if [[ ${#1} -gt $max ]]; then
            TITLE="${1%% *}${mask}${1:${#1%% *}+${#mask}+${#1}-${max}:${#1}} $separator ${1:${#1%% *}:${#1}}"
        else
            TITLE+="$1 $separator"
        fi
        echo -ne "\ek${TITLE}\e\\"
    fi
}

PS1="${GRASS_VERSION:+%BGRASS%b }zsh %(!.#.$) "

PS2="%_ > "
PS4="%_:%N(%I) >"

# vim: filetype=zsh