summaryrefslogtreecommitdiff
path: root/11_Prompts.zsh
blob: e1b759b650d70c844a483e1c4fe8497c3b8e9f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
##
## Part of configuration files for Zsh 4
## by Hugues Hiegel <hugues@hiegel.fr>
## 
## NO WARRANTY PROVIDED, USE AT YOUR OWN RISKS
##
## You are encouraged to use, modify, and redistribute
## these files with or without this notice.
## 

# Pour personnaliser les couleurs du prompt, configurez ces variables :
#  - PS1_ROOT pour la couleur du prompt ROOT
#  - PS1_USER pour la couleur du prompt USER local
#  - PS1_USER_SSH pour la couleur du prompt USER distant (en ssh)

PS1_ROOT=${PS1_ROOT:-$RED}
PS1_USER=${PS1_USER:-$BLUE}
PS1_USER_SSH=${PS1_USER_SSH:-$MAGENTA}
GENERIC=`print -Pn "%(! $PS1_ROOT $PS1_USER)"`

normal_user && if ( [ "$SSH_TTY" != "" ] )
then
    # Permet de faire une distinction rapide entre les shells locaux
    # et les shells distants. C'est trop bon, mangez-en !
    GENERIC=${PS1_USER_SSH:-$GENERIC}
fi

c_=[
_c=m
C_="%{$c_"
_C="$_c%}"

## Les couleurs !! ##
COLOR_PATH="0;$GENERIC;$BOLD"
COLOR_TERM="0;$GENERIC"
COLOR_USER="0;$GENERIC"
COLOR_HOST="0;$GENERIC"
COLOR_HIST="$VOID"
COLOR_AROB="0;1;%(! $BOLD; )$GENERIC"
COLOR_DIES="0;$GENERIC"
COLOR_DOUBLEDOT="0;%(! $VOID $VOID)"
COLOR_PAREN="0;$CYAN"
COLOR_BAR="0;$GENERIC;$BOLD"
COLOR_BRACES=$COLOR_BAR

COLOR_BRANCH_OR_REV="0;$GENERIC"
COLOR_NOT_UP_TODATE="0;$YELLOW"
COLOR_TOBE_COMMITED="0;$YELLOW;$BOLD"

COLOR_CMD="$VOID"
COLOR_EXEC="$VOID"

COLOR_ERRR="$BOLD;$YELLOW"
COLOR_DATE="0;$GENERIC"

## Prompts
#
# Pour plus d'infos sur les paramètres d'expansion du prompt:
#  man zshmisc(1)
#
# La définition des prompts est séparée de celles desvariables d'environnement
# classiques pour permettre de configurer, par exemple, les couleurs par défaut
# dans ces fichiers. 

## Automagic funcs
#
# Fonctions exécutées automatiquement sous certaines conditions
#
# chpwd		: changement de répertoire
# preexec	: avant d'exécuter une commande
# precmd	: avant d'afficher le prompt
#

term_title()
{
  [[ -t 1 ]] &&
    case $TERM in
      sun-cmd)
        print -Pn "\e]l%n@%m %~$1\e\\" ;;
      *term*|rxvt*)
	    print -Pn "\e]0;%n@%m (%l) %~$1\a" ;;
	  *)
	  	;;
    esac
}

preexec ()
{
    term_title " ··· $(echo $1 | tr '\n' ';' | sed 's/%/%%/g')"
	print -Pn "$C_$COLOR_EXEC$_C"
}

GITCHECK=${GITCHECK:-yeah}
#SVNCHECK=${SVNCHECK:-yeah}
#unset GITCHECK SVNCHECK

precmd ()
{
    term_title

	DATE="%D{%H:%M:%S %d/%m/%Y}"
	datesize=`print -Pn $DATE`
	datesize=${#datesize}
	ERROR=%(? "$C_$COLOR_BAR$_C----" "%4>>"$C_$COLOR_ERRR$_C"%?$C_$COLOR_BAR$_C"---"%>>")
	errorsize=4

	## GIT TRACKING ##
	GITBRANCH=$(git branch 2>&- | grep -E '^\* ' | cut -c3-)
	if [ "$GITBRANCH" != "" ]
	then
		if [ "$GITCHECK" != "" ]
		then
			print -n "Checking git status...\r"
			_git_status=$(git-runstatus 2>&- | grep -E '^# ([[:alpha:]]+ )+(but not|to be)( [[:alpha:]]+)+:$')
			if   [ "$(grep "but not" <<< $_git_status)" != "" ] ; then 
				COLOR_GIT=$COLOR_NOT_UP_TODATE
			elif [ "$(grep "to be committed" <<< $_git_status)" != "" ] ; then 
				COLOR_GIT=$COLOR_TOBE_COMMITED
			else
				COLOR_GIT=$COLOR_BRANCH_OR_REV
			fi

		fi
	fi
	GitBranch=${GITBRANCH:+:$GITBRANCH}
	GITBRANCH=${GITBRANCH:+$C_$COLOR_DOUBLEDOT$_C:$C_$COLOR_GIT$_C$GITBRANCH}

	## SVN TRACKING ##
	SVNREV=$(svn info 2>&- | grep '^Révision : ' | sed 's/^.* : /r/')
	if [ "$SVNREV" != "" ]
	then
		if [ "$SVNCHECK" != "" ] && ( print -n "Checking svn status...\r" ; [ $(svn status 2>&- | grep -v '^?' | wc -l) -gt 0 ] )
		then
			COLOR_SVN=$COLOR_NOT_UP_TODATE
		else
			COLOR_SVN=$COLOR_BRANCH_OR_REV
		fi
	else
		SVNREV=
	fi

	## First line of prompt : 
	#
	# -ERR------------------------git-svn-[ date ]-
	#
	spaceleft=$(($COLUMNS - ${errorsize} - ${#SVNREV} - 3 - ${datesize} - 3))

	unset HBAR
	for h in {1..$(($spaceleft - 1))}
	do
		HBAR=$HBAR-
	done

	## Second line of prompt : don't let the path garbage the entire line
	MY_PATH="%(!.%d.%~)"
	pathsize=`print -Pn $MY_PATH`
	pathsize=${#pathsize}
	spaceleft=`print -Pn "%n@%m $GitBranch %#-ls -laCdtrux-[ $DATE ]-"`
	spaceleft=$(($COLUMNS - ${#spaceleft}))
	minimalsize=`print -Pn "%1~"`
	minimalsize=$((3 + ${#minimalsize}))
	[ $spaceleft -lt $minimalsize ] && spaceleft=$minimalsize
	CURDIR="$C_$COLOR_PATH$_C%`echo $spaceleft`<..<"$MY_PATH"%<<$C_$VOID$_C"

## Le prompt le plus magnifique du monde, et c'est le mien ! 
# Affiche l'user, l'host, le tty et le pwd. Rien que ça... 
# Note que pour le pwd, on n'affiche que les 4 derniers dossiers pour éviter
# de pourrir le fenêtre de terminal avec un prompt à rallonge.
	PS1=$C_$COLOR_BAR$_C"-""$ERROR$HBAR"$C_$COLOR_SVN$_C"$SVNREV"$C_$COLOR_BAR$_C"-"$C_$COLOR_BRACES$_C"[ "$C_$COLOR_DATE$_C$DATE$C_$COLOR_BRACES$_C" ]"$C_$COLOR_BAR$_C"-
"$C_$COLOR_USER$_C"%n"$C_$COLOR_AROB$_C"@"$C_$COLOR_HOST$_C"%m $CURDIR$GITBRANCH "$C_$COLOR_DIES$_C"%#"$C_$COLOR_CMD$_C" "
}

chpwd()
{
    which todo > /dev/null 2>&1 && todo
}


# Prompt level 2
PS2="%{%}%B%_%b%{%}%B>%b%{%} "

# Prompt level 3
PS3="?# "

# Prompt level 4
PS4="+%N:%i> "

# Prompt de droite, pour l'heure et le code d'erreur de la dernière commande
#RPS1="%(?;;"$C_$COLOR_ERRR$_C"%?"$C_$VOID$_C")"

# Ultime : prompt de correction :-)
SPROMPT="zsh: $C_$BLUE$_C%B'%R'%b$C_$VOID$_C ? Vous ne vouliez pas plutôt $C_$MAGENTA$_C%B'%r'%b$C_$VOID$_C ? [%BN%byae] "