config / .zsh / prompt /
Newer Older
106 lines | 3.508kb
commit initial
Sébastien MARQUE authored on 2016-10-31
1
autoload -U colors
2
colors
3
#setopt prompt_subst
4

            
5
# Modify the colors and symbols in these variables as desired.
6
GIT_PROMPT_SYMBOL="%{$fg[blue]%}±"
7
GIT_PROMPT_PREFIX="%{$fg[green]%}[%{$reset_color%}"
8
GIT_PROMPT_SUFFIX="%{$fg[green]%}]%{$reset_color%}"
9
GIT_PROMPT_AHEAD="%{$fg[red]%}ANUM%{$reset_color%}"
10
GIT_PROMPT_BEHIND="%{$fg[cyan]%}BNUM%{$reset_color%}"
11
GIT_PROMPT_MERGING="%{$fg[magenta]%}⚡︎%{$reset_color%}"
12
GIT_PROMPT_UNTRACKED="%{$fg[red]%}●%{$reset_color%}"
13
GIT_PROMPT_MODIFIED="%{$fg[yellow]%}●%{$reset_color%}"
14
GIT_PROMPT_STAGED="%{$fg[green]%}●%{$reset_color%}"
15

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

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

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

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

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

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

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

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

            
53
    if [[ -n $GIT_STATE ]]; then
54
        echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX"
55
    fi
56
}
57

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

            
64
precmd () {
65
    local test_code_de_sortie=$?;
66
    if [ $__LINENO__ -gt 0 ]; then
67
        __PROMPT__='[ '
68
        [[ $test_code_de_sortie != 0 ]] && __PROMPT__+="\e[41m\e[1;39m $test_code_de_sortie \e[0m "
69
        __PROMPT__+="\e[0;33m$(date '+%H:%M %x')\e[0m ]\n\n"
70
    fi
71
    (( __LINENO__ = $__LINENO__+ 1))
72
    nbrj=$(jobs | grep -c '^\[');
73
    if [ $nbrj -gt 1 ]; then
74
        __PROMPT__+="\e[1;34m$nbrj\e[0;34m jobs en cours\n"
75
    elif [ $nbrj -gt 0 ]; then 
76
        __PROMPT__+="\e[1;34m$nbrj\e[0;34m job en cours\n"
77
    fi
78
#    __PROMPT__+=$(git_prompt_string)
79
    __PROMPT__+="\e[0;32m$([[ $UID == 0 ]] && echo '\e[47m\e[4;31m /!\ root /!\ \e[0m\e[0;31m ')"
80
    __PROMPT__+="$(pwd)\e[0m"
81
    echo -e "$__PROMPT__"
82
}
83

            
84
set extented_glob
85
preexec () {
86
    if [[ "$TERM" == "screen" ]]; then
87
        TITLE=''
88
        local max=18
89
        local mask=' ...'
90
        local separator='▰'
91
        [[ $UID == 0 ]] && 1="☢$1"
92
        if [[ ${#1} -gt $max ]]; then
93
            TITLE="${1%% *}${mask}${1:${#1%% *}+${#mask}+${#1}-${max}:${#1}} $separator ${1:${#1%% *}:${#1}}"
94
        else
95
            TITLE+="$1 $separator"
96
        fi
97
        echo -ne "\ek${TITLE}\e\\"
98
    fi
99
}
100

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

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

            
106
# vim: filetype=zsh