New helper for colors
[zsh.git] / .zsh / 05_Functions.zsh
1 # -*- mode: sh;-*-
2
3 ## User-defined functions
4 #
5 cmd_exists ()
6 {
7 \which -p $1 >/dev/null 2>&1
8 }
9
10 preprint()
11 {
12 local my_color i
13 my_color=${2-"$prompt_colors[generic]"}
14
15 hbar=$T_
16 for i in {1..$((74 - ${#1} - 5))}; do
17 hbar=${hbar}$_t_q
18 done
19 hbar=${hbar}$_T
20
21 if [ "$1" != "" ]; then
22 print -Pn "${C_}$my_color;1${_C}${hbar}$T_$_t_u$_T${C_}0;$my_color${_C} $1 ${C_}0;$my_color;1${_C}$T_$_t_t$_t_q$_T\r${C_}0${_C}"
23 else
24 print -Pn "${C_}$my_color;1${_C}${hbar}$T_$_t_q$_t_q$_t_q$_t_q$_t_q$_T${C_}0${_C}"
25 fi
26 }
27
28 normal_user ()
29 {
30 if [ -e /etc/login.defs ]; then
31 eval `grep -v '^[$#]' /etc/login.defs | grep "^UID_" | tr -d '[:blank:]' | sed 's/^[A-Z_]\+/&=/'`
32 [ \( $UID -ge $UID_MIN \) ]
33 else
34 [ "`whoami`" != "root" ]
35 fi
36 }
37
38 privileged_user ()
39 {
40 ! normal_user
41 }
42
43 _jj_chpwd()
44 {
45 if ( cmd_exists git && test -d .git ); then
46 # Shows tracked branches and modified files
47 git checkout HEAD 2>&1 | sed 's/^/ /'
48 fi
49 }
50
51 if is434 ; then
52 add-zsh-hook chpwd _jj_chpwd
53 else
54 function chpwd() {
55 _jj_chpwd
56 }
57 fi
58
59 # Taken from oh-my-zsh
60 if is434; then
61 function dirpersiststore () {
62 dirs -p | perl -e 'foreach (reverse <STDIN>) {chomp;s/([& ])/\\$1/g ;print "if [ -d $_ ]; then pushd -q $_; fi\n"}' >| $zdirstore
63 }
64 add-zsh-hook zshexit dirpersiststore
65
66 function dirpersistrestore () {
67 if [ -f $zdirstore ]; then
68 source $zdirstore
69 fi
70 }
71 fi
72
73 docolors()
74 {
75 if zrcautoload colors && colors 2>/dev/null ; then
76 for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
77 eval BOLD_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
78 eval $color='%{$fg[${(L)color}]%}'
79 (( count = $count + 1 ))
80 done
81 NO_COLOR="%{${reset_color}%}"
82 else
83 BLUE=$'%{\e[1;34m%}'
84 RED=$'%{\e[1;31m%}'
85 GREEN=$'%{\e[1;32m%}'
86 CYAN=$'%{\e[1;36m%}'
87 WHITE=$'%{\e[1;37m%}'
88 MAGENTA=$'%{\e[1;35m%}'
89 YELLOW=$'%{\e[1;33m%}'
90 NO_COLOR=$'%{\e[0m%}'
91 fi
92 }